更新:
似乎创建了一个patch,以支持x86和ARM(XScale)的uclibc上的backtrace(),它使用__libc_stack_end符号.
原始答案:
我在一个项目中,我们使用的glibc版本没有为我们的ARM处理器提供功能性的backtrace(),所以我们使用__libc_stack_end符号开发了我们自己的外部glibc.以下是结果代码.也许你可以用它来写一个uclibc的backtrace()函数.
extern void * __libc_stack_end;
struct backtrace_frame_t
{
void * fp;
void * sp;
void * lr;
void * pc;
};
int backtrace(void ** array, int size)
{
void * top_frame_p;
void * current_frame_p;
struct backtrace_frame_t * frame_p;
int frame_count;
top_frame_p = __builtin_frame_address(0);
current_frame_p = top_frame_p;
frame_p = (struct backtrace_frame_t*)((void**)(current_frame_p)-3);
frame_count = 0;
if (__builtin_return_address(0) != frame_p->lr)
{
fprintf(stderr, "backtrace error: __builtin_return_address(0) != frame_p->lr\n");
return frame_count;
}
if (current_frame_p != NULL
&& current_frame_p > (void*)&frame_count
&& current_frame_p < __libc_stack_end)
{
while (frame_count < size
&& current_frame_p != NULL
&& current_frame_p > (void*)&frame_count
&& current_frame_p < __libc_stack_end)
{
frame_p = (struct backtrace_frame_t*)((void**)(current_frame_p)-3);
array[frame_count] = frame_p->lr;
frame_count++;
current_frame_p = frame_p->fp;
}
}
return frame_count;
}
注意:__libc_stack_end符号不再在glibc的更新版本中导出,我不知道它在uclibc中的存在或类似的符号.