use callback function to report stack backtrace
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 functi开发者_开发知识库on where the error occured and then back trace through the stack to identify each calling function by address, name, and offset?
http://linux.die.net/man/3/backtrace_symbols
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.
精彩评论