stacktrace.py 779 B

1234567891011121314151617181920212223242526
  1. """
  2. Copyright 2013 Eygene A. Ryabinkin
  3. Functions to perform stack tracing (for multithreaded programs
  4. as well as for single-threaded ones).
  5. """
  6. import sys
  7. import threading
  8. import traceback
  9. def dump(out):
  10. """ Dumps current stack trace into I/O object 'out' """
  11. id2name = {}
  12. for th_en in threading.enumerate():
  13. id2name[th_en.ident] = th_en.name
  14. count = 0
  15. for i, stack in list(sys._current_frames().items()):
  16. out.write("\n# Thread #%d (id=%d), %s\n" % (count, i, id2name[i]))
  17. count = count + 1
  18. for file, lno, name, line in traceback.extract_stack(stack):
  19. out.write('File: "%s", line %d, in %s' % (file, lno, name))
  20. if line:
  21. out.write(" %s" % (line.strip()))
  22. out.write("\n")