Exemplo n.º 1
0
def test_flask_breakpoint_no_multiproc(start_flask, bp_target):
    bp_file, bp_line, bp_name = {
        "code": (paths.app_py, lines.app_py["bphome"], "home"),
        "template": (paths.hello_html, 8, "template"),
    }[bp_target]
    bp_var_content = compat.force_str("Flask-Jinja-Test")

    with debug.Session() as session:
        with start_flask(session):
            session.set_breakpoints(bp_file, [bp_line])

        with flask_server:
            home_request = flask_server.get("/")
            session.wait_for_stop(
                "breakpoint",
                expected_frames=[
                    some.dap.frame(some.dap.source(bp_file), name=bp_name, line=bp_line)
                ],
            )

            var_content = session.get_variable("content")
            assert var_content == some.dict.containing(
                {
                    "name": "content",
                    "type": "str",
                    "value": repr(bp_var_content),
                    "presentationHint": {"attributes": ["rawString"]},
                    "evaluateName": "content",
                    "variablesReference": 0,
                }
            )

            session.request_continue()
            assert bp_var_content in home_request.response_text()
Exemplo n.º 2
0
def Response(request, body=some.object):
    assert isinstance(request, Expectation) or isinstance(
        request, RequestOccurrence)

    exp = PatternExpectation("response", request, body)
    exp.timeline = request.timeline
    exp.has_lower_bound = request.has_lower_bound

    # Try to be as specific as possible.
    if isinstance(request, Expectation):
        if request.circumstances[0] != "request":
            exp.describe = lambda: fmt("response to {0!r}", request)
            return
        else:
            items = (("command", request.circumstances[1]), )
    else:
        items = (("command", request.command), )

    if isinstance(request, Occurrence):
        items += (("request_seq", request.seq), )

    if body is some.object:
        items += (("\002...", "...\003"), )
    elif body is some.error or body == some.error:
        items += (("success", False), )
        if body == some.error:
            items += (("message", compat.force_str(body)), )
    else:
        items += (("body", body), )

    exp.describe = lambda: _describe_message("response", *items)
    return exp
Exemplo n.º 3
0
def test_flask_breakpoint_multiproc(start_flask):
    bp_line = lines.app_py["bphome"]
    bp_var_content = compat.force_str("Flask-Jinja-Test")

    with debug.Session() as parent_session:
        with start_flask(parent_session, multiprocess=True):
            parent_session.set_breakpoints(paths.app_py, [bp_line])

        child_pid = parent_session.wait_for_next_subprocess()
        with debug.Session() as child_session:
            # TODO: this is wrong, but we don't have multiproc attach
            # yet, so update this when that is done
            # https://github.com/microsoft/ptvsd/issues/1776
            with child_session.attach_by_pid(child_pid):
                child_session.set_breakpoints(paths.app_py, [bp_line])

            with flask_server:
                home_request = flask_server.get("/")
                child_session.wait_for_stop(
                    "breakpoint",
                    expected_frames=[
                        some.dap.frame(some.dap.source(paths.app_py),
                                       line=bp_line,
                                       name="home")
                    ],
                )

                var_content = child_session.get_variable("content")
                assert var_content == some.dict.containing({
                    "name":
                    "content",
                    "type":
                    "str",
                    "value":
                    repr(bp_var_content),
                    "presentationHint": {
                        "attributes": ["rawString"]
                    },
                    "evaluateName":
                    "content",
                    "variablesReference":
                    0,
                })

                child_session.request_continue()
                assert bp_var_content in home_request.response_text()
