fork download
  1. #include <execinfo.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* Obtain a backtrace and print it to stdout. */
  6. void
  7. print_trace ()
  8. {
  9. void *array[10];
  10. size_t size;
  11. char **strings;
  12. size_t i;
  13.  
  14. size = backtrace (array, 10);
  15. strings = backtrace_symbols (array, size);
  16.  
  17. printf ("Obtained %zd stack frames.\n", size);
  18.  
  19. for (i = 0; i < size; i++)
  20. printf ("%s\n", strings[i]);
  21.  
  22. free (strings);
  23. }
  24.  
  25. /* A dummy function to make the backtrace more interesting. */
  26. void
  27. dummy_function (int i)
  28. {
  29. (void)i;
  30. print_trace ();
  31. }
  32.  
  33. int
  34. main (void)
  35. {
  36. int i = (int)&main - (int)&dummy_function + (int)&main - (int)&print_trace;
  37. dummy_function (i);
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 1908KB
stdin
Standard input is empty
stdout
Obtained 2 stack frames.
./prog [0x804855a]
./prog [0x8048481]