Esempio n. 1
0
    def test_attach_started_separately(self):
        lockfile = self.workspace.lockfile()
        done, waitscript = lockfile.wait_in_script()
        filename = self.write_script('spam.py', waitscript)
        addr = Address('localhost', 8888)
        with DebugAdapter.start_for_attach(addr, filename) as adapter:
            with DebugClient() as editor:
                session = editor.attach_socket(addr, adapter)

                with session.wait_for_event('thread'):
                    (req_initialize, req_launch, req_config, _, _, _
                     ) = lifecycle_handshake(session, 'attach')

                done()
                adapter.wait()

        received = list(_strip_newline_output_events(session.received))
        self.assert_received(received[:7], [
            self.new_version_event(session.received),
            self.new_response(req_initialize.req, **INITIALIZE_RESPONSE),
            self.new_event('initialized'),
            self.new_response(req_launch.req),
            self.new_response(req_config.req),
            self.new_event('process', **{
                'isLocalProcess': True,
                'systemProcessId': adapter.pid,
                'startMethod': 'attach',
                'name': filename,
            }),
            self.new_event('thread', reason='started', threadId=1),
            #self.new_event('thread', reason='exited', threadId=1),
            #self.new_event('exited', exitCode=0),
            #self.new_event('terminated'),
        ])
Esempio n. 2
0
    def test_reattach(self):
        lockfile1 = self.workspace.lockfile()
        done1, waitscript1 = lockfile1.wait_in_script(timeout=5)
        lockfile2 = self.workspace.lockfile()
        done2, waitscript2 = lockfile2.wait_in_script(timeout=5)
        filename = self.write_script('spam.py', waitscript1 + waitscript2)
        addr = Address('localhost', 8888)
        #DebugAdapter.VERBOSE = True
        with DebugAdapter.start_for_attach(addr, filename) as adapter:
            with DebugClient() as editor:
                # Attach initially.
                session1 = editor.attach_socket(addr, adapter)
                with session1.wait_for_event('thread'):
                    reqs = lifecycle_handshake(session1, 'attach')
                    reqs[1].wait()
                    done1()
                req_disconnect = session1.send_request('disconnect')
                req_disconnect.wait()
                editor.detach(adapter)

                # Re-attach
                session2 = editor.attach_socket(addr, adapter)
                (req_initialize, req_launch, req_config, _, _, _
                 ) = lifecycle_handshake(session2, 'attach')
                req_launch.wait()
                done2()

                adapter.wait()

        received = list(_strip_newline_output_events(session1.received))

        self.assert_contains(received, [
            self.new_version_event(session1.received),
            self.new_response(reqs[0].req, **INITIALIZE_RESPONSE),
            self.new_event('initialized'),
            self.new_response(reqs[1].req),
            self.new_response(reqs[2].req),
            self.new_event('process', **{
                'isLocalProcess': True,
                'systemProcessId': adapter.pid,
                'startMethod': 'attach',
                'name': filename,
            }),
            self.new_event('thread', reason='started', threadId=1),
            self.new_response(req_disconnect.req),
        ])
        self.messages.reset_all()
        received = list(_strip_newline_output_events(session2.received))

        self.assert_contains(received, [
            self.new_version_event(session2.received),
            self.new_response(req_initialize.req, **INITIALIZE_RESPONSE),
            self.new_event('initialized'),
            self.new_response(req_launch.req),
            self.new_response(req_config.req),
            self.new_event('process', **{
                'isLocalProcess': True,
                'systemProcessId': adapter.pid,
                'startMethod': 'attach',
                'name': filename,
            }),
            self.new_event('exited', exitCode=0),
            self.new_event('terminated'),
        ])
Esempio n. 3
0
    def start_debugging(self, debug_info):
        addr = Address('localhost', debug_info.port)
        cwd = debug_info.cwd
        env = debug_info.env
        wait_for_port_to_free(debug_info.port)

        def _kill_proc(pid):
            """If debugger does not end gracefully, then kill proc and
            wait for socket connections to die out. """
            try:
                os.kill(pid, signal.SIGTERM)
            except Exception:
                pass
            time.sleep(1)  # wait for socket connections to die out.

        def _wrap_and_reraise(session, ex, exc_type, exc_value, exc_traceback):
            """If we have connetion errors, then re-raised wrapped in
            ConnectionTimeoutError. If using py3, then chain exceptions so
            we do not loose the original exception, else try hack approach
            for py27."""
            messages = []
            formatted_ex = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)) # noqa
            try:
                messages = [str(msg) for msg in
                            _strip_newline_output_events(session.received)]
            except Exception:
                pass

            message = """
Session Messages:
-----------------
{}

Original Error:
---------------
{}""".format(os.linesep.join(messages), formatted_ex)

            raise Exception(message)

        def _handle_exception(ex, adapter, session):
            exc_type, exc_value, exc_traceback = sys.exc_info()
            _kill_proc(adapter.pid)
            _wrap_and_reraise(session, ex, exc_type, exc_value, exc_traceback)

        if debug_info.verbose:
            DebugAdapter.VERBOSE = True
        if debug_info.attachtype == 'import' and \
            debug_info.modulename is not None:
            argv = debug_info.argv
            with DebugAdapter.start_wrapper_module(
                    debug_info.modulename,
                    argv,
                    env=env,
                    cwd=cwd) as adapter:
                with DebugClient() as editor:
                    time.sleep(DELAY_WAITING_FOR_SOCKETS)
                    session = editor.attach_socket(addr, adapter)
                    try:
                        yield Debugger(session=session, adapter=adapter)
                        adapter.wait()
                    except Exception as ex:
                        _handle_exception(ex, adapter, session)
        elif debug_info.attachtype == 'import' and \
            debug_info.starttype == 'attach' and \
            debug_info.filename is not None:
            argv = debug_info.argv
            adapter = DebugAdapter.start_embedded(
                addr,
                debug_info.filename,
                argv=argv,
                env=env,
                cwd=cwd,
            )
            with adapter:
                with DebugClient() as editor:
                    time.sleep(DELAY_WAITING_FOR_SOCKETS)
                    session = editor.attach_socket(addr, adapter)
                    try:
                        yield Debugger(session=session, adapter=adapter)
                        adapter.wait()
                    except Exception as ex:
                        _handle_exception(ex, adapter, session)
        elif debug_info.starttype == 'attach':
            if debug_info.modulename is None:
                name = debug_info.filename
                kind = 'script'
            else:
                name = debug_info.modulename
                kind = 'module'
            argv = debug_info.argv
            adapter = DebugAdapter.start_for_attach(
                addr,
                name=name,
                extra=argv,
                kind=kind,
                env=env,
                cwd=cwd,
            )
            with adapter:
                with DebugClient() as editor:
                    time.sleep(DELAY_WAITING_FOR_SOCKETS)
                    session = editor.attach_socket(addr, adapter)
                    try:
                        yield Debugger(session=session, adapter=adapter)
                        adapter.wait()
                    except Exception as ex:
                        _handle_exception(ex, adapter, session)
        else:
            if debug_info.filename is None:
                argv = ['-m', debug_info.modulename] + debug_info.argv
            else:
                argv = [debug_info.filename] + debug_info.argv
            with DebugClient(
                    port=debug_info.port,
                    connecttimeout=CONNECT_TIMEOUT) as editor:
                time.sleep(DELAY_WAITING_FOR_SOCKETS)
                adapter, session = editor.host_local_debugger(
                    argv, cwd=cwd, env=env)
                try:
                    yield Debugger(session=session, adapter=adapter)
                    adapter.wait()
                except Exception as ex:
                    _handle_exception(ex, adapter, session)