Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
More test fixes (#1753)
Browse files Browse the repository at this point in the history
* Migrate tracing feature and update tracing tests

* Other minor fixes

* fix resume on stepping
  • Loading branch information
karthiknadig authored Sep 7, 2019
1 parent 7623560 commit 3061599
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
21 changes: 21 additions & 0 deletions src/ptvsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ def debug_this_thread():
return api.debug_this_thread()


def tracing(should_trace=None):
"""Enables or disables tracing on this thread. When called without an
argument, returns the current tracing state.
When tracing is disabled, breakpoints will not be hit, but code executes
significantly faster.
If debugger is not attached, this function has no effect.
This function can also be used in a with-statement to automatically save
and then restore the previous tracing setting::
with ptvsd.tracing(False):
# Tracing disabled
...
# Tracing restored
Parameters
----------
should_trace : bool, optional
Whether to enable or disable tracing.
"""
from ptvsd.server import api
return api.tracing(should_trace)


__version__ = _version.get_versions()["version"]

# Force absolute path on Python 2.
Expand Down
58 changes: 57 additions & 1 deletion src/ptvsd/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

from __future__ import absolute_import, print_function, unicode_literals

import contextlib
import os
import sys
import pydevd
import sys
import threading

import ptvsd
Expand Down Expand Up @@ -121,3 +122,58 @@ def break_into_debugger():
def debug_this_thread():
log.info("debug_this_thread()")
pydevd.settrace(suspend=False)


_tls = threading.local()


def tracing(should_trace):
pydb = get_global_debugger()

try:
was_tracing = _tls.is_tracing
except AttributeError:
was_tracing = pydb is not None

if should_trace is None:
return was_tracing

# It is possible that IDE attaches after tracing is changed, but before it is
# restored. In this case, we don't really want to restore the original value,
# because it will effectively disable tracing for the just-attached IDE. Doing
# the check outside the function below makes it so that if the original change
# was a no-op because IDE wasn't attached, restore will be no-op as well, even
# if IDE has attached by then.

tid = threading.current_thread().ident
if pydb is None:
log.info("ptvsd.tracing() ignored on thread {0} - debugger not attached", tid)
def enable_or_disable(_):
# Always fetch the fresh value, in case it changes before we restore.
_tls.is_tracing = get_global_debugger() is not None
else:
def enable_or_disable(enable):
if enable:
log.info("Enabling tracing on thread {0}", tid)
pydb.enable_tracing()
else:
log.info("Disabling tracing on thread {0}", tid)
pydb.disable_tracing()
_tls.is_tracing = enable

# Context managers don't do anything unless used in a with-statement - that is,
# even the code up to yield won't run. But we want callers to be able to omit
# with-statement for this function, if they don't want to restore. So, we apply
# the change directly out here in the non-generator context, so that it happens
# immediately - and then return a context manager that is solely for the purpose
# of restoring the original value, which the caller can use or discard.

@contextlib.contextmanager
def restore_tracing():
try:
yield
finally:
enable_or_disable(was_tracing)

enable_or_disable(should_trace)
return restore_tracing()
2 changes: 1 addition & 1 deletion tests/ptvsd/server/test_breakpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def code_to_debug():
assert breakpoints == [{
'verified': False,
'message': 'Breakpoint in file that does not exist.',
'source': some.dict_with({
'source': some.dict.containing({
'path': some.path('invalid_file.py')
}),
'line': 1
Expand Down
2 changes: 1 addition & 1 deletion tests/ptvsd/server/test_system_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def version_str(v):
except AttributeError:
impl_version = ''

return some.dict_with({
return some.dict.containing({
'ptvsd': {
'version': ptvsd.__version__,
},
Expand Down
24 changes: 12 additions & 12 deletions tests/ptvsd/server/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,33 @@ def inner2():
session.set_breakpoints(code_to_debug, all)
session.start_debugging()

stop = session.wait_for_thread_stopped()
frame = stop.stacktrace.body['stackFrames'][0]
assert frame == some.dict_with({
stop = session.wait_for_stop()
frame = stop.frames[0]
assert frame == some.dict.containing({
"line": code_to_debug.lines["inner2"],
})

session.request_continue()

stop = session.wait_for_thread_stopped()
frame = stop.stacktrace.body['stackFrames'][0]
assert frame == some.dict_with({
stop = session.wait_for_stop()
frame = stop.frames[0]
assert frame == some.dict.containing({
"line": code_to_debug.lines["outer2"],
})

session.request_continue()

stop = session.wait_for_thread_stopped()
frame = stop.stacktrace.body['stackFrames'][0]
assert frame == some.dict_with({
stop = session.wait_for_stop()
frame = stop.frames[0]
assert frame == some.dict.containing({
"line": code_to_debug.lines["inner1"],
})

session.request_continue()

stop = session.wait_for_thread_stopped()
frame = stop.stacktrace.body['stackFrames'][0]
assert frame == some.dict_with({
stop = session.wait_for_stop()
frame = stop.frames[0]
assert frame == some.dict.containing({
"line": code_to_debug.lines["inner3"],
})

Expand Down
2 changes: 1 addition & 1 deletion tests/start_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _build_common_args(
if maxExceptionStackFrames:
args["maxExceptionStackFrames"] = maxExceptionStackFrames

if steppingResumesAllThreads:
if steppingResumesAllThreads is not None:
args["steppingResumesAllThreads"] = steppingResumesAllThreads

if rules is not None:
Expand Down

0 comments on commit 3061599

Please sign in to comment.