Automatic tracing of program execution

三世轮回 提交于 2019-12-30 08:39:15

问题


I would like to know if we can enable tracing in any C or C++ application.

For example, with a gcc option or a small tool, I will enable trace and either trace is printed on console or dumped to a file.

Since there are lots of files and function / classes, I don't want to start adding the trace prints manually.

If such tools are not available, next choice is use scripting and try to add at the trace printing.

strace is not much useful as it gives mainly the system calls.


回答1:


To trace the function entry/exit, you can recompile your code with the option -finstrument-functions so that each time a function is invoked, a __cyg_profile_func_enter() function is called, and __cyg_profile_func_exit() is called when the function returns.

You can implement those functions to trace the addresses on the called functions, and them use nm to convert the addresses into function names.

EDIT: etrace does all this: it provides the source code for the __cyg_profile_func_enter() and __cyg_profile_func_exit() and functions that write the addresses to a named pipe and a Perl and a Python script to read the addresses and do the actual tracing with the function names and indentation.




回答2:


For GCC, you could build with profiling support, and then run the program. That will create the gmon.out file, which in turn will contain a (sort of) trace of the functions executed by the program.

That will not even come close to the utility or ease of use of hand-written trace printf()s, though.




回答3:


Since you were asking about gcc. It has the option -finstrument-functions that allows you to execute arbitrary code before and after calls to functions.




回答4:


USE Log4cxx.

This will log the details once you specify the file type,filename and file size in their configuration file.

Follow the steps to run the sample program.




回答5:


If you don't want to modify all your printf() calls, I'd suggest doing something along those lines in a header file, and include it in all your C code files:

#ifndef DEBUG /* if not in debug mode, disable printf */
  #ifdef printf
    #undef printf
    #define printf(format, ...)
  #endif 
#endif

What's cool with that is that you can also replace printf with a logging function of your own:

#define printf(format, ...) my_log_function( format, ##__VA_ARGS__ )

Notice that I use here a nice macro trick: variadic macro.




回答6:


What about ltrace?

A tracing utility for library function calls.




回答7:


Use Lttng but it requires source code instrumentation if you want to trace on binaries systemtap linux based tool might be helpful. Also Aspect C++ could be used were u need to code and precompile with aspect compiler




回答8:


I know you don't want to add something to each function, but if it's as simple as changing { to {_ does that win you over? A bit of scripting can do this automatically - ping me if you need that. If this does work, then look at this small utility I put together - just a single header file to include and then you get nice portable tracing

https://github.com/goblinhack/callstack

e.g.: void my_function (void) {_ // rest of code }

Call CALLSTACK_DUMP() at any time to dump the current callstack.

Just do

make
./callstack

Stack dump:
(stack) 1 main.cpp void foo3(int, int), line 7
(stack) 2 main.cpp void foo2(int), line 12
(stack) 3 main.cpp void foo1(), line 17
(stack) 4 main.cpp int main(int32_t, char **), line 22
|improve this answer

来源:https://stackoverflow.com/questions/5816950/automatic-tracing-of-program-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!