|
30 | 30 | import struct
|
31 | 31 | import subprocess
|
32 | 32 | import sys
|
| 33 | +import traceback |
33 | 34 |
|
34 | 35 | import elftools
|
35 | 36 | from elftools.elf.elffile import ELFFile
|
@@ -263,7 +264,7 @@ def __init__(self, elffile: str):
|
263 | 264 | self.__xlen = None
|
264 | 265 | self.__text = 0
|
265 | 266 |
|
266 |
| - def parse(self): |
| 267 | + def parse(self, load_symbol: bool): |
267 | 268 | self.__memories = []
|
268 | 269 | elf = ELFFile.load_from_path(self.elffile)
|
269 | 270 | self.__arch = elf.get_machine_arch().lower().replace("-", "")
|
@@ -313,26 +314,27 @@ def parse(self):
|
313 | 314 | if segment.header.p_flags & 1 and not self.__text:
|
314 | 315 | self.__text = segment.header.p_vaddr
|
315 | 316 |
|
316 |
| - symtab = elf.get_section_by_name(".symtab") |
317 |
| - self.symbol = {} |
318 |
| - for symbol in symtab.iter_symbols(): |
319 |
| - if symbol["st_info"]["type"] != "STT_OBJECT": |
320 |
| - continue |
| 317 | + self.load_symbol = load_symbol |
| 318 | + if load_symbol: |
| 319 | + symtab = elf.get_section_by_name(".symtab") |
| 320 | + self.symbol = {} |
| 321 | + for symbol in symtab.iter_symbols(): |
| 322 | + if symbol["st_info"]["type"] != "STT_OBJECT": |
| 323 | + continue |
321 | 324 |
|
322 |
| - if symbol.name in ( |
323 |
| - "g_tcbinfo", |
324 |
| - "g_pidhash", |
325 |
| - "g_npidhash", |
326 |
| - "g_last_regs", |
327 |
| - "g_running_tasks", |
328 |
| - ): |
329 |
| - self.symbol[symbol.name] = symbol |
330 |
| - logger.debug( |
331 |
| - f"name:{symbol.name} size:{symbol['st_size']} value:{hex(symbol['st_value'])}" |
332 |
| - ) |
| 325 | + if symbol.name in ( |
| 326 | + "g_tcbinfo", |
| 327 | + "g_pidhash", |
| 328 | + "g_npidhash", |
| 329 | + "g_last_regs", |
| 330 | + "g_running_tasks", |
| 331 | + ): |
| 332 | + self.symbol[symbol.name] = symbol |
| 333 | + logger.debug( |
| 334 | + f"name:{symbol.name} size:{symbol['st_size']} value:{hex(symbol['st_value'])}" |
| 335 | + ) |
333 | 336 |
|
334 | 337 | elf.close()
|
335 |
| - |
336 | 338 | return True
|
337 | 339 |
|
338 | 340 | def merge(self, other):
|
@@ -537,25 +539,30 @@ def __init__(
|
537 | 539 |
|
538 | 540 | self.threadinfo = []
|
539 | 541 | self.current_thread = 0
|
540 |
| - try: |
541 |
| - self.parse_thread() |
542 |
| - logger.debug(f"Have {len(self.threadinfo)} threads to debug.") |
543 |
| - if len(self.threadinfo) == 0: |
544 |
| - logger.critical( |
545 |
| - "Check if your coredump or raw file matches the ELF file" |
546 |
| - ) |
547 |
| - sys.exit(1) |
| 542 | + if elffile.load_symbol: |
| 543 | + try: |
| 544 | + self.parse_thread() |
| 545 | + logger.debug(f"Have {len(self.threadinfo)} threads to debug.") |
| 546 | + if len(self.threadinfo) == 0: |
| 547 | + logger.critical( |
| 548 | + "Check if your coredump or raw file matches the ELF file" |
| 549 | + ) |
| 550 | + sys.exit(1) |
548 | 551 |
|
549 |
| - if arch in reg_fix_value.keys(): |
550 |
| - self.regfix = True |
551 |
| - logger.info(f"Current arch is {arch}, need reg index fix.") |
| 552 | + if arch in reg_fix_value.keys(): |
| 553 | + self.regfix = True |
| 554 | + logger.info(f"Current arch is {arch}, need reg index fix.") |
552 | 555 |
|
553 |
| - except TypeError: |
554 |
| - if not self.registers: |
555 |
| - logger.critical( |
556 |
| - "Logfile, coredump, or rawfile do not contain register. Please check if the files are correct." |
557 |
| - ) |
558 |
| - sys.exit(1) |
| 556 | + except TypeError: |
| 557 | + if not self.registers: |
| 558 | + logger.critical( |
| 559 | + "Logfile, coredump, or rawfile do not contain register," |
| 560 | + "Please check if the files are correct." |
| 561 | + ) |
| 562 | + |
| 563 | + stack_trace = traceback.format_exc() |
| 564 | + logger.debug(stack_trace) |
| 565 | + sys.exit(1) |
559 | 566 |
|
560 | 567 | def get_gdb_packet(self):
|
561 | 568 | socket = self.socket
|
@@ -1017,6 +1024,15 @@ def arg_parser():
|
1017 | 1024 | help="coredump file, will prase memory in this file",
|
1018 | 1025 | )
|
1019 | 1026 |
|
| 1027 | + parser.add_argument( |
| 1028 | + "-s", |
| 1029 | + "--symbol", |
| 1030 | + action="store_true", |
| 1031 | + help="Analyze the symbol table in the ELF file, use in thread awareness" |
| 1032 | + "if use logfile input, this option will is false by default" |
| 1033 | + "if use rawfile or coredump input, this option will is true by default", |
| 1034 | + ) |
| 1035 | + |
1020 | 1036 | parser.add_argument(
|
1021 | 1037 | "--debug",
|
1022 | 1038 | action="store_true",
|
@@ -1101,7 +1117,11 @@ def main(args):
|
1101 | 1117 |
|
1102 | 1118 | config_log(args.debug)
|
1103 | 1119 | elf = DumpELFFile(args.elffile[0])
|
1104 |
| - elf.parse() |
| 1120 | + if args.symbol is False: |
| 1121 | + if args.rawfile or args.coredump: |
| 1122 | + args.symbol = True |
| 1123 | + |
| 1124 | + elf.parse(args.symbol) |
1105 | 1125 | elf_texts = [elf.text()]
|
1106 | 1126 | for name in args.elffile[1:]:
|
1107 | 1127 | other = DumpELFFile(name)
|
|
0 commit comments