Example #1
0
def attach(port, host, client, log_dir):
    try:
        import sys
        if 'threading' not in sys.modules:
            try:

                def on_warn(msg):
                    import ptvsd.log
                    ptvsd.log.warn(msg)

                def on_exception(msg):
                    import ptvsd.log
                    ptvsd.log.exception(msg)

                def on_critical(msg):
                    import ptvsd.log
                    ptvsd.log.error(msg)

                import os
                sys.path.append(
                    os.path.join(
                        os.path.dirname(__file__),
                        '_vendored',
                        'pydevd',
                        'pydevd_attach_to_process'))

                # Note that it's not a part of the pydevd PYTHONPATH
                import attach_script
                attach_script.fix_main_thread_id(
                    on_warn=on_warn, on_exception=on_exception, on_critical=on_critical)
            except:
                import ptvsd.log
                ptvsd.log.exception()

        if not log_dir:
            log_dir = None

        import ptvsd.options
        ptvsd.options.log_dir = log_dir
        ptvsd.options.client = client
        ptvsd.options.host = host
        ptvsd.options.port = port

        import ptvsd.log
        ptvsd.log.to_file()
        ptvsd.log.info("Debugger successfully injected")

        if ptvsd.options.client:
            from ptvsd._remote import attach
            attach((host, port))
        else:
            ptvsd.enable_attach((host, port))

    except:
        import traceback
        traceback.print_exc()
Example #2
0
    def method():
        lock2.acquire()
        import threading  # Note: imported on wrong thread
        assert threading.current_thread().ident == thread.get_ident()
        assert threading.current_thread(
        ) is attach_script.get_main_thread_instance(threading)

        attach_script.fix_main_thread_id()

        assert threading.current_thread().ident == thread.get_ident()
        assert threading.current_thread(
        ) is not attach_script.get_main_thread_instance(threading)

        with lock:
            pass  # Will only finish when lock is released.
def attach(host, port, client, log_dir=None):
    try:
        import sys
        if 'threading' not in sys.modules:
            try:

                def on_warn(msg):
                    print(msg, file=sys.stderr)

                def on_exception(msg):
                    print(msg, file=sys.stderr)

                def on_critical(msg):
                    print(msg, file=sys.stderr)

                pydevd_attach_to_process_path = os.path.join(
                    _ptvsd_dir, 'ptvsd', '_vendored', 'pydevd',
                    'pydevd_attach_to_process')
                assert os.path.exists(pydevd_attach_to_process_path)
                sys.path.insert(0, pydevd_attach_to_process_path)

                # NOTE: that it's not a part of the pydevd PYTHONPATH
                import attach_script
                attach_script.fix_main_thread_id(on_warn=on_warn,
                                                 on_exception=on_exception,
                                                 on_critical=on_critical)

                # NOTE: At this point it should be safe to remove this.
                sys.path.remove(pydevd_attach_to_process_path)
            except:
                import traceback
                traceback.print_exc()
                raise

        sys.path.insert(0, _ptvsd_dir)
        import ptvsd

        # NOTE: Don't do sys.path.remove here it will remove all instances of that path
        # and the user may have set that to ptvsd path via PYTHONPATH
        assert sys.path[0] == _ptvsd_dir
        del sys.path[0]

        from ptvsd.common import options as common_opts
        from ptvsd.server import options
        if log_dir is not None:
            common_opts.log_dir = log_dir
        options.client = client
        options.host = host
        options.port = port

        if options.client:
            ptvsd.attach((options.host, options.port))
        else:
            ptvsd.enable_attach((options.host, options.port))

        from ptvsd.common import log
        log.info("Debugger successfully injected")

    except:
        import traceback
        traceback.print_exc()
        raise log.exception()
Example #4
0
def attach(setup):
    log = None
    try:
        import sys

        if "threading" not in sys.modules:
            try:

                def on_warn(msg):
                    print(msg, file=sys.stderr)

                def on_exception(msg):
                    print(msg, file=sys.stderr)

                def on_critical(msg):
                    print(msg, file=sys.stderr)

                pydevd_attach_to_process_path = os.path.join(
                    _debugpy_dir,
                    "debugpy",
                    "_vendored",
                    "pydevd",
                    "pydevd_attach_to_process",
                )
                assert os.path.exists(pydevd_attach_to_process_path)
                sys.path.insert(0, pydevd_attach_to_process_path)

                # NOTE: that it's not a part of the pydevd PYTHONPATH
                import attach_script

                attach_script.fix_main_thread_id(on_warn=on_warn,
                                                 on_exception=on_exception,
                                                 on_critical=on_critical)

                # NOTE: At this point it should be safe to remove this.
                sys.path.remove(pydevd_attach_to_process_path)
            except:
                import traceback

                traceback.print_exc()
                raise

        sys.path.insert(0, _debugpy_dir)
        try:
            import debugpy
            import debugpy.server
            from debugpy.common import log
            import pydevd
        finally:
            assert sys.path[0] == _debugpy_dir
            del sys.path[0]

        py_db = pydevd.get_global_debugger()
        if py_db is not None:
            py_db.dispose_and_kill_all_pydevd_threads(wait=False)

        if setup["log_to"] is not None:
            debugpy.log_to(setup["log_to"])
        log.info("Configuring injected debugpy: {0!j}", setup)

        if setup["mode"] == "listen":
            debugpy.listen(setup["address"])
        elif setup["mode"] == "connect":
            debugpy.connect(setup["address"],
                            access_token=setup["adapter_access_token"])
        else:
            raise AssertionError(repr(setup))

    except:
        import traceback

        traceback.print_exc()
        if log is None:
            raise
        else:
            log.reraise_exception()

    log.info("debugpy injected successfully")