From 3318e531a60530ad0aaeb0ddc5804ad174987a14 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 4 Oct 2016 16:22:01 +1100 Subject: [PATCH 1/2] debug: add a dump of objects in memory --- netlib/debug.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/netlib/debug.py b/netlib/debug.py index f9c700de1..7a7d3479f 100644 --- a/netlib/debug.py +++ b/netlib/debug.py @@ -1,5 +1,6 @@ from __future__ import (absolute_import, print_function, division) +import gc import os import sys import threading @@ -78,6 +79,19 @@ def dump_info(signal=None, frame=None, file=sys.stdout): # pragma: no cover for i in bthreads: print(i._threadinfo(), file=file) + print() + print("Memory", file=file) + print("=======", file=file) + gc.collect() + d = {} + for i in gc.get_objects(): + t = str(type(i)) + if "mitmproxy" in t or "netlib" in t: + d[t] = d.setdefault(t, 0) + 1 + itms = list(d.items()) + itms.sort(key=lambda x: x[1]) + for i in itms[-20:]: + print(i[1], i[0]) print("****************************************************", file=file) From 882c363ec57792b00ebf1f731ec5c5e4bd24d58d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 4 Oct 2016 16:48:13 +1100 Subject: [PATCH 2/2] debug: Hard exit after debug signal --- netlib/debug.py | 9 +++++++-- test/netlib/test_debug.py | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/netlib/debug.py b/netlib/debug.py index 7a7d3479f..657d44657 100644 --- a/netlib/debug.py +++ b/netlib/debug.py @@ -38,7 +38,7 @@ def sysinfo(): return "\n".join(data) -def dump_info(signal=None, frame=None, file=sys.stdout): # pragma: no cover +def dump_info(signal=None, frame=None, file=sys.stdout, testing=False): # pragma: no cover print("****************************************************", file=file) print("Summary", file=file) print("=======", file=file) @@ -94,8 +94,11 @@ def dump_info(signal=None, frame=None, file=sys.stdout): # pragma: no cover print(i[1], i[0]) print("****************************************************", file=file) + if not testing: + sys.exit(1) -def dump_stacks(signal=None, frame=None, file=sys.stdout): + +def dump_stacks(signal=None, frame=None, file=sys.stdout, testing=False): id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) code = [] for threadId, stack in sys._current_frames().items(): @@ -109,6 +112,8 @@ def dump_stacks(signal=None, frame=None, file=sys.stdout): if line: code.append(" %s" % (line.strip())) print("\n".join(code), file=file) + if not testing: + sys.exit(1) def register_info_dumpers(): diff --git a/test/netlib/test_debug.py b/test/netlib/test_debug.py index 51710da0d..9ceaace98 100644 --- a/test/netlib/test_debug.py +++ b/test/netlib/test_debug.py @@ -6,13 +6,13 @@ from netlib import debug def test_dump_info(): cs = StringIO() - debug.dump_info(None, None, file=cs) + debug.dump_info(None, None, file=cs, testing=True) assert cs.getvalue() def test_dump_stacks(): cs = StringIO() - debug.dump_stacks(None, None, file=cs) + debug.dump_stacks(None, None, file=cs, testing=True) assert cs.getvalue()