Created
August 31, 2017 21:44
-
-
Save bhyde/be6c4f75fc7f015fab78ae084ef371f0 to your computer and use it in GitHub Desktop.
pyrasite script to dump thread tracebacks, merging those with identical tracebacks
This file contains hidden or 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
# pyrasite script to dump all thread tracebacks, grouping | |
# together threads with identical tracebacks. | |
# pyrasite <pid> dump_tracebacks.py | |
# The report is appended to /tmp/report.txt | |
import os | |
import traceback | |
import sys | |
import threading | |
id2thread = {} | |
for thread in threading.enumerate(): | |
id2thread[thread.ident] = thread | |
traces = {} | |
for thread_id, frame in sys._current_frames().items(): | |
t = id2thread.get(thread_id, None) | |
thread_name = t.name if t is not None else "UNKNOWN" | |
thread_description = "Thread: %s (%d)" % (thread_name, thread_id) | |
stack_trace = "".join(traceback.format_list(traceback.extract_stack(frame))) | |
if stack_trace in traces: | |
traces[stack_trace].append(thread_description) | |
else: | |
traces[stack_trace] = [thread_description] | |
with open("/tmp/report.txt", 'a') as s: | |
def p(x): | |
s.write(x) | |
s.write("\n") | |
return None | |
p("Begin stack dump of process %d:" % os.getpid()) | |
p("Note: Threads with identical tracebacks are grouped together.") | |
for trace, thread_descriptions in traces.iteritems(): | |
p("") | |
map(p, thread_descriptions) | |
p("") | |
p(trace) | |
p("") | |
p("End stack dump of process %d" % os.getpid()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment