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