コード例 #1
0
        /// <summary>
        /// Opens a dump file for debugging.
        /// </summary>
        /// <param name="dumpFile">Path to a dump file to process.</param>
        /// <param name="debuggerInformation">An SimpleDebugger instance which contains connection information. Call Dispose() on this object.</param>
        /// <returns>HRESULT</returns>
        public static int OpenDumpFile(string dumpFile, out SimpleDebugger debuggerInformation)
        {
            IDebugClient debugClient = null;
            int          hr          = DebugCreate_IDebugClient(typeof(IDebugClient).GUID, out debugClient);

            if (FAILED(hr))
            {
                debuggerInformation = null;
                return(hr);
            }

            DebugUtilities debugUtilities = new DebugUtilities(debugClient);

            hr = ConnectDebuggerDumpHelper(debugUtilities, dumpFile, out debuggerInformation);
            if (FAILED(hr))
            {
                goto Error;
            }

            goto Exit;

Error:
            if (debugClient != null)
            {
                ReleaseComObjectSafely(debugClient);
            }
            debuggerInformation = null;
Exit:
            return(hr);
        }
コード例 #2
0
        private static int ConnectDebuggerDumpHelper(DebugUtilities debugUtilities, string dumpFile, out SimpleDebugger debuggerInformation)
        {
            int hr;

            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;
            }

            hr = debugUtilities.DebugClient.OpenDumpFileWide(dumpFile, 0);
            if (FAILED(hr))
            {
                goto ErrorWithDetach;
            }

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

            debuggerInformation = new SimpleDebugger(debugUtilities, 0, true, 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);
        }
コード例 #3
0
        /// <summary>
        /// Create a debugging connection to a process
        /// </summary>
        /// <param name="processID">The ID of the process to attach to</param>
        /// <param name="passive">Whether the debugger connection should be passive</param>
        /// <param name="debuggerInformation">An SimpleDebugger instance which contains connection information. Call Dispose() on this object.</param>
        /// <returns>HRESULT of the creation process</returns>
        public static int ConnectDebugger(uint processID, bool passive, out SimpleDebugger debuggerInformation)
        {
            IDebugClient debugClient = null;
            int          hr          = DebugCreate_IDebugClient(typeof(IDebugClient).GUID, out debugClient);

            if (FAILED(hr))
            {
                debuggerInformation = null;
                return(hr);
            }
            else
            {
                DebugUtilities debugUtilities = new DebugUtilities(debugClient);
                return(ConnectDebuggerLiveHelper(debugUtilities, processID, passive, out debuggerInformation));
            }
        }
コード例 #4
0
        /// <summary>
        /// Create a debugging connection to a process
        /// </summary>
        /// <param name="processName">The name of the process to attach to</param>
        /// <param name="passive">Whether the debugger connection should be passive</param>
        /// <param name="debuggerInformation">An SimpleDebugger instance which contains connection information. Call Dispose() on this object.</param>
        /// <returns>HRESULT of the creation process</returns>
        public static int ConnectDebugger(string processName, bool passive, out SimpleDebugger debuggerInformation)
        {
            IDebugClient debugClient = null;
            int          hr          = DebugCreate_IDebugClient(typeof(IDebugClient).GUID, out debugClient);

            if (FAILED(hr))
            {
                debuggerInformation = null;
                return(hr);
            }

            uint processID = 0;

            hr = debugClient.GetRunningProcessSystemIdByExecutableName(0, processName, 0, out processID);
            if (FAILED(hr))
            {
                goto Error;
            }

            DebugUtilities debugUtilities = new DebugUtilities(debugClient);

            hr = ConnectDebuggerLiveHelper(debugUtilities, processID, passive, out debuggerInformation);
            if (FAILED(hr))
            {
                goto Error;
            }

            goto Exit;

Error:
            if (debugClient != null)
            {
                ReleaseComObjectSafely(debugClient);
            }
            debuggerInformation = null;
Exit:
            return(hr);
        }
コード例 #5
0
        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);
        }