Skip to content

Commit 6c19aba

Browse files
authored
Merge pull request #1007 from rchiodo/rchiodo/allow_adapter_debugging
Add DEBUGPY_TRACE_DEBUGPY variable to allow debugpy to debug itself
2 parents 4f6638b + 10d8839 commit 6c19aba

File tree

6 files changed

+26
-10
lines changed

6 files changed

+26
-10
lines changed

src/debugpy/common/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, filename, file, levels=LEVELS, close_file=True):
5858
platform.machine(),
5959
platform.python_implementation(),
6060
platform.python_version(),
61-
64 if sys.maxsize > 2**32 else 32,
61+
64 if sys.maxsize > 2 ** 32 else 32,
6262
debugpy.__version__,
6363
_to_files=[self],
6464
)

src/debugpy/common/messaging.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import threading
2222

2323
from debugpy.common import json, log, util
24+
from debugpy.common.util import hide_thread_from_debugger
2425

2526

2627
class JsonIOError(IOError):
@@ -1148,8 +1149,8 @@ def start(self):
11481149
self._parser_thread = threading.Thread(
11491150
target=self._parse_incoming_messages, name=f"{self} message parser"
11501151
)
1151-
self._parser_thread.pydev_do_not_trace = True
1152-
self._parser_thread.is_pydev_daemon_thread = True
1152+
1153+
hide_thread_from_debugger(self._parser_thread)
11531154
self._parser_thread.daemon = True
11541155
self._parser_thread.start()
11551156

src/debugpy/common/sockets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import threading
88

99
from debugpy.common import log
10+
from debugpy.common.util import hide_thread_from_debugger
1011

1112

1213
def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None):
@@ -115,8 +116,7 @@ def accept_worker():
115116

116117
thread = threading.Thread(target=accept_worker)
117118
thread.daemon = True
118-
thread.pydev_do_not_trace = True
119-
thread.is_pydev_daemon_thread = True
119+
hide_thread_from_debugger(thread)
120120
thread.start()
121121

122122
return listener

src/debugpy/common/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,17 @@ def srcnameof(obj):
148148
name += ")"
149149

150150
return name
151+
152+
153+
def hide_debugpy_internals():
154+
"""Returns True if the caller should hide something from debugpy."""
155+
return "DEBUGPY_TRACE_DEBUGPY" not in os.environ
156+
157+
158+
def hide_thread_from_debugger(thread):
159+
"""Disables tracing for the given thread if DEBUGPY_TRACE_DEBUGPY is not set.
160+
DEBUGPY_TRACE_DEBUGPY is used to debug debugpy with debugpy
161+
"""
162+
if hide_debugpy_internals():
163+
thread.pydev_do_not_trace = True
164+
thread.is_pydev_daemon_thread = True

src/debugpy/server/api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from debugpy.common import json, log, sockets
1515
from _pydevd_bundle.pydevd_constants import get_global_debugger
1616
from pydevd_file_utils import absolute_path
17-
17+
from debugpy.common.util import hide_debugpy_internals
1818

1919
_tls = threading.local()
2020

@@ -129,9 +129,10 @@ def debug(address, **kwargs):
129129
"patch_multiprocessing": _config.get("subProcess", True),
130130
}
131131

132-
debugpy_path = os.path.dirname(absolute_path(debugpy.__file__))
133-
settrace_kwargs["dont_trace_start_patterns"] = (debugpy_path,)
134-
settrace_kwargs["dont_trace_end_patterns"] = (str("debugpy_launcher.py"),)
132+
if hide_debugpy_internals():
133+
debugpy_path = os.path.dirname(absolute_path(debugpy.__file__))
134+
settrace_kwargs["dont_trace_start_patterns"] = (debugpy_path,)
135+
settrace_kwargs["dont_trace_end_patterns"] = (str("debugpy_launcher.py"),)
135136

136137
try:
137138
return func(address, settrace_kwargs, **kwargs)

src/debugpy/server/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def do(arg, it):
109109
port = int(port)
110110
except Exception:
111111
port = -1
112-
if not (0 <= port < 2**16):
112+
if not (0 <= port < 2 ** 16):
113113
raise ValueError("invalid port number")
114114

115115
options.mode = mode

0 commit comments

Comments
 (0)