Exemplo n.º 4
0
def test_django_breakpoint_multiproc(start_django):
    bp_line = lines.app_py["bphome"]
    bp_var_content = compat.force_str("Django-Django-Test")

    with debug.Session() as parent_session:
        with start_django(parent_session, multiprocess=True):
            parent_session.set_breakpoints(paths.app_py, [bp_line])

        with parent_session.wait_for_next_subprocess() as child_session:
            with child_session.start():
                child_session.set_breakpoints(paths.app_py, [bp_line])

            with django_server:
                home_request = django_server.get("/home")
                child_session.wait_for_stop(
                    "breakpoint",
                    expected_frames=[
                        some.dap.frame(some.dap.source(paths.app_py),
                                       line=bp_line,
                                       name="home")
                    ],
                )

                var_content = child_session.get_variable("content")
                assert var_content == some.dict.containing({
                    "name":
                    "content",
                    "type":
                    "str",
                    "value":
                    compat.unicode_repr(bp_var_content),
                    "presentationHint": {
                        "attributes": ["rawString"]
                    },
                    "evaluateName":
                    "content",
                })

                child_session.request_continue()
                assert bp_var_content in home_request.response_text()
Exemplo n.º 5
0
def spawn(process_name, cmdline, cwd, env, redirect_output):
    log.info(
        "Spawning debuggee process:\n\n"
        "Current directory: {0!j}\n\n"
        "Command line: {1!j}\n\n"
        "Environment variables: {2!j}\n\n",
        cwd,
        cmdline,
        env,
    )

    close_fds = set()
    try:
        if redirect_output:
            # subprocess.PIPE behavior can vary substantially depending on Python version
            # and platform; using our own pipes keeps it simple, predictable, and fast.
            stdout_r, stdout_w = os.pipe()
            stderr_r, stderr_w = os.pipe()
            close_fds |= {stdout_r, stdout_w, stderr_r, stderr_w}
            kwargs = dict(stdout=stdout_w, stderr=stderr_w)
        else:
            kwargs = {}

        try:
            global process
            process = subprocess.Popen(cmdline,
                                       cwd=cwd,
                                       env=env,
                                       bufsize=0,
                                       **kwargs)
        except Exception as exc:
            raise messaging.MessageHandlingError(
                fmt("Couldn't spawn debuggee: {0}\n\nCommand line:{1!r}", exc,
                    cmdline))

        log.info("Spawned {0}.", describe())
        atexit.register(kill)
        launcher.channel.send_event(
            "process",
            {
                "startMethod": "launch",
                "isLocalProcess": True,
                "systemProcessId": process.pid,
                "name": process_name,
                "pointerSize": struct.calcsize(compat.force_str("P")) * 8,
            },
        )

        if redirect_output:
            for category, fd, tee in [
                ("stdout", stdout_r, sys.stdout),
                ("stderr", stderr_r, sys.stderr),
            ]:
                output.CaptureOutput(describe(), category, fd, tee)
                close_fds.remove(fd)

        wait_thread = threading.Thread(target=wait_for_exit,
                                       name="wait_for_exit()")
        wait_thread.daemon = True
        wait_thread.start()

    finally:
        for fd in close_fds:
            try:
                os.close(fd)
            except Exception:
                log.exception()
