20
20
import argparse
21
21
import binascii
22
22
import logging
23
+ import multiprocessing
23
24
import os
24
25
import re
25
26
import shutil
26
27
import socket
27
28
import struct
29
+ import subprocess
28
30
import sys
29
31
30
32
import elftools
39
41
40
42
GDB_SIGNAL_DEFAULT = 7
41
43
44
+ DEFAULT_GDB_INIT_CMD = "-ex 'bt full' -ex 'info reg' -ex 'display /40i $pc-40'"
45
+
46
+
42
47
logger = logging .getLogger ()
43
48
44
49
reg_table = {
@@ -605,7 +610,26 @@ def arg_parser():
605
610
choices = [arch for arch in reg_table .keys ()],
606
611
)
607
612
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
+ )
609
633
return parser .parse_args ()
610
634
611
635
@@ -675,9 +699,9 @@ def main(args):
675
699
676
700
config_log (args .debug )
677
701
678
- res = auto_parse_log_file (args .logfile )
702
+ selected_log = auto_parse_log_file (args .logfile )
679
703
680
- log = DumpLogFile (res )
704
+ log = DumpLogFile (selected_log )
681
705
log .parse (args .arch )
682
706
elf = DumpELFFile (args .elffile )
683
707
elf .parse ()
@@ -693,9 +717,34 @@ def main(args):
693
717
gdbserver .bind (("" , args .port ))
694
718
gdbserver .listen (1 )
695
719
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
+ )
696
733
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 ()
699
748
700
749
while True :
701
750
try :
0 commit comments