Merge pull request #1007 from rchiodo/rchiodo/allow_adapter_debugging

Add DEBUGPY_TRACE_DEBUGPY variable to allow debugpy to debug itself
This commit is contained in:
Rich Chiodo 2022-08-04 16:52:02 -07:00 committed by GitHub
commit 6c19aba462
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 10 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 hide_thread_from_debugger
class JsonIOError(IOError):
@ -1148,8 +1149,8 @@ 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
hide_thread_from_debugger(self._parser_thread)
self._parser_thread.daemon = True
self._parser_thread.start()

View file

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

View file

@ -148,3 +148,17 @@ def srcnameof(obj):
name += ")"
return name
def hide_debugpy_internals():
"""Returns True if the caller should hide something from debugpy."""
return "DEBUGPY_TRACE_DEBUGPY" not in os.environ
def hide_thread_from_debugger(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 hide_debugpy_internals():
thread.pydev_do_not_trace = True
thread.is_pydev_daemon_thread = True

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 hide_debugpy_internals
_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 hide_debugpy_internals():
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