Пример #1
0
    def on_legacyRemoteDebuggerAttach(self, request, args):
        if self.__waiting_for_authentication:
            self.send_response(
                request,
                success=False,
                message=
                'legacyRemoteDebuggerAuthenticate request must be sent first.',
            )
            self.set_exit()
            return

        try:
            debug_options = vspd.parse_debug_options(args['debugOptions'])
            if self.__redirect_output:
                debug_options.add('RedirectOutput')

            if vspd.DETACHED:
                try:
                    pid = os.getpid()
                except AttributeError:
                    pid = 0

                major, minor, micro, release_level, serial = sys.version_info

                self.send_response(
                    request,
                    accepted=True,
                    processId=pid,
                    pythonMajor=major,
                    pythonMinor=minor,
                    pythonMicro=micro,
                )

                vspd.attach_process_from_socket(self.__owned_socket,
                                                debug_options,
                                                report=True)
                vspd.mark_all_threads_for_break(vspd.STEPPING_ATTACH_BREAK)

                _attached.set()

                # Prevent from closing the socket, it will be used by debugger
                self.__owned_socket = None
            else:
                self.send_response(
                    request,
                    accepted=False,
                )
        finally:
            self.set_exit()
    def server_thread_func():
        while True:
            client = None
            raw_client = None
            try:
                client, addr = server.accept()
                if certfile:
                    client = ssl.wrap_socket(client,
                                             server_side=True,
                                             ssl_version=ssl.PROTOCOL_TLSv1,
                                             certfile=certfile,
                                             keyfile=keyfile)
                write_bytes(client, PTVSDBG)
                write_int(client, PTVSDBG_VER)

                response = read_bytes(client, 7)
                if response != PTVSDBG:
                    continue
                dbg_ver = read_int(client)
                if dbg_ver != PTVSDBG_VER:
                    continue

                client_secret = read_string(client)
                if secret is None or secret == client_secret:
                    write_bytes(client, ACPT)
                else:
                    write_bytes(client, RJCT)
                    continue

                response = read_bytes(client, 4)

                if response == INFO:
                    try:
                        pid = os.getpid()
                    except AttributeError:
                        pid = 0
                    write_int(client, pid)

                    exe = sys.executable or ''
                    write_string(client, exe)

                    try:
                        username = getpass.getuser()
                    except AttributeError:
                        username = ''
                    write_string(client, username)

                    try:
                        impl = platform.python_implementation()
                    except AttributeError:
                        try:
                            impl = sys.implementation.name
                        except AttributeError:
                            impl = 'Python'

                    major, minor, micro, release_level, serial = sys.version_info

                    os_and_arch = platform.system()
                    if os_and_arch == "":
                        os_and_arch = sys.platform
                    try:
                        if sys.maxsize > 2**32:
                            os_and_arch += ' 64-bit'
                        else:
                            os_and_arch += ' 32-bit'
                    except AttributeError:
                        pass

                    version = '%s %s.%s.%s (%s)' % (impl, major, minor, micro,
                                                    os_and_arch)
                    write_string(client, version)

                    client.recv(1)

                elif response == ATCH:
                    debug_options = vspd.parse_debug_options(
                        read_string(client))
                    debug_options.setdefault('rules', []).append({
                        'path':
                        PY_ROOT,
                        'include':
                        False,
                    })
                    if redirect_output:
                        debug_options.add('RedirectOutput')

                    if vspd.DETACHED:
                        write_bytes(client, ACPT)
                        try:
                            pid = os.getpid()
                        except AttributeError:
                            pid = 0
                        write_int(client, pid)

                        major, minor, micro, release_level, serial = sys.version_info
                        write_int(client, major)
                        write_int(client, minor)
                        write_int(client, micro)

                        vspd.attach_process_from_socket(client,
                                                        debug_options,
                                                        report=True)
                        vspd.mark_all_threads_for_break(
                            vspd.STEPPING_ATTACH_BREAK)
                        _attached.set()
                        client = None
                    else:
                        write_bytes(client, RJCT)

                elif response == REPL:
                    if not vspd.DETACHED:
                        write_bytes(client, ACPT)
                        vspd.connect_repl_using_socket(client)
                        client = None
                    else:
                        write_bytes(client, RJCT)

            except (socket.error, OSError):
                pass
            finally:
                if client is not None:
                    client.close()
