Skip to content

Instantly share code, notes, and snippets.

@SamTebbs33
Last active June 25, 2019 21:15
Show Gist options
  • Select an option

  • Save SamTebbs33/38cb0c48609beec96903077f288897f0 to your computer and use it in GitHub Desktop.

Select an option

Save SamTebbs33/38cb0c48609beec96903077f288897f0 to your computer and use it in GitHub Desktop.
This file shows the execution trail of a gdb backtrace, as well as the relevant lines in the source file. Run with python 3.
import sys
import re
# Usage: python3 trace.py <path to gdb backtrace> <function to start analysing from>
# The function to start analysing from can be any function name that appears in the backtrace, such as "assert" or "std.special.panic"
trace_path = sys.argv[1]
start_func = sys.argv[2]
regex = r"#\d+\s+0x[a-f0-9]+ in ([a-zA-Z0-9_.(,)]+) \(([a-zA-Z0-9_]+=.+)*\) at ([\/a-zA-Z0-9_\.]+):(\d+)"
def parse(line):
matches = re.findall(regex, line)[0]
return matches
with open(trace_path, "r") as trace_file:
printing = False
for trace_line in trace_file:
# Parse the line to function name, arguments list, file and line
(func, args, f, trace_line) = parse(trace_line)
trace_line = int(trace_line)
# If we have started printing or the function matches the one to start with
if printing or func == start_func or start_func == "":
printing = True
# Print the function name, arguments, file and line
print("\n##### %s(%s) in %s:%s" %(func, args, f, trace_line))
# Print the surrounding lines from the file
with open(f, "r") as func_file:
for i, line in enumerate(func_file):
# i starts at 0 but trace_line starts at 1, so subtract 1
if i == trace_line - 1:
print("--> %s" %(line), end='')
elif i >= trace_line - 4 and i <= trace_line + 2:
print(" %s" %(line), end='')
elif i > trace_line + 2:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment