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

Commit

Permalink
Refactor tests.debug to accommodate ptvsd.server spawning the adapter…
Browse files Browse the repository at this point in the history
…, and remove the need for "custom_client" and "custom_server" start methods.

Fix launcher not propagating debuggee exit code.

Fix attach-by-PID without explicit --log-dir overriding PTVSD_LOG_DIR (and disabling logging).

Improve test logging, with a separate directory for every test.

Various test fixes.
  • Loading branch information
int19h committed Sep 26, 2019
1 parent 346ebd4 commit 8f358d6
Show file tree
Hide file tree
Showing 45 changed files with 1,879 additions and 2,819 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"console": "integratedTerminal",
"consoleTitle": "ptvsd.adapter",
"program": "${workspaceFolder}/src/ptvsd/adapter",
"args": ["--port", "8765", "--cls"],
"args": ["--port", "8765", "--log-stderr"],
"customDebugger": true,
},

Expand Down
2 changes: 1 addition & 1 deletion clean.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pushd %~dp0
del /s /q *.pyc
del /s /q *.pyo
for /d /r %%i in (__pycache__.*) do rd %%i
for /d /r %%i in (__pycache__.*) do rd "%%i"
popd

pushd %~dp0\src
Expand Down
18 changes: 3 additions & 15 deletions src/ptvsd/adapter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,24 @@ def main(args):
from ptvsd.common import log, options as common_options
from ptvsd.adapter import session, options as adapter_options

if args.cls:
sys.stderr.write("\033c")
if args.log_stderr:
log.stderr_levels |= set(log.LEVELS)
adapter_options.log_stderr = True
if args.log_dir is not None:
common_options.log_dir = args.log_dir

log.filename_prefix = "ptvsd.adapter"
log.stderr_levels |= {"info"}
log.to_file()
log.to_file(prefix="ptvsd.adapter")
log.describe_environment("ptvsd.adapter startup environment:")

session = session.Session()
if args.port is None:
session.connect_to_ide()
else:
# If in debugServer mode, log everything to stderr.
log.stderr_levels |= set(log.LEVELS)

if args.for_server_on_port is not None:
session.connect_to_server(("127.0.0.1", args.for_server_on_port))
with session.accept_connection_from_ide((args.host, args.port)) as (_, port):
try:
if session.server:
session.server.set_debugger_property({"adapterPort": port})
except AttributeError:
pass
session.wait_for_completion()


Expand Down Expand Up @@ -76,10 +68,6 @@ def _parse_argv(argv):
help=argparse.SUPPRESS
)

parser.add_argument(
"--cls", action="store_true", help="clear screen before starting the debuggee"
)

parser.add_argument(
"--log-dir",
type=str,
Expand Down
18 changes: 12 additions & 6 deletions src/ptvsd/adapter/ide.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def launch_request(self, request):
args = ["-c"] + request(
"code", json.array(unicode, vectorize=True, size=(1,))
)
else:
args = []
args += request("args", json.array(unicode))

console = request(
Expand All @@ -232,12 +234,13 @@ def attach_request(self, request):
if self.session.no_debug:
raise request.isnt_valid('"noDebug" is not supported for "attach"')



pid = request("processId", int, optional=True)
if pid == ():
if self.server is not None:
# we are already connected to the debug server
# When the adapter is spawned by the debug server, it is connected to the
# latter from the get go, and "host" and "port" in the "attach" request
# are actually the host and port on which the adapter itself was listening,
# so we can ignore those.
if self.server:
return

host = request("host", "127.0.0.1")
Expand All @@ -248,8 +251,11 @@ def attach_request(self, request):
else:
self.session.connect_to_server((host, port))
else:
if self.server is not None:
raise request.isnt_valid("Session is already started")
if self.server:
raise request.isnt_valid(
'"attach" with "processId" cannot be serviced by adapter '
"that is already associated with a debug server"
)

ptvsd_args = request("ptvsdArgs", json.array(unicode))
self.session.inject_server(pid, ptvsd_args)
Expand Down
18 changes: 15 additions & 3 deletions src/ptvsd/adapter/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@

import ptvsd
import ptvsd.launcher
from ptvsd.common import compat, fmt, log, messaging, options as common_options, sockets, util
from ptvsd.common import (
compat,
fmt,
log,
messaging,
options as common_options,
sockets,
util,
)
from ptvsd.adapter import components, ide, launcher, options as adapter_options, server


Expand Down Expand Up @@ -126,6 +134,7 @@ def connect_to_ide(self):
"""Sets up a DAP message channel to the IDE over stdio.
"""

log.info("{0} connecting to IDE over stdio...", self)
stream = messaging.JsonIOStream.from_stdio()

# Make sure that nothing else tries to interfere with the stdio streams
Expand All @@ -143,6 +152,7 @@ def connect_to_server(self, address):
"""

host, port = address
log.info("{0} connecting to Server on {1}:{2}...", self, host, port)
sock = sockets.create_client()
sock.connect(address)

Expand Down Expand Up @@ -208,7 +218,9 @@ def spawn_launcher():
with self._accept_connection_from_launcher() as (_, launcher_port):
env[str("PTVSD_LAUNCHER_PORT")] = str(launcher_port)
if common_options.log_dir is not None:
env[str("PTVSD_LOG_DIR")] = compat.filename_str(common_options.log_dir)
env[str("PTVSD_LOG_DIR")] = compat.filename_str(
common_options.log_dir
)
if adapter_options.log_stderr:
env[str("PTVSD_LOG_STDERR")] = str("debug info warning error")
if console == "internalConsole":
Expand Down Expand Up @@ -369,7 +381,7 @@ def _finalize(self, why, terminate_debuggee):

# Tell the IDE that debugging is over, but don't close the channel until it
# tells us to, via the "disconnect" request.
if self.ide.is_connected:
if self.ide and self.ide.is_connected:
try:
self.ide.channel.send_event("terminated")
except Exception:
Expand Down
Loading

0 comments on commit 8f358d6

Please sign in to comment.