目的:
解析某个函数被调用时的调用栈
方式:
1.同步、同线程解析——成功解析
在目标函数内利用libunwind来实现,大概代码如下:
unw_cursor_t cursor;
unw_context_t context;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
while (unw_step(&cursor) > 0)
{
unw_word_t offset, pc;
char symbols[64] = {0};
unw_get_reg(&cursor, UNW_REG_IP, &pc);
unw_get_proc_name(&cursor, symbols, sizeof(symbols), &offset);
printf ("%lu : (%s+0x%lu) [%lu]\n", pc, symbols, offset, pc);
}
2.异步、异线程解析——失败
在目标进程A启动时create一个新的线程B,在A进程的目标函数Func内保存一些信息,然后A继续执行,B去解析A的调用栈;
A的代码大概如下:
int depth = 0;
while (unw_step(&cursor[depth]) > 0)
{
unw_get_reg(&cursor[depth], UNW_REG_IP, &pc[depth]);
depth++;
}
B的代码大概如下:
nt curDepth = 0;
char symbols[1024][64];
while(curDepth < depth)
{
unw_get_proc_name(&cursor[curDepth], symbols[curDepth], 64, &offset[curDepth]);
}
整体的思路大概就是通过异步、多线程的方式来减少解析调用栈的耗时。
但是libunwind的没有提供上述的实现机制,目前也没有很好的思路,不知道大家有没有什么好的建议?非常感谢!