开发者

How do I get a print of the functions called in a C++ program under Linux?

What I want is a mix of what can be obtained by a static code analysis like Doxygen and the stackframe you can see when using GDB. I know which problematic function I'm debugging and I want to see the neighbourhood of the function calls that guided the execution to this function call. For instance, running a simple HelloWorld! would output something like:

main:
   Greeter::Greeter()
   Greeter::printHello()
   Greeter::printWorld()

denoting that from the main function, the constructor开发者_运维问答 was called and then the printHello and printWorld functions where called. Notice that in GDB if I break at printWorld I won't be able to see in the stackframe that printHello was called.

Any ideas about how to trace function calls without going through the pain of inserting log messages in a myriad of source files?

Thanks!!


The -finstrument-functions option to gcc instructs the compiler to call a user-provided profiling function at every function entry and exit.

You could use this to write a function that just logs every function entry and exit.


From reading the question I understand that you want a list of all relevant functions executed in order as they're executed.

Unfortunately there is no application to generate this list automatically, but there are helper macros to save you a lot of time. Define a single macro called LOGFUNCTION or whatever you want and define it as:

#define LOGFUNCTION printf("In %s (%s:%d)\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);

Now you do have to paste the line LOGFUNCTION wherever you want a trace to be added.

wherever you see fit.

see http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html and http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html


GDB features a stack trace, it does what you ask for.


What he wants is to obtain tha info (for example, backtrace from gdb) but printed in a 'nicer' format than gdb do. I think you can't. I mean, maybe there is some type of app that trace your application and do something like that, but I never hear about something like that.

The best thing you can do is use GDB, maybe create some type of bash script that use gdb to obtain the info and print it out in the way you like.

Of course, your application MUST be compiled with debug symbols (-g param to gcc).


I'm not entirely sure what the problem is with gdb's backtrace, but maybe a profiler is closer to what you want? For example, using valgrind:

valgrind --tool cachegrind ./myprogram
kcachegrind callgrind.out.NNNN


Have you tried to use gprof to generate a call graph? You can also convert gprof output to something easier on the eye with gprof2dot for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