Automatic tracking of program execution

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

For example, with the gcc option or a small tool, I will enable tracing and the trace will be printed to the console or dumped to a file.

Since there are many files and functions / classes, I do not want to start adding trace prints manually.

If such tools are not available, the next choice is to use scripts and try to add traces to the print.

strace not very useful as it gives mainly system calls.

+6
source share
8 answers

To track function on / off, you can recompile your code with the -finstrument-functions option so that each time the function is called, the __cyg_profile_func_enter() function is called, and __cyg_profile_func_exit() is called when the function __cyg_profile_func_exit() .

You can implement these functions to track the addresses of called functions, and they use nm to translate addresses into function names.

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

+11
source

For GCC, you can create profiling and then run the program. This will create the gmon.out file, which, in turn, will contain (sort) a trace of the functions performed by the program.

It doesn't even come close to the utility or ease of use of the manually-written printf () s trace.

+4
source

Since you asked about gcc. It has the -finstrument-functions option, which allows you to execute arbitrary code before and after function calls.

+3
source

USE Log4cxx .

This will log the information after you specify the file type, file name and file size in their configuration file.

Follow the steps to run the sample program.

+1
source

If you do not want to modify all your printf() calls, I would suggest doing something along these lines in the header file and including 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 

It's nice to note that you can also replace printf with your own registration function:

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

Note that here I am using a good macro trick: a variable macro .

0
source

What about ltrace ?

Tracing utility for calling library functions.

0
source

Use Lttng, but this requires tools with source code if you want to trace on binary files. Alternatively, you could use Aspect C ++ so that you need to code and precompile using the aspect compiler

0
source

I know that you don’t want to add something to each function, but if it is as simple as changing {to {_, will it beat you? Few scripts can do this automatically - ping me if you need it. If this works, look at this little utility that I have compiled - just one header file to include, and then you get a nice portable trace

https://github.com/goblinhack/callstack

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

Call CALLSTACK_DUMP () at any time to reset the current column.

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 
0
source

Source: https://habr.com/ru/post/886860/


All Articles