Exemplo n.º 6
0
Arquivo: api.py Projeto: int19h/ptvsd
def enable_attach(dont_trace_start_patterns, dont_trace_end_patterns):
    # Errors below are logged with level="info", because the caller might be catching
    # and handling exceptions, and we don't want to spam their stderr unnecessarily.

    import subprocess

    if hasattr(enable_attach, "adapter"):
        raise AssertionError("enable_attach() can only be called once per process")

    server_access_token = compat.force_str(codecs.encode(os.urandom(32), "hex"))

    try:
        endpoints_listener = sockets.create_server("127.0.0.1", 0, timeout=5)
    except Exception as exc:
        log.exception("Can't listen for adapter endpoints:")
        raise RuntimeError("can't listen for adapter endpoints: " + str(exc))
    endpoints_host, endpoints_port = endpoints_listener.getsockname()
    log.info(
        "Waiting for adapter endpoints on {0}:{1}...", endpoints_host, endpoints_port
    )

    adapter_args = [
        sys.executable,
        os.path.dirname(adapter.__file__),
        "--for-server",
        str(endpoints_port),
        "--host",
        options.host,
        "--port",
        str(options.port),
        "--server-access-token",
        server_access_token,
    ]
    if log.log_dir is not None:
        adapter_args += ["--log-dir", log.log_dir]
    log.info("enable_attach() spawning adapter: {0!j}", adapter_args)

    # On Windows, detach the adapter from our console, if any, so that it doesn't
    # receive Ctrl+C from it, and doesn't keep it open once we exit.
    creationflags = 0
    if sys.platform == "win32":
        creationflags |= 0x08000000  # CREATE_NO_WINDOW
        creationflags |= 0x00000200  # CREATE_NEW_PROCESS_GROUP

    # Adapter will outlive this process, so we shouldn't wait for it. However, we
    # need to ensure that the Popen instance for it doesn't get garbage-collected
    # by holding a reference to it in a non-local variable, to avoid triggering
    # https://bugs.python.org/issue37380.
    try:
        enable_attach.adapter = subprocess.Popen(
            adapter_args, close_fds=True, creationflags=creationflags
        )
        if os.name == "posix":
            # It's going to fork again to daemonize, so we need to wait on it to
            # clean it up properly.
            enable_attach.adapter.wait()
        else:
            # Suppress misleading warning about child process still being alive when
            # this process exits (https://bugs.python.org/issue38890).
            enable_attach.adapter.returncode = 0
            pydevd.add_dont_terminate_child_pid(enable_attach.adapter.pid)
    except Exception as exc:
        log.exception("Error spawning debug adapter:", level="info")
        raise RuntimeError("error spawning debug adapter: " + str(exc))

    try:
        sock, _ = endpoints_listener.accept()
        try:
            sock.settimeout(None)
            sock_io = sock.makefile("rb", 0)
            try:
                endpoints = json.loads(sock_io.read().decode("utf-8"))
            finally:
                sock_io.close()
        finally:
            sockets.close_socket(sock)
    except socket.timeout:
        log.exception("Timed out waiting for adapter to connect:", level="info")
        raise RuntimeError("timed out waiting for adapter to connect")
    except Exception as exc:
        log.exception("Error retrieving adapter endpoints:", level="info")
        raise RuntimeError("error retrieving adapter endpoints: " + str(exc))

    log.info("Endpoints received from adapter: {0!j}", endpoints)

    if "error" in endpoints:
        raise RuntimeError(str(endpoints["error"]))

    try:
        host = str(endpoints["server"]["host"])
        port = int(endpoints["server"]["port"])
        options.port = int(endpoints["ide"]["port"])
    except Exception as exc:
        log.exception(
            "Error parsing adapter endpoints:\n{0!j}\n", endpoints, level="info"
        )
        raise RuntimeError("error parsing adapter endpoints: " + str(exc))
    log.info(
        "Adapter is accepting incoming IDE connections on {0}:{1}",
        options.host,
        options.port,
    )

    _settrace(
        host=host,
        port=port,
        suspend=False,
        patch_multiprocessing=options.multiprocess,
        wait_for_ready_to_run=False,
        block_until_connected=True,
        dont_trace_start_patterns=dont_trace_start_patterns,
        dont_trace_end_patterns=dont_trace_end_patterns,
        access_token=server_access_token,
        client_access_token=options.client_access_token,
    )
    log.info("pydevd is connected to adapter at {0}:{1}", host, port)
    return options.host, options.port
