Exemple #1
0
def attach_by_pid(session, target, cwd=None, wait=True):
    if platform.system() != "Windows":
        pytest.skip("https://github.com/microsoft/ptvsd/issues/1810")
    if sys.version_info < (3, ):
        pytest.skip("https://github.com/microsoft/ptvsd/issues/1811")

    log.info("Attaching {0} to {1} by PID.", session, target)

    config = session.config
    try:
        config["processId"] = int(target)
    except TypeError:
        pass

    if "processId" not in config:
        _attach_common_config(session, target, cwd)
        args = target.cli(session.spawn_debuggee.env)

        if wait:
            debug_me = """
import sys
while not "ptvsd" in sys.modules: pass
import ptvsd
while not ptvsd.is_attached(): pass
    """
        else:
            debug_me = None

        session.spawn_debuggee(args, cwd=cwd, debug_me=debug_me)
        config["processId"] = session.debuggee.pid

    session.spawn_adapter()
    return session.request_attach()
Exemple #2
0
def attach_connect(session, target, method, cwd=None, wait=True, log_dir=None):
    log.info("Attaching {0} to {1} by socket using {2}.", session, target,
             method.upper())

    assert method in ("api", "cli")

    config = _attach_common_config(session, target, cwd)
    config["connect"] = {}
    config["connect"]["host"] = host = attach_connect.host
    config["connect"]["port"] = port = attach_connect.port

    if method == "cli":
        args = [
            os.path.dirname(debugpy.__file__),
            "--listen",
            compat.filename_str(host) + ":" + str(port),
        ]
        if wait:
            args += ["--wait-for-client"]
        if log_dir is not None:
            args += ["--log-to", log_dir]
        if "subProcess" in config:
            args += ["--configure-subProcess", str(config["subProcess"])]
        debuggee_setup = None
    elif method == "api":
        args = []
        api_config = {k: v for k, v in config.items() if k in {"subProcess"}}
        debuggee_setup = """
import debugpy
if {log_dir!r}:
    debugpy.log_to({log_dir!r})
debugpy.configure({api_config!r})
debugpy.listen(({host!r}, {port!r}))
if {wait!r}:
    debugpy.wait_for_client()
"""
        debuggee_setup = fmt(
            debuggee_setup,
            host=host,
            port=port,
            wait=wait,
            log_dir=log_dir,
            api_config=api_config,
        )
    else:
        raise ValueError
    args += target.cli(session.spawn_debuggee.env)

    try:
        del config["subProcess"]
    except KeyError:
        pass

    session.spawn_debuggee(args, cwd=cwd, setup=debuggee_setup)
    if wait:
        session.wait_for_adapter_socket()

    session.connect_to_adapter((host, port))
    return session.request_attach()
Exemple #3
0
def attach_by_socket(session,
                     target,
                     method,
                     listener="server",
                     cwd=None,
                     wait=True,
                     log_dir=None):
    log.info("Attaching {0} to {1} by socket using {2}.", session, target,
             method.upper())

    assert method in ("api", "cli")
    assert listener in ("server")  # TODO: ("adapter", "server")

    config = _attach_common_config(session, target, cwd)

    host = config["host"] = attach_by_socket.host
    port = config["port"] = attach_by_socket.port

    if method == "cli":
        args = [os.path.dirname(ptvsd.__file__)]
        if wait:
            args += ["--wait"]
        args += ["--host", compat.filename_str(host), "--port", str(port)]
        if not config["subProcess"]:
            args += ["--no-subprocesses"]
        if log_dir is not None:
            args += ["--log-dir", log_dir]
        debug_me = None
    elif method == "api":
        args = []
        debug_me = """
import ptvsd
ptvsd.enable_attach(({host!r}, {port!r}), {args})
if {wait!r}:
    ptvsd.wait_for_attach()
"""
        attach_args = "" if log_dir is None else fmt("log_dir={0!r}", log_dir)
        debug_me = fmt(debug_me,
                       host=host,
                       port=port,
                       wait=wait,
                       args=attach_args)
    else:
        raise ValueError
    args += target.cli(session.spawn_debuggee.env)

    session.spawn_debuggee(args, cwd=cwd, debug_me=debug_me)
    if wait:
        session.wait_for_enable_attach()

    session.connect_to_adapter((host, port))
    return session.request_attach()
Exemple #4
0
def attach_by_pid(session, target, cwd=None, wait=True):
    if sys.version_info < (3,) and sys.platform == "win32":
        pytest.skip("https://github.com/microsoft/ptvsd/issues/1811")
    if sys.version_info < (3,) and sys.platform == "darwin":
        pytest.skip("https://github.com/microsoft/ptvsd/issues/1916")
    if wait and not sys.platform.startswith("linux"):
        pytest.skip("https://github.com/microsoft/ptvsd/issues/1926")

    log.info("Attaching {0} to {1} by PID.", session, target)

    config = session.config
    try:
        config["processId"] = int(target)
    except TypeError:
        pass

    if "processId" not in config:
        _attach_common_config(session, target, cwd)
        args = target.cli(session.spawn_debuggee.env)

        if wait:
            debug_me = """
import sys
import threading
import time

while "ptvsd" not in sys.modules:
    time.sleep(0.1)

from debug_me import scratchpad

while "_attach_by_pid" not in scratchpad:
    time.sleep(0.1)
    """
        else:
            debug_me = None

        session.spawn_debuggee(args, cwd=cwd, debug_me=debug_me)
        config["processId"] = session.debuggee.pid

    session.spawn_adapter()
    with session.request_attach():
        yield

    if wait:
        session.scratchpad["_attach_by_pid"] = True
Exemple #5
0
 def spawn_debuggee(occ):
     assert occ.body == some.dict.containing({"host": host, "port": port})
     session.spawn_debuggee(args, cwd=cwd, setup=debuggee_setup)