예제 #1
0
        // This function is used to terminate a process that the engine launched
        // The debugger will call IDebugEngineLaunch2::CanTerminateProcess before calling this method.
        int IDebugEngineLaunch2.TerminateProcess(IDebugProcess2 process)
        {
            AssertMainThread();
            Debug.Assert(_events != null);
            Debug.Assert(_process != null);

            int processId = EngineUtils.GetProcessId(process);

            if (processId != _process.Id)
            {
                return(VSConstants.S_FALSE);
            }

            _process.Terminate();

            return(VSConstants.S_OK);
        }
예제 #2
0
        // Resume a process launched by IDebugEngineLaunch2.LaunchSuspended
        int IDebugEngineLaunch2.ResumeProcess(IDebugProcess2 process)
        {
            Debug.WriteLine("Python Debugger ResumeProcess Begin");

            AssertMainThread();
            if (_events == null)
            {
                // process failed to start
                Debug.WriteLine("ResumeProcess fails, no events");
                return(VSConstants.E_FAIL);
            }

            Debug.Assert(_events != null);
            Debug.Assert(_process != null);
            Debug.Assert(_process != null);
            Debug.Assert(_ad7ProgramId == Guid.Empty);

            int processId = EngineUtils.GetProcessId(process);

            if (processId != _process.Id)
            {
                Debug.WriteLine("ResumeProcess fails, wrong process");
                return(VSConstants.S_FALSE);
            }

            // Send a program node to the SDM. This will cause the SDM to turn around and call IDebugEngine2.Attach
            // which will complete the hookup with AD7
            IDebugPort2 port;

            EngineUtils.RequireOk(process.GetPort(out port));

            IDebugDefaultPort2 defaultPort = (IDebugDefaultPort2)port;

            IDebugPortNotify2 portNotify;

            EngineUtils.RequireOk(defaultPort.GetPortNotify(out portNotify));

            EngineUtils.RequireOk(portNotify.AddProgramNode(new AD7ProgramNode(_process.Id)));

            if (_ad7ProgramId == Guid.Empty)
            {
                Debug.WriteLine("ResumeProcess fails, empty program guid");
                Debug.Fail("Unexpected problem -- IDebugEngine2.Attach wasn't called");
                return(VSConstants.E_FAIL);
            }

            // wait for the load event to complete, and pump messages

            while (!_process.HasExited && !_loadComplete.WaitOne(100))
            {
                Debug.WriteLine("ResumeProcess waiting for load complete");
            }

            // Resume the threads in the debuggee process
            if (_process.HasExited)
            {
                Debug.WriteLine("ResumeProcess resume all");
                _process.Resume();
            }
            else
            {
                // return failure?
                Debug.WriteLine("Process exited");
            }

            Debug.WriteLine("ResumeProcess return S_OK");
            return(VSConstants.S_OK);
        }
예제 #3
0
        // Attach the debug engine to a program.
        int IDebugEngine2.Attach(IDebugProgram2[] rgpPrograms, IDebugProgramNode2[] rgpProgramNodes, uint celtPrograms, IDebugEventCallback2 ad7Callback, enum_ATTACH_REASON dwReason)
        {
            Debug.WriteLine("PythonEngine Attach Begin " + GetHashCode());

            AssertMainThread();
            Debug.Assert(_ad7ProgramId == Guid.Empty);

            if (celtPrograms != 1)
            {
                Debug.Fail("Python debugging only supports one program in a process");
                throw new ArgumentException();
            }

            int processId = EngineUtils.GetProcessId(rgpPrograms[0]);

            if (processId == 0)
            {
                // engine only supports system processes
                Debug.WriteLine("PythonEngine failed to get process id during attach");
                return(VSConstants.E_NOTIMPL);
            }

            EngineUtils.RequireOk(rgpPrograms[0].GetProgramId(out _ad7ProgramId));

            // Attach can either be called to attach to a new process, or to complete an attach
            // to a launched process
            if (_process == null)
            {
                // TODO: Where do we get the language version from?
                _events = ad7Callback;

                var attachRes = PythonProcess.TryAttach(processId, out _process);
                if (attachRes != ConnErrorMessages.None)
                {
                    string msg;
                    switch (attachRes)
                    {
                    case ConnErrorMessages.CannotInjectThread: msg = "Cannot create thread in debuggee process"; break;

                    case ConnErrorMessages.CannotOpenProcess: msg = "Cannot open process for debugging"; break;

                    case ConnErrorMessages.InterpreterNotInitialized: msg = "Python interpreter has not been initialized in this process"; break;

                    case ConnErrorMessages.LoadDebuggerBadDebugger: msg = "Failed to load debugging script (incorrect version of script?)"; break;

                    case ConnErrorMessages.LoadDebuggerFailed: msg = "Failed to compile debugging script"; break;

                    case ConnErrorMessages.OutOfMemory: msg = "Out of memory"; break;

                    case ConnErrorMessages.PythonNotFound: msg = "Python interpreter not found"; break;

                    case ConnErrorMessages.TimeOut: msg = "Timeout while attaching"; break;

                    case ConnErrorMessages.UnknownVersion: msg = "Unknown Python version loaded in process"; break;

                    case ConnErrorMessages.SysNotFound: msg = "sys module not found"; break;

                    case ConnErrorMessages.SysSetTraceNotFound: msg = "settrace not found in sys module"; break;

                    case ConnErrorMessages.PyDebugAttachNotFound: msg = "Cannot find PyDebugAttach.dll at " + attachRes; break;

                    default: msg = "Unknown error"; break;
                    }

                    MessageBox.Show("Failed to attach debugger: " + msg);
                    return(VSConstants.E_FAIL);
                }

                AttachEvents(_process);
                _attached = true;
            }
            else
            {
                if (processId != _process.Id)
                {
                    Debug.Fail("Asked to attach to a process while we are debugging");
                    return(VSConstants.E_FAIL);
                }
                _attached = false;
            }

            AD7EngineCreateEvent.Send(this);

            lock (_syncLock) {
                _programCreated = true;
                AD7ProgramCreateEvent.Send(this);

                if (_processLoadedThread != null)
                {
                    SendLoadComplete(_processLoadedThread);
                }
            }

            Debug.WriteLine("PythonEngine Attach returning S_OK");
            return(VSConstants.S_OK);
        }