Exemplo n.º 7
0
Arquivo: api.py Projeto: yazici/ptvsd
def enable_attach(dont_trace_start_patterns, dont_trace_end_patterns):
    if hasattr(enable_attach, "called"):
        raise RuntimeError(
            "enable_attach() can only be called once per process.")

    server_access_token = compat.force_str(codecs.encode(
        os.urandom(32), "hex"))

    import subprocess

    adapter_args = [
        sys.executable,
        _ADAPTER_PATH,
        "--for-server",
        "--host",
        options.host,
        "--port",
        str(options.port),
        "--server-access-token",
        server_access_token,
    ]

    if log.log_dir is not None:
        adapter_args += ["--log-dir", log.log_dir]

    log.info("enable_attach() spawning adapter: {0!r}", adapter_args)

    # Adapter will outlive this process, so we shouldn't wait for it. However, we do
    # need to ensure that the Popen instance for it doesn't get garbage-collected, to
    # avoid triggering https://bugs.python.org/issue37380.
    enable_attach.process = process = subprocess.Popen(adapter_args,
                                                       bufsize=0,
                                                       stdout=subprocess.PIPE)

    line = process.stdout.readline()
    if isinstance(line, bytes):
        line = line.decode("utf-8")
    connection_details = json.JSONDecoder().decode(line)
    log.info("Connection details received from adapter: {0!r}",
             connection_details)

    host = "127.0.0.1"  # This should always be loopback address.
    port = connection_details["server"]["port"]

    pydevd.settrace(
        host=host,
        port=port,
        suspend=False,
        patch_multiprocessing=options.multiprocess,
        wait_for_ready_to_run=False,
        block_until_connected=True,
        dont_trace_start_patterns=dont_trace_start_patterns,
        dont_trace_end_patterns=dont_trace_end_patterns,
        access_token=server_access_token,
        client_access_token=options.client_access_token,
    )

    log.info("pydevd debug client connected to: {0}:{1}", host, port)

    # Ensure that we ignore the adapter process when terminating the debugger.
    pydevd.add_dont_terminate_child_pid(process.pid)
    options.port = connection_details["ide"]["port"]

    enable_attach.called = True
    log.info("ptvsd debug server running at: {0}:{1}", options.host,
             options.port)
    return options.host, options.port
Exemplo n.º 8
0
def main(args):
    from ptvsd import adapter
    from ptvsd.common import compat, log, sockets
    from ptvsd.adapter import ide, servers, sessions

    if args.for_server is not None:
        if os.name == "posix":
            # On POSIX, we need to leave the process group and its session, and then
            # daemonize properly by double-forking (first fork already happened when
            # this process was spawned).
            os.setsid()
            if os.fork() != 0:
                sys.exit(0)

        for stdio in sys.stdin, sys.stdout, sys.stderr:
            if stdio is not None:
                stdio.close()

    if args.log_stderr:
        log.stderr.levels |= set(log.LEVELS)
    if args.log_dir is not None:
        log.log_dir = args.log_dir

    log.to_file(prefix="ptvsd.adapter")
    log.describe_environment("ptvsd.adapter startup environment:")

    servers.access_token = args.server_access_token
    if args.for_server is None:
        adapter.access_token = compat.force_str(
            codecs.encode(os.urandom(32), "hex"))

    try:
        server_host, server_port = servers.listen()
    except Exception as exc:
        if args.for_server is None:
            raise
        endpoints = {
            "error": "Can't listen for server connections: " + str(exc)
        }
    else:
        endpoints = {"server": {"host": server_host, "port": server_port}}
        try:
            ide_host, ide_port = ide.listen(port=args.port)
        except Exception as exc:
            if args.for_server is None:
                raise
            endpoints = {
                "error": "Can't listen for IDE connections: " + str(exc)
            }
        else:
            endpoints["ide"] = {"host": ide_host, "port": ide_port}

    if args.for_server is not None:
        log.info(
            "Sending endpoints info to debug server at localhost:{0}:\n{1!j}",
            args.for_server,
            endpoints,
        )

        try:
            sock = sockets.create_client()
            try:
                sock.settimeout(None)
                sock.connect(("127.0.0.1", args.for_server))
                sock_io = sock.makefile("wb", 0)
                try:
                    sock_io.write(json.dumps(endpoints).encode("utf-8"))
                finally:
                    sock_io.close()
            finally:
                sockets.close_socket(sock)
        except Exception:
            raise log.exception(
                "Error sending endpoints info to debug server:")

        if "error" in endpoints:
            log.error("Couldn't set up endpoints; exiting.")
            sys.exit(1)

    listener_file = os.getenv("PTVSD_ADAPTER_ENDPOINTS")
    if listener_file is not None:
        log.info("Writing endpoints info to {0!r}:\n{1!j}", listener_file,
                 endpoints)

        def delete_listener_file():
            log.info("Listener ports closed; deleting {0!r}", listener_file)
            try:
                os.remove(listener_file)
            except Exception:
                log.exception("Failed to delete {0!r}",
                              listener_file,
                              level="warning")

        try:
            with open(listener_file, "w") as f:
                atexit.register(delete_listener_file)
                print(json.dumps(endpoints), file=f)
        except Exception:
            raise log.exception("Error writing endpoints info to file:")

    if args.port is None:
        ide.IDE("stdio")

    # These must be registered after the one above, to ensure that the listener sockets
    # are closed before the endpoint info file is deleted - this way, another process
    # can wait for the file to go away as a signal that the ports are no longer in use.
    atexit.register(servers.stop_listening)
    atexit.register(ide.stop_listening)

    servers.wait_until_disconnected()
    log.info(
        "All debug servers disconnected; waiting for remaining sessions...")

    sessions.wait_until_ended()
    log.info("All debug sessions have ended; exiting.")
