#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
int dump_stack(void)
{
int j, nptrs;
void *buffer[16];
char ** strings;
nptrs = backtrace(buffer,16);
printf("backtrace() returned %d addresses\n",nptrs);
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
printf("error backtrace_symbols\n");
return -1;
}
for(j = 0; j<nptrs; j++)
printf("[%02d] %s\n",j,strings[j]);
free(strings);
return 0;
}
volatile void test_call_3(void)
{
printf("I am %s\n",__func__);
dump_stack();
printf("I exit %s\n",__func__);
}
volatile void test_call_2(void)
{
printf("I am %s\n",__func__);
test_call_3();
printf("I exit %s\n",__func__);
}
int main(int argc,char **argv)
{
test_call_2();
}