windows / c ++:如何从信号处理程序中获取有用的堆栈跟踪?

时间:2015-01-22 22:40:00

标签: c++ windows multithreading winapi juce

我有一个Windows / C ++应用程序(使用JUCE),我想在应用程序崩溃时将堆栈跟踪转储到文件中。在我的初始化代码中,我得到了:

signal(SIGABRT, abortHandler);
signal(SIGSEGV, abortHandler);
signal(SIGILL, abortHandler);
signal(SIGFPE, abortHandler);

然后我的处理程序看起来像:

void abortHandler(int signum)
{
    juce::File log("stacktrace.txt");

    log.appendText(juce::SystemStats::getStackBacktrace());

    exit(signum);
}

但是,生成的堆栈跟踪不是发生崩溃的线程:

0: AudulusDebug32: juce::SystemStats::getStackBacktrace + 0x7f
1: AudulusDebug32: abortHandler + 0x61
2: AudulusDebug32: _XcptFilter + 0x1e3
3: AudulusDebug32: __tmainCRTStartup + 0x15f
4: AudulusDebug32: WinMainCRTStartup + 0xd
5: BaseThreadInitThunk + 0xe
6: RtlInitializeExceptionChain + 0x84
7: RtlInitializeExceptionChain + 0x5a

在内部,getStackBacktrace执行以下操作:

    HANDLE process = GetCurrentProcess();
    SymInitialize (process, nullptr, TRUE);

    void* stack[128];
    int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);

有没有办法让崩溃发生的线程(或所有线程)得到堆栈跟踪?

1 个答案:

答案 0 :(得分:7)

正如@HansPassant建议的那样,我的解决方案是使用:

SetUnhandledExceptionFilter(TopLevelExceptionHandler);

TopLevelExceptionHandler中,我使用CaptureStackBackTrace而不是调用StackWalk64,这允许您指定要走的是哪个堆栈(而不是假设当前堆栈)。< / p>

以下是代码:

LONG WINAPI TopLevelExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
    std::ofstream f;
    f.open("stacktrace.txt", std::ios::out | std::ios::trunc);

    HANDLE process = GetCurrentProcess();
    SymInitialize(process, NULL, TRUE);

    // StackWalk64() may modify context record passed to it, so we will
    // use a copy.
    CONTEXT context_record = *pExceptionInfo->ContextRecord;
    // Initialize stack walking.
    STACKFRAME64 stack_frame;
    memset(&stack_frame, 0, sizeof(stack_frame));
    #if defined(_WIN64)
    int machine_type = IMAGE_FILE_MACHINE_AMD64;
    stack_frame.AddrPC.Offset = context_record.Rip;
    stack_frame.AddrFrame.Offset = context_record.Rbp;
    stack_frame.AddrStack.Offset = context_record.Rsp;
    #else
    int machine_type = IMAGE_FILE_MACHINE_I386;
    stack_frame.AddrPC.Offset = context_record.Eip;
    stack_frame.AddrFrame.Offset = context_record.Ebp;
    stack_frame.AddrStack.Offset = context_record.Esp;
    #endif
    stack_frame.AddrPC.Mode = AddrModeFlat;
    stack_frame.AddrFrame.Mode = AddrModeFlat;
    stack_frame.AddrStack.Mode = AddrModeFlat;

    juce::HeapBlock<SYMBOL_INFO> symbol;
    symbol.calloc(sizeof(SYMBOL_INFO) + 256, 1);
    symbol->MaxNameLen = 255;
    symbol->SizeOfStruct = sizeof(SYMBOL_INFO);

    while (StackWalk64(machine_type,
        GetCurrentProcess(),
        GetCurrentThread(),
        &stack_frame,
        &context_record,
        NULL,
        &SymFunctionTableAccess64,
        &SymGetModuleBase64,
        NULL)) {

        DWORD64 displacement = 0;

        if (SymFromAddr(process, (DWORD64)stack_frame.AddrPC.Offset, &displacement, symbol))
        {
            IMAGEHLP_MODULE64 moduleInfo;
            juce::zerostruct(moduleInfo);
            moduleInfo.SizeOfStruct = sizeof(moduleInfo);

            if (::SymGetModuleInfo64(process, symbol->ModBase, &moduleInfo))
                f << moduleInfo.ModuleName << ": ";

            f << symbol->Name << " + 0x" << String::toHexString((juce::int64)displacement) << std::endl;
        }

    }

    return EXCEPTION_CONTINUE_SEARCH;
}