Illustration: The following program explains the use of backtrace(): Just compile it and run you will get like:
main() => foo() => showBackTrace() with the virtual addresses.
/* Include execinfo.h header file for backtrace() functions */
#include < stdio.h >
#include < stdlib.h >
#include < execinfo.h >
/* Include prototypes */
void foo();
void showBackTrace();
/* Main function to call foo() function */
int main(void) {
foo();
return 0;
}
/* Call showBackTrace() function here */
void foo(void) {
showBackTrace();
}
/* showBackTrace() function to list the backtrace symbols obtained by backtrace() call */
void showBackTrace() {
const int maxbtsize = 50;
int btsize;
void* bt[maxbtsize];
char** strs = 0;
int i = 0;
/* Get the depth of backtraces available */
btsize = backtrace(bt, maxbtsize);
/* Get the symbols into 'strs' which is a char's double pointer */
strs = backtrace_symbols(bt, btsize);
/* Print the symbols obtained */
for (i = 0; i < btsize; i += 1) {
printf("%d.) %s\n", i, strs[i]);
}
free(strs);
}
Thanks.
No comments:
Post a Comment