C / C ++ needs a smart way to track function calls

I'm looking for a smart way to track function calls and return them. I know that I can use the debugger, but I would like it to simply print something in the terminal when the function is called, and not to skip the code.
I think I could use a preprocessor, but I'm not sure what would be the best way to do this. Or is there a way to use gdb to print out information that would be useful while it would not be necessary to go through the code.

+5
source share
9 answers

Most compilers allow you to introduce an instrumental function before and after the function call.

in msvc they are _penter and _pexit
good article http://www.drdobbs.com/184403601

in gcc you should use the functions -finstrument functions of
http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Code-Gen-Options.html

You can use debug libraries or map files for more information.

+13
source

To solve the problems associated with the use of the function, a rather complicated solution is used. This will have a big impact on performance, but will be fairly explicit in the logs, without requiring the user to add tools to all possible code paths that might leave this function:

class ScopeLogger {
public:
   ScopeLogger( std::string const & msg ) : msg(msg)
   {   std::cout << "Enter: " << msg << std::endl; }
   ~ScopeLogger()
   {   std::cout << "Exit:  " << msg << std::endl; }
   std::string msg;
};
#if DEBUG
#define FUNCTION(x) ScopeLogger l_##x##_scope(x);
#endif

void foo( int value ) {
   FUNCTION( __FUNCTION__ );
   if ( value > 10 ) throw std::exception;
   std::cout << "." << std::endl;
}

int main() {
   foo(0);    // Enter: foo\n.\nExit:  foo
   foo(100);  // Enter: foo\nExit:  foo
}

, ScopedLogger, :

class ScopeLogger {
public:
   ScopeLogger( std::string const & msg ) : msg(msg)
   {   std::cout << std::string(indent++,' ') << "Enter: " << msg << std::endl; }
   ~ScopeLogger()
   {   std::cout << std::string(--indent,' ') << "Exit:  " << msg << std::endl; }
   std::string msg;
   static int indent;
};
int ScopeLogger::indent = 0;
+4
#define BEGIN_FUNC(X) printf("Function %s Entered",X)
#define END_FUNC(X)  printf("Function %s End",X)

foo()
{
BEGIN_FUNC(__func__);

//Your code here


END_FUNC(__func__);


}

, , , , , .

+3

GCC, .

Link-Time Replacement / Wrapping
– GCC option: -Wl,--wrap,function_name

, "function_name()" "__wrap_function_name()". , "__real_function_name()".

+3

gdb , ,

. , . "", . "backtrace" ( "bt" ), .

+2
+2

, TARGET_ASM_FUNCTION_END_PROLOGUE TARGET_ASM_FUNCTION_BEGIN_EPILOGUE. , , - - , / , FUNCTION_PROFILE / PROFILE_HOOK (, at: http://gcc.gnu.org/onlinedocs/gccint/Function-Entry.html).

+1

__FUNCTION__ (Reference), ( Class::Method), , .

However, when I needed the same "trace" of information recently, I could not find an automatic method.

0
source

Some time ago, I listened to a conversation about aspect programming , which includes what you want to achieve. Perhaps finding this term helps.

0
source

All Articles