There is a code with functions. Is there any way to know the order of these functions? Those. I want to see something like this:

main -> func1 star -> func2 start -> func2 end -> funс1 end -> func3 start -> func3 end 

You can of course at the beginning and end write puts("star/end\n") . Perhaps there is something more convenient, or by what words is it google?

  • #define as always, but not quite ... - Qwertiy
  • one
    There is a debugger for this ... - VTT

2 answers 2

I assume that you do not need a list of ALL calls. And, in fact, you need a stack of function calls at some point at which (most likely) an error occurred and you are trying to understand where the control came from.

For this purpose, in c / s ++ there is a backtrace function:

backtrace () returns a backtrace for the calling program. A backtrace is currently active function calls for the program. It is a void of each type of frame. It can be stored in buffer. If the backtrace is larger than the size, then the addresses corresponding to the size most recent function calls are returned; To obtain the complete backtrace, make sure that the buffer size is large enough.

man 3 backtrace

  • 2
    This is not "in c / c ++", but a specific functionality in the GNU library. In C / C ++, there is nothing like this. - AnT
  • In C / C ++, there is nothing like this. In C / C ++ languages ​​there is NOTHING other than the language itself. Even printf is no longer a language, but a library. - Sergey
  • 6
    printf is a function from the standard library, the specification of which is part of the language specification (that is, described by the language standard). The printf function is a standard function. The backtrace function is not standard. - AnT

I once used the (C ++ only) type class

 class CallInfo { public: CallInfo(const char * name):name(name) { cout << name << " start\n"; } ~CallInfo() { cout << name << " end\n"; } private: const char * name; }; #define Callinfo CallInfo ci__(__func__); void test() { Callinfo } int main(int argc, const char * argv[]) { Callinfo test(); }