Add DEBUGPY_TRACE_DEBUGPY variable to allow debugpy to debug itself

This commit is contained in:
Rich Chiodo 2022-08-04 15:34:06 -07:00
parent 6b276e339c
commit 83ff28006d
7 changed files with 34 additions and 12 deletions

View file

@ -58,7 +58,7 @@ class LogFile(object):
platform.machine(),
platform.python_implementation(),
platform.python_version(),
64 if sys.maxsize > 2**32 else 32,
64 if sys.maxsize > 2 ** 32 else 32,
debugpy.__version__,
_to_files=[self],
)

View file

@ -21,6 +21,7 @@ import sys
import threading
from debugpy.common import json, log, util
from debugpy.common.util import skip_trace
class JsonIOError(IOError):
@ -1148,9 +1149,11 @@ class JsonMessageChannel(object):
self._parser_thread = threading.Thread(
target=self._parse_incoming_messages, name=f"{self} message parser"
)
self._parser_thread.pydev_do_not_trace = True
self._parser_thread.is_pydev_daemon_thread = True
self._parser_thread.daemon = True
if skip_trace():
self._parser_thread.pydev_do_not_trace = True
self._parser_thread.is_pydev_daemon_thread = True
self._parser_thread.daemon = True
self._parser_thread.start()
def wait(self):

View file

@ -7,6 +7,7 @@ import sys
import threading
from debugpy.common import log
from debugpy.common.util import skip_trace
def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None):
@ -114,9 +115,10 @@ def serve(name, handler, host, port=0, backlog=socket.SOMAXCONN, timeout=None):
handler(sock)
thread = threading.Thread(target=accept_worker)
thread.daemon = True
thread.pydev_do_not_trace = True
thread.is_pydev_daemon_thread = True
if skip_trace():
thread.daemon = True
thread.pydev_do_not_trace = True
thread.is_pydev_daemon_thread = True
thread.start()
return listener

View file

@ -148,3 +148,18 @@ def srcnameof(obj):
name += ")"
return name
def skip_trace():
"""Returns True if tracing should be skipped for the current thread."""
return "DEBUGPY_TRACE_DEBUGPY" not in os.environ
def disable_tracing(thread):
"""Disables tracing for the given thread if DEBUGPY_TRACE_DEBUGPY is not set.
DEBUGPY_TRACE_DEBUGPY is used to debug debugpy with debugpy
"""
if skip_trace():
thread.pydev_do_not_trace = True
thread.is_pydev_daemon_thread = True
thread.daemon = True

View file

@ -10,6 +10,7 @@ from debugpy import launcher
from debugpy.common import json
from debugpy.launcher import debuggee
def launch_request(request):
debug_options = set(request("debugOptions", json.array(str)))

View file

@ -14,7 +14,7 @@ from debugpy import adapter
from debugpy.common import json, log, sockets
from _pydevd_bundle.pydevd_constants import get_global_debugger
from pydevd_file_utils import absolute_path
from debugpy.common.util import skip_trace
_tls = threading.local()
@ -129,9 +129,10 @@ def _starts_debugging(func):
"patch_multiprocessing": _config.get("subProcess", True),
}
debugpy_path = os.path.dirname(absolute_path(debugpy.__file__))
settrace_kwargs["dont_trace_start_patterns"] = (debugpy_path,)
settrace_kwargs["dont_trace_end_patterns"] = (str("debugpy_launcher.py"),)
if skip_trace():
debugpy_path = os.path.dirname(absolute_path(debugpy.__file__))
settrace_kwargs["dont_trace_start_patterns"] = (debugpy_path,)
settrace_kwargs["dont_trace_end_patterns"] = (str("debugpy_launcher.py"),)
try:
return func(address, settrace_kwargs, **kwargs)

View file

@ -109,7 +109,7 @@ def set_address(mode):
port = int(port)
except Exception:
port = -1
if not (0 <= port < 2**16):
if not (0 <= port < 2 ** 16):
raise ValueError("invalid port number")
options.mode = mode