Find out which line caused the crash or exit in Python

Published on

import sys

def trace(frame, event, arg):
  with open("trace.log", "a") as f:
    f.write("%s, %s:%d\n" % (event, frame.f_code.co_filename, frame.f_lineno))
  return trace
  
sys.settrace(trace)

If there is a sigfault or an exit but you don’t know where, just print every executed line, then tail -n 100 the trace.log