示例#1
0
        private void WaitForDebugEvents()
        {
            using (var miniDumper = CreateMiniDumper()) {
                if (_options.NoDumpOptionSelected)
                {
                    miniDumper.DumpWithoutReason();
                    DetachProcess();
                    return;
                }

                while (!_detached)
                {
                    var debugEvent = WaitForDebugEvent(1000);
                    if (_shouldDeatch)
                    {
                        DetachProcess();
                        return;
                    }
                    if (debugEvent.HasValue)
                    {
                        switch (debugEvent.Value.dwDebugEventCode)
                        {
                        case DEBUG_EVENT_CODE.EXIT_PROCESS_DEBUG_EVENT:
                            if (_options.DumpOnProcessTerminate)
                            {
                                miniDumper.DumpOnProcessExit(debugEvent.Value.ExitProcess.dwExitCode);
                            }
                            _shouldDeatch = true;
                            break;

                        case DEBUG_EVENT_CODE.EXCEPTION_DEBUG_EVENT:
                            var exception = debugEvent.Value.Exception;
                            if (_options.DumpOnException == 1 && exception.dwFirstChance == 1 ||
                                _options.DumpOnException == 2 && exception.dwFirstChance == 0)
                            {
                                miniDumper.DumpOnException((uint)debugEvent.Value.dwThreadId, exception.ExceptionRecord);
                            }
                            break;

                        case DEBUG_EVENT_CODE.OUTPUT_DEBUG_STRING_EVENT:
                            if (_options.Verbose)
                            {
                                miniDumper.PrintDebugString(debugEvent.Value.DebugString);
                            }
                            break;

                        default:
                            break;
                        }
                        if (!_shouldDeatch && miniDumper.NumberOfDumpsTaken >= _options.NumberOfDumps)
                        {
                            Console.WriteLine("Number of dumps exceeded the specified limit - detaching.");
                            _shouldDeatch = true;
                        }
                        if (_shouldDeatch)
                        {
                            DetachProcess();
                            return;
                        }
                        if (_detached)
                        {
                            return;
                        }
                        var continueStatus = HandleDebugEvent(debugEvent.Value);
                        if (!DebuggingNativeMethods.ContinueDebugEvent(debugEvent.Value.dwProcessId,
                                                                       debugEvent.Value.dwThreadId, continueStatus))
                        {
                            throw new LastWin32ErrorException("Error in ContinueDebugEvent");
                        }
                    }
                }
            }
        }