use callback function to report stack backtrace

2019-02-20 15:46发布

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?

2条回答
Melony?
3楼-- · 2019-02-20 16:20

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.

查看更多
登录 后发表回答