From 7b86560dedaad88baaab87bd5c1be7719ec1810e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 11 Jun 2016 23:07:42 +1200 Subject: debug: On SIGUSR2, we dump tracebacks for all threads to screen --- mitmproxy/main.py | 2 +- netlib/debug.py | 16 +++++++++++++++- test/netlib/test_debug.py | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mitmproxy/main.py b/mitmproxy/main.py index 53417fe8..bf01a3cb 100644 --- a/mitmproxy/main.py +++ b/mitmproxy/main.py @@ -47,7 +47,7 @@ def process_options(parser, options): sys.exit(0) if options.quiet: options.verbose = 0 - debug.register_info_dumper() + debug.register_info_dumpers() return config.process_proxy_options(parser, options) diff --git a/netlib/debug.py b/netlib/debug.py index 6f6b0460..792ff52e 100644 --- a/netlib/debug.py +++ b/netlib/debug.py @@ -4,6 +4,7 @@ import sys import threading import signal import platform +import traceback import psutil @@ -76,5 +77,18 @@ def dump_info(sig, frm, file=sys.stdout): # pragma: no cover print("****************************************************", file=file) -def register_info_dumper(): # pragma: no cover +def dump_stacks(signal, frame, file=sys.stdout): + id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) + code = [] + for threadId, stack in sys._current_frames().items(): + code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId)) + for filename, lineno, name, line in traceback.extract_stack(stack): + code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) + if line: + code.append(" %s" % (line.strip())) + print("\n".join(code), file=file) + + +def register_info_dumpers(): # pragma: no cover signal.signal(signal.SIGUSR1, dump_info) + signal.signal(signal.SIGUSR2, dump_stacks) diff --git a/test/netlib/test_debug.py b/test/netlib/test_debug.py index c39d3752..b9315c7f 100644 --- a/test/netlib/test_debug.py +++ b/test/netlib/test_debug.py @@ -10,5 +10,11 @@ def test_dump_info(): assert cs.getvalue() +def test_dump_stacks(): + cs = StringIO() + debug.dump_stacks(None, None, file=cs) + assert cs.getvalue() + + def test_sysinfo(): assert debug.sysinfo() -- cgit v1.2.3 From e9f8eb1f6bf1a30bba304769507c1eb6ef64c3cf Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 11 Jun 2016 23:13:41 +1200 Subject: Satisfy lint --- netlib/debug.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/netlib/debug.py b/netlib/debug.py index 792ff52e..303a2f6f 100644 --- a/netlib/debug.py +++ b/netlib/debug.py @@ -81,7 +81,11 @@ def dump_stacks(signal, frame, file=sys.stdout): id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) code = [] for threadId, stack in sys._current_frames().items(): - code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""), threadId)) + code.append( + "\n# Thread: %s(%d)" % ( + id2name.get(threadId, ""), threadId + ) + ) for filename, lineno, name, line in traceback.extract_stack(stack): code.append('File: "%s", line %d, in %s' % (filename, lineno, name)) if line: -- cgit v1.2.3