Пример #3
0
    def server_thread_func():
        while True:
            client = None
            raw_client = None
            try:
                client, addr = server.accept()
                if certfile:
                    client = ssl.wrap_socket(client, server_side = True, ssl_version = ssl.PROTOCOL_TLSv1, certfile = certfile, keyfile = keyfile)
                write_bytes(client, PTVSDBG)
                write_int(client, PTVSDBG_VER)

                response = read_bytes(client, 7)
                if response != PTVSDBG:
                    continue
                dbg_ver = read_int(client)
                if dbg_ver != PTVSDBG_VER:
                    continue

                client_secret = read_string(client)
                if secret is None or secret == client_secret:
                    write_bytes(client, ACPT)
                else:
                    write_bytes(client, RJCT)
                    continue

                response = read_bytes(client, 4)

                if response == INFO:
                    try:
                        pid = os.getpid()
                    except AttributeError:
                        pid = 0
                    write_int(client, pid)

                    exe = sys.executable or ''
                    write_string(client, exe)

                    try:
                        username = getpass.getuser()
                    except AttributeError:
                        username = ''
                    write_string(client, username)

                    try:
                        impl = platform.python_implementation()
                    except AttributeError:
                        try:
                            impl = sys.implementation.name
                        except AttributeError:
                            impl = 'Python'

                    major, minor, micro, release_level, serial = sys.version_info

                    os_and_arch = platform.system()
                    if os_and_arch == "":
                        os_and_arch = sys.platform
                    try:
                        if sys.maxsize > 2**32:
                            os_and_arch += ' 64-bit'
                        else:
                            os_and_arch += ' 32-bit'
                    except AttributeError:
                        pass

                    version = '%s %s.%s.%s (%s)' % (impl, major, minor, micro, os_and_arch)
                    write_string(client, version)

                    # Don't just drop the connection - let the debugger close it after it finishes reading.
                    client.recv(1)

                elif response == ATCH:
                    debug_options = vspd.parse_debug_options(read_string(client))
                    if redirect_output:
                        debug_options.add('RedirectOutput')

                    if vspd.DETACHED:
                        write_bytes(client, ACPT)
                        try:
                            pid = os.getpid()
                        except AttributeError:
                            pid = 0
                        write_int(client, pid)

                        major, minor, micro, release_level, serial = sys.version_info
                        write_int(client, major)
                        write_int(client, minor)
                        write_int(client, micro)

                        vspd.attach_process_from_socket(client, debug_options, report = True)
                        vspd.mark_all_threads_for_break(vspd.STEPPING_ATTACH_BREAK)
                        _attached.set()
                        client = None
                    else:
                        write_bytes(client, RJCT)

                elif response == REPL:
                    if not vspd.DETACHED:
                        write_bytes(client, ACPT)
                        vspd.connect_repl_using_socket(client)
                        client = None
                    else:
                        write_bytes(client, RJCT)

            except (socket.error, OSError):
                pass
            finally:
                if client is not None:
                    client.close()
def launch():
    import os
    import os.path
    import sys
    import traceback
    try:
        import ptvsd.visualstudio_py_debugger as vspd
    except:
        traceback.print_exc()
        print('''
    Internal error detected. Please copy the above traceback and report at
    http://go.microsoft.com/fwlink/?LinkId=293415

    Press Enter to close. . .''')
        try:
            raw_input()
        except NameError:
            input()
        sys.exit(1)

    # Arguments are:
    # 1. Working directory.
    # 2. VS debugger port to connect to.
    # 3. GUID for the debug session.
    # 4. Debug options (as integer - see enum PythonDebugOptions).
    # 5. '-m' or '-c' to override the default run-as mode. [optional]
    # 6. Startup script name.
    # 7. Script arguments.

    # change to directory we expected to start from
    os.chdir(sys.argv[1])

    port_num = int(sys.argv[2])
    debug_id = sys.argv[3]
    debug_options = vspd.parse_debug_options(sys.argv[4])
    del sys.argv[0:5]

    # set run_as mode appropriately
    run_as = 'script'
    if sys.argv and sys.argv[0] == '-m':
        run_as = 'module'
        del sys.argv[0]
    if sys.argv and sys.argv[0] == '-c':
        run_as = 'code'
        del sys.argv[0]

    # preserve filename before we del sys
    filename = sys.argv[0]

    # exclude ourselves from being debugged
    vspd.DONT_DEBUG.append(os.path.normcase(__file__))

    ## Begin modification by Don Jayamanne
    # Get current Process id to pass back to debugger
    currentPid = os.getpid()
    ## End Modification by Don Jayamanne

    # remove all state we imported
    del sys, os

    # and start debugging
    ## Begin modification by Don Jayamanne
    # Pass current Process id to pass back to debugger
    vspd.debug(filename, port_num, debug_id, debug_options, currentPid, run_as)