Automatic tracing of program execution

落爺英雄遲暮 提交于 2019-12-01 04:07:31

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.

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.

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.

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.

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.

What about ltrace?

A tracing utility for library function calls.

Step

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

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

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

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