From 04dd1936a74bc8897372d949602c2324fad59373 Mon Sep 17 00:00:00 2001 From: abejgonzalez Date: Thu, 5 Dec 2024 09:51:37 -0800 Subject: [PATCH] Add function trace utils --- scripts/func-trace-utils/Makefile | 29 ++++++++++ .../func-trace-utils/create-func-trace-int.py | 58 +++++++++++++++++++ scripts/func-trace-utils/create-func-trace.py | 57 ++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 scripts/func-trace-utils/Makefile create mode 100755 scripts/func-trace-utils/create-func-trace-int.py create mode 100755 scripts/func-trace-utils/create-func-trace.py diff --git a/scripts/func-trace-utils/Makefile b/scripts/func-trace-utils/Makefile new file mode 100644 index 0000000000..08d46d0578 --- /dev/null +++ b/scripts/func-trace-utils/Makefile @@ -0,0 +1,29 @@ +default: bin.trace bin.trace.interleaved + +vmlinux.dump: boards/default/linux/vmlinux + riscv64-unknown-elf-objdump -D $< > $@ + +busybox.dump: wlutil/busybox/busybox_unstripped + riscv64-unknown-linux-gnu-objdump -D -S $< > $@ + +# userspace binary +USERSPACE_BIN ?= +bin.dump: $(USERSPACE_BIN) + riscv64-unknown-elf-objdump -D -S $< > $@ + +# logfile from spike or CY RTL sim. +IN_LOG ?= +bin.trace: vmlinux.dump busybox.dump bin.dump $(IN_LOG) + ./create-func-trace.py $^ + mv $(IN_LOG).functrace $@ + +bin.trace.interleaved: vmlinux.dump busybox.dump bin.dump $(IN_LOG) + ./create-func-trace-interleaved.py $^ + mv $(IN_LOG).functrace $@ + +.PHONY: dumps +dumps: busybox.dump bin.dump vmlinux.dump + +.PHONY: clean +clean: + rm -rf *.trace* diff --git a/scripts/func-trace-utils/create-func-trace-int.py b/scripts/func-trace-utils/create-func-trace-int.py new file mode 100755 index 0000000000..f561d9d730 --- /dev/null +++ b/scripts/func-trace-utils/create-func-trace-int.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import sys +import re + +back_args = sys.argv[1:] +files_to_check = back_args[:-1] +outfile = back_args[-1] + +print(f"Checking {files_to_check}") + +func_names = {} + +for dumpfile in files_to_check: + with open(dumpfile, 'r') as ld: + for l in ld: + match = re.match(r"^([a-fA-F0-9]+) <(.*)>:", l) + if match: + pc, name = match.groups() + if pc in func_names: + func_names[pc] = func_names[pc] + " or " + name + else: + func_names[pc] = name + +print(f"Got {len(func_names)} functions from files") +print(f"Creating function trace from {outfile}") + +with open(outfile, 'r') as of: + with open(outfile + ".functrace", 'w') as wf: + lines = of.readlines() + tlc = len(lines) + lno = 0 + for l in lines: + match = re.search(r"\[1\] pc=\[([a-fA-F0-9]+)\]", l) + if match: + pc = match.groups()[0] + if pc in func_names: + wf.write(f"{pc}: {func_names[pc]}\n") + #wf.write(f"{(100*lno/tlc):.1f}: {pc}: {func_names[pc]}\n") + else: + match = re.search(r": 0x([a-fA-F0-9]+) ", l) + if match: + pc = match.groups()[0] + if pc in func_names: + wf.write(f"{pc}: {func_names[pc]}\n") + #wf.write(f"{(100*lno/tlc):.1f}: {pc}: {func_names[pc]}\n") + # match = re.search(r": exception.*,", l) + # if match: + # wf.write(l) + + # match = re.search(r"\[0\] pc=\[0000000000193f80\]", l) + # if match: + # wf.write(l) + + wf.write(l) + lno = lno + 1 + +print(f"Done: {outfile}.functrace") diff --git a/scripts/func-trace-utils/create-func-trace.py b/scripts/func-trace-utils/create-func-trace.py new file mode 100755 index 0000000000..1fdac2649e --- /dev/null +++ b/scripts/func-trace-utils/create-func-trace.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import sys +import re + +back_args = sys.argv[1:] +files_to_check = back_args[:-1] +outfile = back_args[-1] + +print(f"Checking {files_to_check}") + +func_names = {} + +for dumpfile in files_to_check: + with open(dumpfile, 'r') as ld: + for l in ld: + match = re.match(r"^([a-fA-F0-9]+) <(.*)>:", l) + if match: + pc, name = match.groups() + if pc in func_names: + func_names[pc] = func_names[pc] + " or " + name + else: + func_names[pc] = name + +print(f"Got {len(func_names)} functions from files") +print(f"Creating function trace from {outfile}") + +with open(outfile, 'r') as of: + with open(outfile + ".functrace", 'w') as wf: + lines = of.readlines() + tlc = len(lines) + lno = 0 + for l in lines: + match = re.search(r"\[1\] pc=\[([a-fA-F0-9]+)\]", l) + if match: + pc = match.groups()[0] + if pc in func_names: + wf.write(f"{pc}: {func_names[pc]}\n") + #wf.write(f"{(100*lno/tlc):.1f}: {pc}: {func_names[pc]}\n") + else: + match = re.search(r": 0x([a-fA-F0-9]+) ", l) + if match: + pc = match.groups()[0] + if pc in func_names: + wf.write(f"{pc}: {func_names[pc]}\n") + #wf.write(f"{(100*lno/tlc):.1f}: {pc}: {func_names[pc]}\n") + #wf.write(l) + match = re.search(r": exception.*,", l) + if match: + wf.write(l) + + match = re.search(r"\[0\] pc=\[0000000000193f80\]", l) + if match: + wf.write(l) + lno = lno + 1 + +print(f"Done: {outfile}.functrace")