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

35

我正在为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);

代码本身:

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);
}
问题:如果我在unwindCallback中调用_Unwind_GetIP(context)来获取PC寄存器,我会得到完整的跟踪记录,但这是一个单独的堆栈,显然不是我想要的。因此,我尝试提供从信号处理程序中的ucontext获取的PC,结果很奇怪:我只得到一条堆栈记录,它是正确的记录——导致出现信号的函数。但是它被记录了两次(即使地址相同,所以这不是符号名称查找错误)。显然,这还不够好——我需要整个堆栈。我想知道这个结果是否仅仅是偶然的(即它通常不起作用)。
现在,我读到我还需要提供堆栈指针,我显然可以从ucontext中获取,和PC一样。但我不知道该怎么做。我必须手动展开而不是使用_Unwind_Backtrace吗?如果是这样,请给我一个示例代码?我已经搜索了一天,仍然找不到可以复制粘贴到我的项目中的任何东西。
就这个问题而言,这里是包含_Unwind_Backtrace定义的libunwind源代码。如果我看到了源代码,我想我可以找到一些解决方法,但它比我预期的要复杂得多。

顺便提一下,Dalvik虚拟机从其他线程收集本地堆栈跟踪的方式始于此:https://android.googlesource.com/platform/dalvik/+/kitkat-release/vm/interp/Stack.cpp#1389。实现是向其他线程发送信号以导致它们进行收集。 - fadden
@fadden:不幸的是,它使用了“corkscrew/backtrace.h”,这在NDK中不可用,而且我听说该库已从Android 5中删除。 - Violet Giraffe
1
这是一个几乎只有头文件的库,能够在出现分段错误时漂亮地打印出堆栈信息。https://github.com/vmarkovtsev/DeathHandler - Jens Munk
@JensMunk:谢谢,我会试一下!不过没有提到Android支持。我相信它在那里不起作用。我会在周一尝试并回报结果。 - Violet Giraffe
@violet giraffe,它已在ARM7和ARM9上使用了该库,并且符合POSIX标准。输出格式非常漂亮,带有颜色-非常适合捕捉崩溃中的重要信息,并且处理程序中没有进行内存分配(如上所述)。 - Jens Munk
显示剩余21条评论
3个回答

5
为了获得导致SIGSEGV的代码堆栈跟踪,而不是信号处理程序的堆栈跟踪,您需要从中获取ARM寄存器,并将它们用于展开。但是,使用_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.
}

这是一个带有三种实现的回溯方法的样例测试应用程序,其中包括上面展示的方法。 https://github.com/alexeikh/android-ndk-backtrace-test

0

1
  1. 谢谢。我知道调用malloc()是不安全的(以及做许多其他事情),但是没有办法避免它。此外,应用程序已经崩溃,最糟糕的情况就是我无法获得堆栈跟踪日志。这正是现在正在发生的事情。
  2. 它在信号处理程序之外确实有效,这是我测试的第一件事情(显然是通过_Unwind_GetIP获取的PC)。
  3. 第一个链接似乎不相关,第二个链接包含一个有用的答案,我的代码是基于它构建的。
- Violet Giraffe
3
  1. 不,最糟糕的是应用程序会死锁,并且除非有某种显式的方法终止它,否则它将永远无法退出。而且有无限多种方法来避免调用malloc(),即使是间接调用也可以避免。
  2. 你需要使用ucontext作为起点让它起作用。
  3. 如果你真的需要堆栈回溯信息,只需运行system("pstack PID");来发出当前堆栈跟踪。虽然system()本身不是异步信号安全的,但它通常是基于fork()/exec()构建的,而这些函数是异步信号安全的。或者你可以自己编写fork()/exec()函数。
- Andrew Henle
  1. 我明白,但是怎么做呢?
  2. 我会尝试,但我严重怀疑它需要 root 权限,这是不可接受的。
- Violet Giraffe
  1. 你可能需要使用google-breakpad来获取ucontext: https://code.google.com/p/google-breakpad/
  2. 我现在没有运行中的Android安装,但我认为你不需要root权限来pstack自己的进程。
- Andrew Henle
  1. 我已经多次查看了Breakpad,它看起来非常复杂,而且很难使其工作(过于繁琐)。
  2. 我的代码中已经有ucontext了。我可以从那里获取SP以及许多其他寄存器的值。但是我不知道如何使用它。我目前最好的想法是在AOSP源代码中找到_Unwind_Backtrace的源代码(这本身就是一个任务),将其代码提取到我的项目中(这不是最好的想法,因为它可能无法跨不同的Android平台版本移植),并将SP替换为从ucontext获取的SP。
- Violet Giraffe
这个可以完成任务。该库仅包含头文件,非常安全。https://github.com/vmarkovtsev/DeathHandler - Jens Munk

0
作为在 arm-linux-eabihf 上获取通过信号处理程序解除绑定(例如从一个信号处理程序抛出异常)的一部分,我还获得了从信号处理程序中获得工作返回跟踪。 我非常确定这是特定于 glibc 的,因此在 Android 上无法使用,但也许可以适应或用作灵感来源:https://github.com/mvduin/arm-signal-unwind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接