如何展开堆栈以获取指定堆栈指针(SP)的回溯?

Vio*_*ffe 29 c++ linux arm backtrace android-ndk

我正在为Android(仅限ARM)编写这个,但我相信通用Linux的原理也是如此.

我正在尝试从信号处理程序中捕获堆栈跟踪,以便我可以在应用程序崩溃时记录它.这就是我想出来的用法<unwind.h>.
初始化:

struct sigaction signalhandlerDescriptor;
memset(&signalhandlerDescriptor, 0, sizeof(signalhandlerDescriptor));
signalhandlerDescriptor.sa_flags = SA_SIGINFO;
signalhandlerDescriptor._u._sa_sigaction = signalHandler;
sigaction(SIGSEGV, &signalhandlerDescriptor, 0);
Run Code Online (Sandbox Code Playgroud)

代码本身:

struct BacktraceState
{
    void** current;
    void** end;
    void* pc;
};

inline _Unwind_Reason_Code unwindCallback(struct _Unwind_Context* context, void* arg)
{
    BacktraceState* state = static_cast<BacktraceState*>(arg);
    state->pc = (void*)_Unwind_GetIP(context);
    if (state->pc)
    {
        if (state->current == state->end)
            return _URC_END_OF_STACK;
        else
            *state->current++ = reinterpret_cast<void*>(state->pc);
    }
    return _URC_NO_REASON;
}

inline size_t captureBacktrace(void** addrs, size_t max, unsigned long pc)
{
    BacktraceState state = {addrs, addrs + max, (void*)pc};
    _Unwind_Backtrace(unwindCallback, &state);
    personality_routine();

    return state.current - addrs;
}

inline void dumpBacktrace(std::ostream& os, void** addrs, size_t count)
{
    for (size_t idx = 0; idx < count; ++idx) {
        const void* addr = addrs[idx];
        const char* symbol = "";

        Dl_info info;
        if (dladdr(addr, &info) && info.dli_sname) {
            symbol = info.dli_sname;
        }

        int status = -3;
        char * demangledName = abi::__cxa_demangle(symbol, 0, 0, &status);
        os << "#" << idx << ": " << addr << "  " << (status == 0 ? demangledName : symbol) << "\n";
        free(demangledName);
    }
}

void signalHandler(int sig, siginfo_t *siginfo, void *uctx)
{
    ucontext * context = (ucontext*)uctx;
    unsigned long PC = context->uc_mcontext.arm_pc;
    unsigned long SP = context->uc_mcontext.arm_sp;

    Logger() << __PRETTY_FUNCTION__ << "Fatal signal:" << sig;
    const size_t maxNumAddresses = 50;
    void* addresses[maxNumAddresses];
    std::ostringstream oss;

    const size_t actualNumAddresses = captureBacktrace(addresses, maxNumAddresses, PC);
    dumpBacktrace(oss, addresses, actualNumAddresses);
    Logger() << oss.str();
    exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)

