我正试图在我的Android应用程序中捕捉诸如SIGSEGV这样的信号,以便进行调试。为此,我设置了一个名为“sigaction”的操作。
我现在正试着去接电话。问题是_Unwind_Backtrace
只在当前堆栈上工作,sigaction在自己的堆栈中运行。
那么,是否有一种方法可以获得接收信号的执行指针的堆栈?(基本上告诉_Unwind_Backtrace
解除另一个堆栈,而不是当前堆栈?)
我应该指出:
backtrace()
和backtrace_symbols()
不是一种选择,因为这些函数不是在Android中交付的。编辑:我尝试使用由fadden提出的系统/核心系统的libcork螺杆,但是当我使用它的unwind_backtrace_signal_arch函数时,我得到了一个奇怪的反向跟踪,它并不代表崩溃。
发布于 2013-08-02 17:22:14
您可以使用pthread_getattr_np
和pthread_attr_getstack
获得堆栈基地址,但您真正需要的是崩溃时的PC和SP。在Linux上,您可以将这些从ucontext
中提取出来。
如果在配置信号处理程序时设置SA_SIGINFO
标志,则处理程序函数将获得三个参数而不是一个参数。第三个void*
参数是一个ucontext
指针。对这个问题的公认答案解释得更多一些。
一旦你有了所有的东西,你就可以展开堆栈了。如果您不介意超出NDK提供的范围,那么Android的螺丝钉有可以释放堆栈并输出结果的功能。调试器守护进程使用它将本机崩溃转储到日志文件。
了解由调试器记录的本机崩溃在/data/tombstones/
中生成堆栈转储可能是有用的。文件权限使普通应用程序无法访问该文件,但在修改后的设备上,您只需将这些文件取出并发送。
发布于 2015-05-28 19:41:58
在我的实践中,标准_Unwind_Backtrace未能切换到信号前堆栈。
我通过调用内部libgcc __gnu_Unwind_Backtrace (它有一个额外的agrument是“当前注册表值”)获得了一些前信号堆栈,因此它在给定的堆栈上运行,而不是在当前堆栈上操作。
//definitions copied from arm-specific libgcc 4.8 sources.
struct core_regs
{
_uw r[16];
};
typedef struct
{
_uw demand_save_flags;
struct core_regs core;
} phase2_vrs;
extern "C"
_Unwind_Reason_Code
__gnu_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument,
phase2_vrs * entry_vrs);
// Getting backtrace with those definitions
//istead of _Unwind_Backtrace(tracer, &state);
if (const ucontext_t* signal_context = last_sigaction_parameter)
{
phase2_vrs pre_signal_state = {};
pre_signal_state.core = *reinterpret_cast<const core_regs*>(&(signal_context->uc_mcontext.arm_r0));
__gnu_Unwind_Backtrace(tracer, &state, &pre_signal_state);
}
发布于 2018-04-25 17:44:49
为了获得导致SIGSEGV的代码堆栈跟踪,而不是信号处理程序的堆栈跟踪,您必须从ucontext_t
获取ARM寄存器并使用它们展开。
但是很难用_Unwind_Backtrace()
来实现。因此,如果您使用libc++ (LLVM ),最好尝试使用与现代Android (at sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a
)捆绑在一起的32位ARM预编译libunwind
。这是一个示例代码。
// 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://stackoverflow.com/a/50027799/1016580
如果使用libstdc++ (GNU ),请使用Vasily的解决方案:
https://stackoverflow.com/a/30515756/1016580
,这与Dar Hoo在另一篇文章中的解决方案相同:
https://stackoverflow.com/questions/18017222
复制相似问题