Skip to content

Commit 83ff280

Browse files
committed
Add DEBUGPY_TRACE_DEBUGPY variable to allow debugpy to debug itself
1 parent 6b276e3 commit 83ff280

7 files changed

Lines changed: 34 additions & 12 deletions

File tree

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: 6 additions & 3 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 skip_trace
2425

2526

2627
class JsonIOError(IOError):
@@ -1148,9 +1149,11 @@ 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
1153-
self._parser_thread.daemon = True
1152+
1153+
if skip_trace():
1154+
self._parser_thread.pydev_do_not_trace = True
1155+
self._parser_thread.is_pydev_daemon_thread = True
1156+
self._parser_thread.daemon = True
11541157
self._parser_thread.start()
11551158

11561159
def wait(self):

src/debugpy/common/sockets.py

Lines changed: 5 additions & 3 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 skip_trace
1011

1112

1213
def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None):
@@ -114,9 +115,10 @@ def accept_worker():
114115
handler(sock)
115116

116117
thread = threading.Thread(target=accept_worker)
117-
thread.daemon = True
118-
thread.pydev_do_not_trace = True
119-
thread.is_pydev_daemon_thread = True
118+
if skip_trace():
119+
thread.daemon = True
120+
thread.pydev_do_not_trace = True
121+
thread.is_pydev_daemon_thread = True
120122
thread.start()
121123

122124
return listener

src/debugpy/common/util.py

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

150150
return name
151+
152+
153+
def skip_trace():
154+
"""Returns True if tracing should be skipped for the current thread."""
155+
return "DEBUGPY_TRACE_DEBUGPY" not in os.environ
156+
157+
158+
def disable_tracing(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 skip_trace():
163+
thread.pydev_do_not_trace = True
164+
thread.is_pydev_daemon_thread = True
165+
thread.daemon = True

src/debugpy/launcher/handlers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from debugpy.common import json
1111
from debugpy.launcher import debuggee
1212

13+
1314
def launch_request(request):
1415
debug_options = set(request("debugOptions", json.array(str)))
1516

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 skip_trace
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 skip_trace():
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)