问题:如果我通过调用获得PC寄存器_Unwind_GetIP(context)unwindCallback,我得到了完整的曲线为信号处理器堆栈.这是一个单独的堆栈,这显然不是我想要的.所以我尝试提供从ucontextin信号处理程序中获取的PC ,并得到了一个奇怪的结果:我得到一个堆栈条目,它是正确的条目 - 首先导致信号的函数.但它记录了两次(即使地址是相同的,所以它不是一个符号名称查找bug).显然,这还不够好 - 我需要整个堆栈.我想知道这个结果是否只是偶然的(即它不应该是一般的.

现在,我读到我需要提供堆栈指针,我显然可以从ucontextPC获得.但我不知道该怎么做.我是否必须手动展开而不是使用_Unwind_Backtrace?如果是这样,你能给我样例代码吗?我一直在寻找一天中最好的部分,但仍然找不到任何我可以复制并粘贴到我的项目中的东西.

对于它的价值,这里是包含定义的libunwind源代码_Unwind_Backtrace.如果我看到它的来源,我觉得我可以想出一些东西,但它比我想象的要复杂得多.

Ale*_*kov 5

为了获取导致 SIGSEGV 的代码的堆栈跟踪而不是信号处理程序的堆栈跟踪,您必须从中获取 ARM 寄存器ucontext_t并使用它们进行展开。

但这很难做到_Unwind_Backtrace()。因此,如果您使用 libc++ (LLVM STL) 并针对 32 位 ARM 进行编译,最好尝试libunwind与现代 Android NDK 捆绑在一起的预编译(位于sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a)。这是示例代码。


// This method can only be used on 32-bit ARM with libc++ (LLVM STL).
// Android NDK r16b contains "libunwind.a" for armeabi-v7a ABI.
// This library is even silently linked in by the ndk-build,
// so we don't have to add it manually in "Android.mk".
// We can use this library, but we need matching headers,
// namely "libunwind.h" and "__libunwind_config.h".
// For NDK r16b, the headers can be fetched here:
// https://android.googlesource.com/platform/external/libunwind_llvm/+/ndk-r16/include/
#if _LIBCPP_VERSION && __has_include("libunwind.h")
#include "libunwind.h"
#endif

struct BacktraceState {
    const ucontext_t*   signal_ucontext;
    size_t              address_count = 0;
    static const size_t address_count_max = 30;
    uintptr_t           addresses[address_count_max] = {};

    BacktraceState(const ucontext_t* ucontext) : signal_ucontext(ucontext) {}

    bool AddAddress(uintptr_t ip) {
        // No more space in the storage. Fail.
        if (address_count >= address_count_max)
            return false;

        // Reset the Thumb bit, if it is set.
        const uintptr_t thumb_bit = 1;
        ip &= ~thumb_bit;

        // Ignore null addresses.
        if (ip == 0)
            return true;

        // Finally add the address to the storage.
        addresses[address_count++] = ip;
        return true;
    }
};

void CaptureBacktraceUsingLibUnwind(BacktraceState* state) {
    assert(state);

    // Initialize unw_context and unw_cursor.
    unw_context_t unw_context = {};
    unw_getcontext(&unw_context);
    unw_cursor_t  unw_cursor = {};
    unw_init_local(&unw_cursor, &unw_context);

    // Get more contexts.
    const ucontext_t* signal_ucontext = state->signal_ucontext;
    assert(signal_ucontext);
    const sigcontext* signal_mcontext = &(signal_ucontext->uc_mcontext);
    assert(signal_mcontext);

    // Set registers.
    unw_set_reg(&unw_cursor, UNW_ARM_R0, signal_mcontext->arm_r0);
    unw_set_reg(&unw_cursor, UNW_ARM_R1, signal_mcontext->arm_r1);
    unw_set_reg(&unw_cursor, UNW_ARM_R2, signal_mcontext->arm_r2);
    unw_set_reg(&unw_cursor, UNW_ARM_R3, signal_mcontext->arm_r3);
    unw_set_reg(&unw_cursor, UNW_ARM_R4, signal_mcontext->arm_r4);
    unw_set_reg(&unw_cursor, UNW_ARM_R5, signal_mcontext->arm_r5);
    unw_set_reg(&unw_cursor, UNW_ARM_R6, signal_mcontext->arm_r6);
    unw_set_reg(&unw_cursor, UNW_ARM_R7, signal_mcontext->arm_r7);
    unw_set_reg(&unw_cursor, UNW_ARM_R8, signal_mcontext->arm_r8);
    unw_set_reg(&unw_cursor, UNW_ARM_R9, signal_mcontext->arm_r9);
    unw_set_reg(&unw_cursor, UNW_ARM_R10, signal_mcontext->arm_r10);
    unw_set_reg(&unw_cursor, UNW_ARM_R11, signal_mcontext->arm_fp);
    unw_set_reg(&unw_cursor, UNW_ARM_R12, signal_mcontext->arm_ip);
    unw_set_reg(&unw_cursor, UNW_ARM_R13, signal_mcontext->arm_sp);
    unw_set_reg(&unw_cursor, UNW_ARM_R14, signal_mcontext->arm_lr);
    unw_set_reg(&unw_cursor, UNW_ARM_R15, signal_mcontext->arm_pc);

    unw_set_reg(&unw_cursor, UNW_REG_IP, signal_mcontext->arm_pc);
    unw_set_reg(&unw_cursor, UNW_REG_SP, signal_mcontext->arm_sp);

    // unw_step() does not return the first IP.
    state->AddAddress(signal_mcontext->arm_pc);

    // Unwind frames one by one, going up the frame stack.
    while (unw_step(&unw_cursor) > 0) {
        unw_word_t ip = 0;
        unw_get_reg(&unw_cursor, UNW_REG_IP, &ip);

        bool ok = state->AddAddress(ip);
        if (!ok)
            break;
    }
}

void SigActionHandler(int sig, siginfo_t* info, void* ucontext) {
    const ucontext_t* signal_ucontext = (const ucontext_t*)ucontext;
    assert(signal_ucontext);

    BacktraceState backtrace_state(signal_ucontext);
    CaptureBacktraceUsingLibUnwind(&backtrace_state);
    // Do something with the backtrace - print, save to file, etc.
}
Run Code Online (Sandbox Code Playgroud)

这是一个示例回溯测试应用程序,其中包含 3 种实现的回溯方法,包括上面所示的方法。

https://github.com/alexeikh/android-ndk-backtrace-test


And*_*nle 0

首先,您需要阅读“异步信号安全”函数部分:

http://man7.org/linux/man-pages/man7/signal.7.html

这是在信号处理程序中可以安全调用的整套函数。最糟糕的事情就是在幕后调用任何调用 malloc()/free() 的东西 - 或者自己做。

其次,首先让它在信号处理程序之外工作。

第三,这些可能是恰当的:

如何在 Android 上获取 C++ 回溯

Android NDK:获取回溯

  • **1.** 不,最糟糕的是应用程序将死锁并且永远不会退出,除非有明确的东西杀死它。有无数种方法可以避免调用 malloc(),甚至是间接的。**2.** 您需要使用 ucontext 起点让它工作。**3.** 如果您确实需要堆栈回溯,只需运行 `system( "pstack PID" );` 即可发出当前堆栈跟踪。虽然 system() 本身并不是异步信号安全的,但它通常构建在 fork()/exec() 之上,而 fork()/exec() 是异步信号安全的。或者你可以推出自己的`fork()/exec()`。 (3认同)

归档时间:

查看次数:

4459 次

最近记录:

7 年,4 月 前