Exemple #1
0
        public DbgEngDataReader(int pid, AttachFlag flags, uint msecTimeout)
        {
            IDebugClient client = CreateIDebugClient();

            CreateClient(client);

            DEBUG_ATTACH attach = (flags == AttachFlag.Invasive) ? DEBUG_ATTACH.DEFAULT : DEBUG_ATTACH.NONINVASIVE;
            int          hr     = m_control.AddEngineOptions(DEBUG_ENGOPT.INITIAL_BREAK);

            if (hr == 0)
            {
                hr = client.AttachProcess(0, (uint)pid, attach);
            }

            if (hr == 0)
            {
                hr = m_control.WaitForEvent(0, msecTimeout);
            }

            if (hr == 1)
            {
                throw new TimeoutException("Break in did not occur within the allotted timeout.");
            }
            else if (hr != 0)
            {
                throw new ClrDiagnosticsException(String.Format("Could not attach to pid {0:X}, HRESULT: 0x{1:x8}", pid, hr), ClrDiagnosticsException.HR.DebuggerError);
            }
        }
Exemple #2
0
        public DbgEngDataReader(int pid, AttachFlag flags, uint msecTimeout)
        {
            IDebugClient client = CreateIDebugClient();

            CreateClient(client);

            DEBUG_ATTACH attach = flags == AttachFlag.Invasive ? DEBUG_ATTACH.DEFAULT : DEBUG_ATTACH.NONINVASIVE;
            int          hr     = _control.AddEngineOptions(DEBUG_ENGOPT.INITIAL_BREAK);

            if (hr == 0)
            {
                hr = client.AttachProcess(0, (uint)pid, attach);
            }

            if (hr == 0)
            {
                hr = _control.WaitForEvent(0, msecTimeout);
            }

            if (hr == 1)
            {
                throw new TimeoutException("Break in did not occur within the allotted timeout.");
            }

            if (hr != 0)
            {
                if ((uint)hr == 0xd00000bb)
                {
                    throw new InvalidOperationException("Mismatched architecture between this process and the target process.");
                }

                throw new ClrDiagnosticsException($"Could not attach to pid {pid:X}, HRESULT: 0x{hr:x8}", ClrDiagnosticsExceptionKind.DebuggerError, hr);
            }
        }
        private static int ConnectDebuggerLiveHelper(DebugUtilities debugUtilities, uint processID, bool passive, out SimpleDebugger debuggerInformation)
        {
            int hr;

            IDebugControl       debugControl       = null;
            IDebugSystemObjects debugSystemObjects = null;

            SimpleOutputHandler debuggerOutputCallbacks = null;
            SimpleEventHandler  debuggerEventCallbacks  = null;

            debuggerInformation = null;

            hr = SimpleOutputHandler.Install(debugUtilities, out debuggerOutputCallbacks);
            if (hr != S_OK)
            {
                goto Error;
            }

            hr = SimpleEventHandler.Install(debugUtilities, out debuggerEventCallbacks);
            if (hr != S_OK)
            {
                goto ErrorWithDetach;
            }

            DEBUG_ATTACH attachFlags = passive ? DEBUG_ATTACH.NONINVASIVE | DEBUG_ATTACH.NONINVASIVE_NO_SUSPEND : DEBUG_ATTACH.INVASIVE_RESUME_PROCESS;

            hr = debugUtilities.DebugClient.AttachProcess(0, processID, attachFlags);
            if (hr != S_OK)
            {
                goto ErrorWithDetach;
            }

            while (debuggerEventCallbacks.SessionIsActive == false)
            {
                hr = debugControl.WaitForEvent(DEBUG_WAIT.DEFAULT, 50);
                if (FAILED(hr))
                {
                    goto ErrorWithDetach;
                }
            }

            bool foundMatchingProcess = false;
            uint numProcesses;

            debugSystemObjects.GetNumberProcesses(out numProcesses);
            uint[] systemProcessIDs = new uint[numProcesses];
            uint[] engineProcessIDs = new uint[numProcesses];
            hr = debugSystemObjects.GetProcessIdsByIndex(0, numProcesses, engineProcessIDs, systemProcessIDs);
            for (uint i = 0; i < numProcesses; ++i)
            {
                if (systemProcessIDs[i] == processID)
                {
                    foundMatchingProcess = true;
                    hr = debugSystemObjects.SetCurrentProcessId(engineProcessIDs[i]);
                    if (FAILED(hr))
                    {
                        debuggerOutputCallbacks.AddNoteLine(String.Format(CultureInfo.InvariantCulture, "ERROR! Failed to set the active process! hr={0:x8}", hr));
                        goto ErrorWithDetach;
                    }
                    break;
                }
            }
            if (foundMatchingProcess == false)
            {
                hr = E_FAIL;
                debuggerOutputCallbacks.AddNoteLine(String.Format(CultureInfo.InvariantCulture, "ERROR! The debugger engine could not find the requested process ID ({0})!", processID));
                goto ErrorWithDetach;
            }

            debuggerInformation = new SimpleDebugger(debugUtilities, processID, passive, debuggerOutputCallbacks, debuggerEventCallbacks);

            goto Exit;

ErrorWithDetach:
            debugUtilities.DebugClient.DetachProcesses();
            debugUtilities.DebugClient.EndSession(DEBUG_END.ACTIVE_DETACH);
Error:
            if (debuggerEventCallbacks != null)
            {
                debuggerEventCallbacks.Dispose();
            }
            if (debuggerOutputCallbacks != null)
            {
                debuggerOutputCallbacks.Dispose();
            }
Exit:
            return(hr);
        }