Skip to content

Commit 7c37a46

Browse files
anjiahao1xiaoxiang781216
authored andcommitted
gdbserver:skip load symbol if use logfile as input
Signed-off-by: anjiahao <[email protected]>
1 parent ddf44de commit 7c37a46

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

tools/gdbserver.py

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import struct
3131
import subprocess
3232
import sys
33+
import traceback
3334

3435
import elftools
3536
from elftools.elf.elffile import ELFFile
@@ -263,7 +264,7 @@ def __init__(self, elffile: str):
263264
self.__xlen = None
264265
self.__text = 0
265266

266-
def parse(self):
267+
def parse(self, load_symbol: bool):
267268
self.__memories = []
268269
elf = ELFFile.load_from_path(self.elffile)
269270
self.__arch = elf.get_machine_arch().lower().replace("-", "")
@@ -313,26 +314,27 @@ def parse(self):
313314
if segment.header.p_flags & 1 and not self.__text:
314315
self.__text = segment.header.p_vaddr
315316

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
321324

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+
)
333336

334337
elf.close()
335-
336338
return True
337339

338340
def merge(self, other):
@@ -537,25 +539,30 @@ def __init__(
537539

538540
self.threadinfo = []
539541
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)
548551

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.")
552555

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)
559566

560567
def get_gdb_packet(self):
561568
socket = self.socket
@@ -1017,6 +1024,15 @@ def arg_parser():
10171024
help="coredump file, will prase memory in this file",
10181025
)
10191026

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+
10201036
parser.add_argument(
10211037
"--debug",
10221038
action="store_true",
@@ -1101,7 +1117,11 @@ def main(args):
11011117

11021118
config_log(args.debug)
11031119
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)
11051125
elf_texts = [elf.text()]
11061126
for name in args.elffile[1:]:
11071127
other = DumpELFFile(name)

0 commit comments

Comments
 (0)