fork download
  1. #include <iostream>
  2.  
  3. #include <execinfo.h>
  4.  
  5. namespace stck {
  6.  
  7.  
  8. class stacktrace_t {};
  9.  
  10. stacktrace_t stacktrace;
  11.  
  12. std::ostream &operator<<(std::ostream &out, stacktrace_t) {
  13. out << "stacktrace:\n";
  14.  
  15. size_t max = 256;
  16. void **stackframes = new void *[max];
  17. size_t numel;
  18. while ((numel = backtrace(stackframes, max)) >= max) {
  19. max *= 2;
  20. delete[] stackframes;
  21. stackframes = new void *[max];
  22. }
  23.  
  24. char **symbols = backtrace_symbols(stackframes, numel);
  25. for(size_t i = 0; i < numel; ++i)
  26. out << symbols[i] << '\n';
  27.  
  28. delete[] stackframes;
  29. return out;
  30. }
  31.  
  32.  
  33. }
  34.  
  35. int main() {
  36. std::cout << stck::stacktrace;
  37.  
  38. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
stacktrace:
./prog() [0x804887f]
./prog() [0x80486da]
/lib/i386-linux-gnu/i686/cmov/libc.so.6(__libc_start_main+0xf5) [0xb75a18f5]
./prog() [0x8048731]