Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add print statements in every function in c files right programmatically?

Tags:

c

debugging

call

I am working on embedded code and for now totally reliant on prints from within functions to figure out the flow of execution (there is no stack trace functionality available).

It often happens that I put a bunch of print statements, build my code and run it only to realize I should've put prints in a dozen other places too. And then start the hour long process again.

Is there an easy way to take my 5 or 6 c files that I want to analyze and run some tool that will go in and add a print statement in each function? (this will obviously have to be after the variable declarations as this is in C)

Even better would be to have a print each time there is an if/ else or switch/ case ..basically any conditional statements.

like image 654
user657862 Avatar asked Mar 13 '11 20:03

user657862


1 Answers

You don't state the compiler you are using, but gcc has a very handy switch:

-finstrument-functions

which inserts a special call at each function entry and exit. You could compile only the relevant files with this tweak, no need to modify the source code.

The calls are made to two functions you should create:

      void __cyg_profile_func_enter (void *this_fn,
                                     void *call_site);
      void __cyg_profile_func_exit  (void *this_fn,
                                     void *call_site);

Etrace is a tool designed for exploiting this to create call traces, see http://ndevilla.free.fr/etrace/

like image 52
mvds Avatar answered Oct 15 '22 20:10

mvds