Skip to content

Commit 524cb33

Browse files
W-Maixiaoxiang781216
authored andcommitted
tools/minudumpserver: support auto parse log file feature
VELAPLATFO-16476 1. support to extract log and list all possible dumps from a full log file Signed-off-by: xinbingnan <[email protected]>
1 parent b57e43b commit 524cb33

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

tools/minidumpserver.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import logging
2323
import os
2424
import re
25+
import shutil
2526
import socket
2627
import struct
2728
import sys
@@ -286,7 +287,7 @@ def get_memories(self):
286287

287288

288289
class DumpLogFile:
289-
def __init__(self, logfile: str):
290+
def __init__(self, logfile):
290291
self.logfile = logfile
291292
self.registers = []
292293
self.__memories = list()
@@ -350,8 +351,11 @@ def parse(self, arch):
350351
data = bytes()
351352
start = 0
352353

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()
355359

356360
for line_num, line in enumerate(lines):
357361
if line == "":
@@ -614,6 +618,52 @@ def config_log(debug):
614618
logging.basicConfig(format="[%(levelname)s][%(name)s] %(message)s")
615619

616620

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+
617667
def main(args):
618668
if not os.path.isfile(args.elffile):
619669
logger.error(f"Cannot find file {args.elffile}, exiting...")
@@ -625,7 +675,9 @@ def main(args):
625675

626676
config_log(args.debug)
627677

628-
log = DumpLogFile(args.logfile)
678+
res = auto_parse_log_file(args.logfile)
679+
680+
log = DumpLogFile(res)
629681
log.parse(args.arch)
630682
elf = DumpELFFile(args.elffile)
631683
elf.parse()

0 commit comments

Comments
 (0)