Skip to content

Commit a104fdc

Browse files
W-Maixiaoxiang781216
authored andcommitted
tools/minudumpserver: support auto start GDB and execute some commands
VELAPLATFO-16485 like ```bash ./nuttx/tools/minidumpserver.py -l log.log -e vela_ap.elf -a arm --gdb prebuilts/gcc/linux/arm/bin/arm-none-eabi-gdb -i ``` Signed-off-by: xinbingnan <xinbingnan@xiaomi.com>
1 parent 8d1cb46 commit a104fdc

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

tools/minidumpserver.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import argparse
2121
import binascii
2222
import logging
23+
import multiprocessing
2324
import os
2425
import re
2526
import shutil
2627
import socket
2728
import struct
29+
import subprocess
2830
import sys
2931

3032
import elftools
@@ -39,6 +41,9 @@
3941

4042
GDB_SIGNAL_DEFAULT = 7
4143

44+
DEFAULT_GDB_INIT_CMD = "-ex 'bt full' -ex 'info reg' -ex 'display /40i $pc-40'"
45+
46+
4247
logger = logging.getLogger()
4348

4449
reg_table = {
@@ -605,7 +610,26 @@ def arg_parser():
605610
choices=[arch for arch in reg_table.keys()],
606611
)
607612
parser.add_argument("-p", "--port", help="gdbport", type=int, default=1234)
608-
parser.add_argument("--debug", action="store_true", default=False)
613+
parser.add_argument(
614+
"-g",
615+
"--gdb",
616+
help="provided a custom GDB path, automatically start GDB session and exit minidumpserver when exit GDB. ",
617+
type=str,
618+
)
619+
parser.add_argument(
620+
"-i",
621+
"--init-cmd",
622+
nargs="?",
623+
default=argparse.SUPPRESS,
624+
help="provided a custom GDB init command, automatically start GDB sessions and input what you provide. "
625+
f"if you don't provide any command, it will use default command [{DEFAULT_GDB_INIT_CMD}]. ",
626+
)
627+
parser.add_argument(
628+
"--debug",
629+
action="store_true",
630+
default=False,
631+
help="if enabled, it will show more logs.",
632+
)
609633
return parser.parse_args()
610634

611635

@@ -675,9 +699,9 @@ def main(args):
675699

676700
config_log(args.debug)
677701

678-
res = auto_parse_log_file(args.logfile)
702+
selected_log = auto_parse_log_file(args.logfile)
679703

680-
log = DumpLogFile(res)
704+
log = DumpLogFile(selected_log)
681705
log.parse(args.arch)
682706
elf = DumpELFFile(args.elffile)
683707
elf.parse()
@@ -693,9 +717,34 @@ def main(args):
693717
gdbserver.bind(("", args.port))
694718
gdbserver.listen(1)
695719

720+
gdb_exec = "gdb" if not args.gdb else args.gdb
721+
722+
gdb_init_cmd = ""
723+
if hasattr(args, "init_cmd"):
724+
if args.init_cmd is not None:
725+
gdb_init_cmd = args.init_cmd.strip()
726+
else:
727+
gdb_init_cmd = DEFAULT_GDB_INIT_CMD
728+
729+
gdb_cmd = (
730+
f"{gdb_exec} {args.elffile} -ex 'target remote localhost:{args.port}' "
731+
f"{gdb_init_cmd}"
732+
)
696733
logger.info(f"Waiting GDB connection on port {args.port} ...")
697-
logger.info("Press Ctrl+C to stop ...")
698-
logger.info(f'Hint: gdb {args.elffile} -ex "target remote localhost:{args.port}"')
734+
735+
if not args.gdb:
736+
logger.info("Press Ctrl+C to stop ...")
737+
logger.info(f"Hint: {gdb_cmd}")
738+
else:
739+
logger.info(f"Run GDB command: {gdb_cmd}")
740+
741+
def gdb_run(cmd):
742+
try:
743+
subprocess.run(cmd, shell=True)
744+
except KeyboardInterrupt:
745+
pass
746+
747+
multiprocessing.Process(target=gdb_run, args=(gdb_cmd,)).start()
699748

700749
while True:
701750
try:

0 commit comments

Comments
 (0)