fork(1) download
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. struct stack_frame {
  7. const char *funName;
  8. const char *fileName;
  9. int line;
  10. stack_frame(const char* func, const char* file, int ln)
  11. : funName(func), fileName(file), line(ln) {}
  12. };
  13.  
  14. thread_local list<stack_frame> *frames = 0;
  15.  
  16. struct entry_exit {
  17. bool delFrames;
  18. entry_exit(const char* func, const char* file, int ln) {
  19. if (!frames) {
  20. frames = new list<stack_frame>();
  21. delFrames = true;
  22. } else {
  23. delFrames = false;
  24. }
  25. frames->push_back(stack_frame(func, file, ln));
  26. }
  27. ~entry_exit() {
  28. frames->pop_back();
  29. if (delFrames) {
  30. delete frames;
  31. frames = 0;
  32. }
  33. }
  34. };
  35.  
  36. void show_stack() {
  37. for (list<stack_frame>::const_iterator i = frames->begin() ; i != frames->end() ; ++i) {
  38. cerr << i->funName << " - " << i->fileName << " (" << i->line << ")" << endl;
  39. }
  40. }
  41.  
  42. #define FUNCTION_ENTRY entry_exit _entry_exit_(__func__, __FILE__, __LINE__);
  43.  
  44. void foo() {
  45. FUNCTION_ENTRY;
  46. show_stack();
  47. }
  48. void bar() {
  49. FUNCTION_ENTRY;
  50. foo();
  51. }
  52. void baz() {
  53. FUNCTION_ENTRY;
  54. bar();
  55. }
  56.  
  57. int main() {
  58. baz();
  59. return 0;
  60. }
Success #stdin #stdout #stderr 0s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
baz - prog.cpp (53)
bar - prog.cpp (49)
foo - prog.cpp (45)