Exemplo n.º 9
0
def main(args):
    from ptvsd import adapter
    from ptvsd.common import compat, log
    from ptvsd.adapter import ide, servers, sessions

    if args.log_stderr:
        log.stderr.levels |= set(log.LEVELS)
    if args.log_dir is not None:
        log.log_dir = args.log_dir

    log.to_file(prefix="ptvsd.adapter")
    log.describe_environment("ptvsd.adapter startup environment:")

    if args.for_server and args.port is None:
        log.error("--for-server requires --port")
        sys.exit(64)

    servers.access_token = args.server_access_token
    if not args.for_server:
        adapter.access_token = compat.force_str(
            codecs.encode(os.urandom(32), "hex"))

    server_host, server_port = servers.listen()
    ide_host, ide_port = ide.listen(port=args.port)
    endpoints_info = {
        "ide": {
            "host": ide_host,
            "port": ide_port
        },
        "server": {
            "host": server_host,
            "port": server_port
        },
    }

    if args.for_server:
        log.info("Writing endpoints info to stdout:\n{0!r}", endpoints_info)
        print(json.dumps(endpoints_info))
        sys.stdout.flush()

    if args.port is None:
        ide.IDE("stdio")

    listener_file = os.getenv("PTVSD_ADAPTER_ENDPOINTS")
    if listener_file is not None:
        log.info("Writing endpoints info to {0!r}:\n{1!r}", listener_file,
                 endpoints_info)

        def delete_listener_file():
            log.info("Listener ports closed; deleting {0!r}", listener_file)
            try:
                os.remove(listener_file)
            except Exception:
                log.exception("Failed to delete {0!r}",
                              listener_file,
                              level="warning")

        with open(listener_file, "w") as f:
            atexit.register(delete_listener_file)
            print(json.dumps(endpoints_info), file=f)

    # These must be registered after the one above, to ensure that the listener sockets
    # are closed before the endpoint info file is deleted - this way, another process
    # can wait for the file to go away as a signal that the ports are no longer in use.
    atexit.register(servers.stop_listening)
    atexit.register(ide.stop_listening)

    servers.wait_until_disconnected()
    log.info(
        "All debug servers disconnected; waiting for remaining sessions...")

    sessions.wait_until_ended()
    log.info("All debug sessions have ended; exiting.")