Skip to content

Commit

Permalink
gdbserver:skip load symbol if use logfile as input
Browse files Browse the repository at this point in the history
Signed-off-by: anjiahao <[email protected]>
  • Loading branch information
anjiahao1 committed Oct 15, 2024
1 parent 578edf5 commit bf0669a
Showing 1 changed file with 56 additions and 36 deletions.
92 changes: 56 additions & 36 deletions tools/gdbserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import struct
import subprocess
import sys
import traceback

import elftools
from elftools.elf.elffile import ELFFile
Expand Down Expand Up @@ -263,7 +264,7 @@ def __init__(self, elffile: str):
self.__xlen = None
self.__text = 0

def parse(self):
def parse(self, load_symbol: bool):
self.__memories = []
elf = ELFFile.load_from_path(self.elffile)
self.__arch = elf.get_machine_arch().lower().replace("-", "")
Expand Down Expand Up @@ -313,26 +314,27 @@ def parse(self):
if segment.header.p_flags & 1 and not self.__text:
self.__text = segment.header.p_vaddr

symtab = elf.get_section_by_name(".symtab")
self.symbol = {}
for symbol in symtab.iter_symbols():
if symbol["st_info"]["type"] != "STT_OBJECT":
continue
self.load_symbol = load_symbol
if load_symbol:
symtab = elf.get_section_by_name(".symtab")
self.symbol = {}
for symbol in symtab.iter_symbols():
if symbol["st_info"]["type"] != "STT_OBJECT":
continue

if symbol.name in (
"g_tcbinfo",
"g_pidhash",
"g_npidhash",
"g_last_regs",
"g_running_tasks",
):
self.symbol[symbol.name] = symbol
logger.debug(
f"name:{symbol.name} size:{symbol['st_size']} value:{hex(symbol['st_value'])}"
)
if symbol.name in (
"g_tcbinfo",
"g_pidhash",
"g_npidhash",
"g_last_regs",
"g_running_tasks",
):
self.symbol[symbol.name] = symbol
logger.debug(
f"name:{symbol.name} size:{symbol['st_size']} value:{hex(symbol['st_value'])}"
)

elf.close()

return True

def merge(self, other):
Expand Down Expand Up @@ -537,25 +539,30 @@ def __init__(

self.threadinfo = []
self.current_thread = 0
try:
self.parse_thread()
logger.debug(f"Have {len(self.threadinfo)} threads to debug.")
if len(self.threadinfo) == 0:
logger.critical(
"Check if your coredump or raw file matches the ELF file"
)
sys.exit(1)
if elffile.load_symbol:
try:
self.parse_thread()
logger.debug(f"Have {len(self.threadinfo)} threads to debug.")
if len(self.threadinfo) == 0:
logger.critical(
"Check if your coredump or raw file matches the ELF file"
)
sys.exit(1)

if arch in reg_fix_value.keys():
self.regfix = True
logger.info(f"Current arch is {arch}, need reg index fix.")
if arch in reg_fix_value.keys():
self.regfix = True
logger.info(f"Current arch is {arch}, need reg index fix.")

except TypeError:
if not self.registers:
logger.critical(
"Logfile, coredump, or rawfile do not contain register. Please check if the files are correct."
)
sys.exit(1)
except TypeError:
if not self.registers:
logger.critical(
"Logfile, coredump, or rawfile do not contain register,"
"Please check if the files are correct."
)

stack_trace = traceback.format_exc()
logger.debug(stack_trace)
sys.exit(1)

def get_gdb_packet(self):
socket = self.socket
Expand Down Expand Up @@ -1017,6 +1024,15 @@ def arg_parser():
help="coredump file, will prase memory in this file",
)

parser.add_argument(
"-s",
"--symbol",
action="store_true",
help="Analyze the symbol table in the ELF file, use in thread awareness"
"if use logfile input, this option will is false by default"
"if use rawfile or coredump input, this option will is true by default",
)

parser.add_argument(
"--debug",
action="store_true",
Expand Down Expand Up @@ -1101,7 +1117,11 @@ def main(args):

config_log(args.debug)
elf = DumpELFFile(args.elffile[0])
elf.parse()
if args.symbol is False:
if args.rawfile or args.coredump:
args.symbol = True

elf.parse(args.symbol)
elf_texts = [elf.text()]
for name in args.elffile[1:]:
other = DumpELFFile(name)
Expand Down

0 comments on commit bf0669a

Please sign in to comment.