1#!/usr/bin/env python3 2# 3# Copyright (c) 2020 Intel Corporation 4# 5# SPDX-License-Identifier: Apache-2.0 6 7import argparse 8import binascii 9import sys 10 11 12COREDUMP_PREFIX_STR = "#CD:" 13 14COREDUMP_BEGIN_STR = COREDUMP_PREFIX_STR + "BEGIN#" 15COREDUMP_END_STR = COREDUMP_PREFIX_STR + "END#" 16COREDUMP_ERROR_STR = COREDUMP_PREFIX_STR + "ERROR CANNOT DUMP#" 17 18 19def parse_args(): 20 parser = argparse.ArgumentParser(allow_abbrev=False) 21 22 parser.add_argument("infile", help="Serial Log File") 23 parser.add_argument("outfile", 24 help="Output file for use with coredump GDB server") 25 26 return parser.parse_args() 27 28 29def main(): 30 args = parse_args() 31 32 infile = open(args.infile, "r") 33 if not infile: 34 print(f"ERROR: Cannot open input file: {args.infile}, exiting...") 35 sys.exit(1) 36 37 outfile = open(args.outfile, "wb") 38 if not outfile: 39 print(f"ERROR: Cannot open output file for write: {args.outfile}, exiting...") 40 sys.exit(1) 41 42 print(f"Input file {args.infile}") 43 print(f"Output file {args.outfile}") 44 45 has_begin = False 46 has_end = False 47 has_error = False 48 go_parse_line = False 49 bytes_written = 0 50 for line in infile.readlines(): 51 if line.find(COREDUMP_BEGIN_STR) >= 0: 52 # Found "BEGIN#" - beginning of log 53 has_begin = True 54 go_parse_line = True 55 continue 56 57 if line.find(COREDUMP_END_STR) >= 0: 58 # Found "END#" - end of log 59 has_end = True 60 go_parse_line = False 61 break 62 63 if line.find(COREDUMP_ERROR_STR) >= 0: 64 # Error was encountered during dumping: 65 # log is not usable 66 has_error = True 67 go_parse_line = False 68 break 69 70 if not go_parse_line: 71 continue 72 73 prefix_idx = line.find(COREDUMP_PREFIX_STR) 74 75 if prefix_idx < 0: 76 continue 77 78 prefix_idx += len(COREDUMP_PREFIX_STR) 79 hex_str = line[prefix_idx:].strip() 80 81 binary_data = binascii.unhexlify(hex_str) 82 outfile.write(binary_data) 83 bytes_written += len(binary_data) 84 85 if not has_begin: 86 print("ERROR: Beginning of log not found!") 87 elif not has_end: 88 print("WARN: End of log not found! Is log complete?") 89 elif has_error: 90 print("ERROR: log has error.") 91 else: 92 print(f"Bytes written {bytes_written}") 93 94 infile.close() 95 outfile.close() 96 97 98if __name__ == "__main__": 99 main() 100