22
22
import logging
23
23
import os
24
24
import re
25
+ import shutil
25
26
import socket
26
27
import struct
27
28
import sys
@@ -286,7 +287,7 @@ def get_memories(self):
286
287
287
288
288
289
class DumpLogFile :
289
- def __init__ (self , logfile : str ):
290
+ def __init__ (self , logfile ):
290
291
self .logfile = logfile
291
292
self .registers = []
292
293
self .__memories = list ()
@@ -350,8 +351,11 @@ def parse(self, arch):
350
351
data = bytes ()
351
352
start = 0
352
353
353
- with open (self .logfile , "r" ) as f :
354
- lines = f .readlines ()
354
+ if isinstance (self .logfile , list ):
355
+ lines = self .logfile
356
+ else :
357
+ with open (self .logfile , "r" ) as f :
358
+ lines = f .readlines ()
355
359
356
360
for line_num , line in enumerate (lines ):
357
361
if line == "" :
@@ -614,6 +618,52 @@ def config_log(debug):
614
618
logging .basicConfig (format = "[%(levelname)s][%(name)s] %(message)s" )
615
619
616
620
621
+ def auto_parse_log_file (logfile ):
622
+ with open (logfile , errors = "ignore" ) as f :
623
+ dumps = []
624
+ tmp_dmp = []
625
+ start = False
626
+ for line in f .readlines ():
627
+ line = line .strip ()
628
+ if (
629
+ "up_dump_register" in line
630
+ or "dump_stack" in line
631
+ or "stack_dump" in line
632
+ ):
633
+ start = True
634
+ else :
635
+ if start :
636
+ start = False
637
+ dumps .append (tmp_dmp )
638
+ tmp_dmp = []
639
+ if start :
640
+ tmp_dmp .append (line )
641
+
642
+ if start :
643
+ dumps .append (tmp_dmp )
644
+
645
+ terminal_width , _ = shutil .get_terminal_size ()
646
+ terminal_width = max (terminal_width - 4 , 0 )
647
+
648
+ def get_one_line (lines ):
649
+ return " " .join (lines [:2 ])[:terminal_width ]
650
+
651
+ if len (dumps ) == 0 :
652
+ logger .error (f"Cannot find any dump in { logfile } , exiting..." )
653
+ sys .exit (1 )
654
+
655
+ if len (dumps ) == 1 :
656
+ return dumps [0 ]
657
+
658
+ for i in range (len (dumps )):
659
+ print (f"{ i } : { get_one_line (dumps [i ])} " )
660
+
661
+ index_input = input ("Dump number[0]: " ).strip ()
662
+ if index_input == "" :
663
+ index_input = 0
664
+ return dumps [int (index_input )]
665
+
666
+
617
667
def main (args ):
618
668
if not os .path .isfile (args .elffile ):
619
669
logger .error (f"Cannot find file { args .elffile } , exiting..." )
@@ -625,7 +675,9 @@ def main(args):
625
675
626
676
config_log (args .debug )
627
677
628
- log = DumpLogFile (args .logfile )
678
+ res = auto_parse_log_file (args .logfile )
679
+
680
+ log = DumpLogFile (res )
629
681
log .parse (args .arch )
630
682
elf = DumpELFFile (args .elffile )
631
683
elf .parse ()
0 commit comments