Created
October 25, 2022 23:30
-
-
Save sylv-io/4c1151696e7b8f3edf124d29238ef018 to your computer and use it in GitHub Desktop.
Convert EDK2 OVMF console log to a GDB script that adds symbol information (add-symbol-file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import sys | |
import os | |
import pefile | |
build_dir='Build/OvmfX64/DEBUG_GCC5/X64' | |
def parse_flags()->argparse.Namespace: | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--input', default='debug.log', help='EDK2 input logfile') | |
parser.add_argument('-o', '--output', default='gdbscript', help='gdbscript output file') | |
return parser.parse_args() | |
def parse_symbol_file(b: list[bytes])->str: | |
ba = int(b[3], base=16) | |
efi = b[5] | |
syms = efi.replace(b'.efi', b'.debug').decode() | |
d = None | |
for (root, _, files) in os.walk(build_dir): | |
if syms in files: | |
d = os.path.dirname(os.path.join(root, syms)) | |
break | |
if not d: | |
raise FileNotFoundError(syms) | |
pe = pefile.PE(os.path.join(d, efi.decode())) | |
va = pe.sections[0].VirtualAddress | |
addr = ba + va | |
return('add-symbol-file {} 0x{:x}'.format(os.path.join(d, syms), addr)) | |
def main()->int: | |
args = parse_flags() | |
try: | |
f = open(args.input, 'rb') | |
except FileNotFoundError: | |
print('logfile "{}" not found'.format(args.logfile)) | |
return 1 | |
lines = [] | |
for lf in f.read().split(b'\n'): | |
lf = lf.strip() | |
if b'Loading' in lf and lf.endswith(b'.efi'): | |
p = lf.split(b' ') | |
lines.append(parse_symbol_file(p)) | |
# write gdb script | |
with open(args.output, 'w') as out: | |
out.write('\n'.join(lines) + '\n') | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment