mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
debug: dump asyncio tasks
This commit is contained in:
parent
9b7dfb0fc9
commit
9b0c4e2338
@ -1,14 +1,17 @@
|
|||||||
|
import asyncio
|
||||||
import gc
|
import gc
|
||||||
|
import linecache
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
|
from contextlib import redirect_stdout
|
||||||
|
|
||||||
from OpenSSL import SSL
|
from OpenSSL import SSL
|
||||||
|
|
||||||
from mitmproxy import version
|
from mitmproxy import version
|
||||||
|
from mitmproxy.utils import asyncio_utils
|
||||||
|
|
||||||
|
|
||||||
def dump_system_info():
|
def dump_system_info():
|
||||||
@ -24,49 +27,50 @@ def dump_system_info():
|
|||||||
|
|
||||||
|
|
||||||
def dump_info(signal=None, frame=None, file=sys.stdout, testing=False): # pragma: no cover
|
def dump_info(signal=None, frame=None, file=sys.stdout, testing=False): # pragma: no cover
|
||||||
print("****************************************************", file=file)
|
with redirect_stdout(file):
|
||||||
print("Summary", file=file)
|
print("****************************************************")
|
||||||
print("=======", file=file)
|
print("Summary")
|
||||||
|
print("=======")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import psutil
|
import psutil
|
||||||
except:
|
except:
|
||||||
print("(psutil not installed, skipping some debug info)", file=file)
|
print("(psutil not installed, skipping some debug info)")
|
||||||
else:
|
else:
|
||||||
p = psutil.Process()
|
p = psutil.Process()
|
||||||
print("num threads: ", p.num_threads(), file=file)
|
print("num threads: ", p.num_threads())
|
||||||
if hasattr(p, "num_fds"):
|
if hasattr(p, "num_fds"):
|
||||||
print("num fds: ", p.num_fds(), file=file)
|
print("num fds: ", p.num_fds())
|
||||||
print("memory: ", p.memory_info(), file=file)
|
print("memory: ", p.memory_info())
|
||||||
|
|
||||||
print(file=file)
|
print()
|
||||||
print("Files", file=file)
|
print("Files")
|
||||||
print("=====", file=file)
|
print("=====")
|
||||||
for i in p.open_files():
|
for i in p.open_files():
|
||||||
print(i, file=file)
|
print(i)
|
||||||
|
|
||||||
print(file=file)
|
print()
|
||||||
print("Connections", file=file)
|
print("Connections")
|
||||||
print("===========", file=file)
|
print("===========")
|
||||||
for i in p.connections():
|
for i in p.connections():
|
||||||
print(i, file=file)
|
print(i)
|
||||||
|
|
||||||
print(file=file)
|
print()
|
||||||
print("Threads", file=file)
|
print("Threads")
|
||||||
print("=======", file=file)
|
print("=======")
|
||||||
bthreads = []
|
bthreads = []
|
||||||
for i in threading.enumerate():
|
for i in threading.enumerate():
|
||||||
if hasattr(i, "_threadinfo"):
|
if hasattr(i, "_threadinfo"):
|
||||||
bthreads.append(i)
|
bthreads.append(i)
|
||||||
else:
|
else:
|
||||||
print(i.name, file=file)
|
print(i.name)
|
||||||
bthreads.sort(key=lambda x: x._thread_started)
|
bthreads.sort(key=lambda x: x._thread_started)
|
||||||
for i in bthreads:
|
for i in bthreads:
|
||||||
print(i._threadinfo(), file=file)
|
print(i._threadinfo())
|
||||||
|
|
||||||
print(file=file)
|
print()
|
||||||
print("Memory", file=file)
|
print("Memory")
|
||||||
print("=======", file=file)
|
print("=======")
|
||||||
gc.collect()
|
gc.collect()
|
||||||
d = {}
|
d = {}
|
||||||
for i in gc.get_objects():
|
for i in gc.get_objects():
|
||||||
@ -76,8 +80,26 @@ def dump_info(signal=None, frame=None, file=sys.stdout, testing=False): # pragm
|
|||||||
itms = list(d.items())
|
itms = list(d.items())
|
||||||
itms.sort(key=lambda x: x[1])
|
itms.sort(key=lambda x: x[1])
|
||||||
for i in itms[-20:]:
|
for i in itms[-20:]:
|
||||||
print(i[1], i[0], file=file)
|
print(i[1], i[0])
|
||||||
print("****************************************************", file=file)
|
|
||||||
|
try:
|
||||||
|
if sys.version_info < (3, 8):
|
||||||
|
raise RuntimeError
|
||||||
|
asyncio.get_running_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
print()
|
||||||
|
print("Tasks")
|
||||||
|
print("=======")
|
||||||
|
for task in asyncio.all_tasks():
|
||||||
|
f = task.get_stack(limit=1)[0]
|
||||||
|
line = linecache.getline(f.f_code.co_filename, f.f_lineno, f.f_globals).strip()
|
||||||
|
line = f"{line} # at {os.path.basename(f.f_code.co_filename)}:{f.f_lineno}"
|
||||||
|
print(f"{asyncio_utils.task_repr(task)}\n"
|
||||||
|
f" {line}")
|
||||||
|
|
||||||
|
print("****************************************************")
|
||||||
|
|
||||||
if not testing:
|
if not testing:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -17,6 +17,15 @@ def test_dump_info():
|
|||||||
cs = io.StringIO()
|
cs = io.StringIO()
|
||||||
debug.dump_info(None, None, file=cs, testing=True)
|
debug.dump_info(None, None, file=cs, testing=True)
|
||||||
assert cs.getvalue()
|
assert cs.getvalue()
|
||||||
|
assert "Tasks" not in cs.getvalue()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_dump_info_async():
|
||||||
|
cs = io.StringIO()
|
||||||
|
debug.dump_info(None, None, file=cs, testing=True)
|
||||||
|
if sys.version_info >= (3,8):
|
||||||
|
assert "Tasks" in cs.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def test_dump_stacks():
|
def test_dump_stacks():
|
||||||
|
Loading…
Reference in New Issue
Block a user