使用回调函数来报告堆栈回溯

假设我有以下内容:

typedef struct { char *name; char binding; int address; } Fn_Symbol //definition of function symbol static Fn_Symbol *fnSymbols; //array of function symbols in a file statc int total; //number of symbol functions in the array and file static void PrintBacktrace(int sigum, siginfo_t * siginfo, void *context) { printf("\nSignal received %d (%s)\n", signum, strsignal(signum)); const int eip_index = 14; void *eip = (void *)((struct ucontext *)context)->uc_mcontext.gregs[eip_index]; printf("Error at [%p] %s (+0x%x), eip, fnName, offset from start); //????? exit(0); } 

到目前为止我有这个,但是使用fnSymbols静态全局指针来识别发生错误的函数然后返回跟踪堆栈以通过地址,名称和偏移识别每个调用函数的最佳方法是什么?

在Linux上,搜索名为addr2line的工具。

您的应用程序必须使用-rdynamic选项进行编译。 下列:

 addr2line 0x8048a76 -f -e app_name 

输出函数名称以及源代码上的行号。