尝试使用__debugbreak()来/catch

try/catch with __debugbreak()

本文关键字:catch debugbreak      更新时间:2023-10-16

我正在使用在某些情况下运行__debugbreak()的第三方c++ DLL,并且在这样做之前没有检查IsDebuggerPresent()。当这种情况发生在调试器之外时(例如,最终用户运行应用程序),这会导致我的应用程序"崩溃"。我想抓住这个问题并自己处理它,或者至少忽略它。

我实际上有一个未处理的异常过滤器,用于将SEH转换为c++异常一段时间,所以它不工作有点奇怪。

::SetUnhandledExceptionFilter(OnUnhandledException);

我一直在做一些直接的测试,和标准的__try/__except工作,所以我可以包装每个调用到DLL与此作为回退,但似乎是,如果__try/__except工作,那么::SetUnhandledExceptionFilter()也应该工作。

    __try
    {
        __debugbreak();
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
        printf("caught");
    }

try/catch(…)不起作用

    try
    {
        __debugbreak();
    }
    catch (...)
    {
        printf("caught");
    }

_set_se_translator()也不能工作。

在https://msdn.microsoft.com/en-us/library/ms679297(VS.85).aspx的MSDN文档中,它指出它应该作为结构化异常运行。我意识到这是DebugBreak()的文档,但我已经测试过了,也有同样的问题,即使是"catch(…)"。

我正在编译/EHa。

我如何捕获__debugbreak (asm INT 3),或者至少改变行为?

断点生成EXCEPTION_BREAKPOINT结构化异常。你不能使用try/catch来捕获它,因为它不会被转换为c++异常,无论/EHa开关还是_set_se_translator

EXCEPTION_BREAKPOINT为特殊例外。

首先,你应该知道catch块和__except块只有在展开堆栈后才执行。这意味着在处理程序块之后继续执行,而不是在调用__debugbreak()之后。所以如果你只是想跳过EXCEPTION_BREAKPOINT,同时继续执行int 3指令。您应该使用向量异常处理程序。下面是一个例子:

// VEH is supported only on Windows XP+ and Windows Server 2003+
#define _WIN32_WINNT 0x05020000
#include <windows.h>
#include <stdio.h>
//AddVectoredExceptionHandler constants:
//CALL_FIRST means call this exception handler first;
//CALL_LAST means call this exception handler last
#define CALL_FIRST 1  
#define CALL_LAST 0
LONG WINAPI
VectoredHandlerBreakPoint(
struct _EXCEPTION_POINTERS *ExceptionInfo
    )
{
    if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
    {
        /*
        If a debugger is attached, this will never be executed.
        */
        printf("BreakPoint at 0x%x skipped.n", ExceptionInfo->ExceptionRecord->ExceptionAddress);
        PCONTEXT Context = ExceptionInfo->ContextRecord;
        // The breakpoint instruction is 0xCC (int 3), just one byte in size.
        // Advance to the next instruction. Otherwise, this handler will just be called ad infinitum.
#ifdef _AMD64_
        Context->Rip++;
#else
        Context->Eip++;
#endif    
        // Continue execution from the instruction at Context->Rip/Eip.
        return EXCEPTION_CONTINUE_EXECUTION;
    }
    // IT's not a break intruction. Continue searching for an exception handler.
    return EXCEPTION_CONTINUE_SEARCH;
}
void main()
{
    // Register the vectored exception handler once.
    PVOID hVeh = AddVectoredExceptionHandler(CALL_FIRST, VectoredHandlerBreakPoint);
    if (!hVeh)
    {
        // AddVectoredExceptionHandler failed.
        // Practically, this never happens.
    }
    DebugBreak();
    // Unregister the handler.
    if (hVeh)
        RemoveVectoredExceptionHandler(hVeh);
}

这样,断点指令int 3将被跳过,下一条指令将被执行。此外,如果附加了调试器,它将为您处理EXCEPTION_BREAKPOINT

然而,如果你真的想要展开堆栈,你必须使用__except(GetExceptionCode() == EXCEPTION_BREAKPOINT ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) .