Back to index

glibc  2.9
execinfo.c
Go to the documentation of this file.
00001 #include <execinfo.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 /* Obtain a backtrace and print it to @code{stdout}. */
00006 void
00007 print_trace (void)
00008 {
00009   void *array[10];
00010   size_t size;
00011   char **strings;
00012   size_t i;
00013 
00014   size = backtrace (array, 10);
00015   strings = backtrace_symbols (array, size);
00016 
00017   printf ("Obtained %zd stack frames.\n", size);
00018 
00019   for (i = 0; i < size; i++)
00020      printf ("%s\n", strings[i]);
00021 
00022   free (strings);
00023 }
00024 
00025 /* A dummy function to make the backtrace more interesting. */
00026 void
00027 dummy_function (void)
00028 {
00029   print_trace ();
00030 }
00031 
00032 int
00033 main (void)
00034 {
00035   dummy_function ();
00036   return 0;
00037 }