use callback function to report stack backtrace

旧巷老猫 提交于 2019-12-19 11:19:57

问题


Assume I have the following:

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);
}

I have this so far, but what is the best way using the fnSymbols static global pointer to identify the function where the error occured and then back trace through the stack to identify each calling function by address, name, and offset?


回答1:


http://linux.die.net/man/3/backtrace_symbols




回答2:


On Linux, search for tool named addr2line.

Your application would have to be compiled with -rdynamic option. The following:

addr2line 0x8048a76 -f -e app_name

outputs the function name and also the line number on the source code.



来源:https://stackoverflow.com/questions/2933697/use-callback-function-to-report-stack-backtrace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!