7

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.

1
  • 1
    Good luck writing a regex to do that. Commented Mar 13, 2011 at 20:36

5 Answers 5

12

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/

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I'm not using gcc but I'll ask the c gurus at my company something similar exists.
1

you can use macros like this

 #define begin {printf(__func__##" Started"); #define end printf(__func__##" Ended");} 

and then replace all your {,} with begin and end macros (__func__ is a macro which return function name and is defined in C99 there are also other equvalent macros for other compilers)

1 Comment

thanks, this is really useful. I didn't know about this macro. googling a bit also found about PRETTY_FUNCTION but the problem still is that if I make the macro put the print (it's actually not a printf..it's a variation for our environment) on the very top, there will be many compile errors because of the var declarations that would not be on top anymore. Similarly, the end one would have problems due to the possibility of several returns here and there. I would still be happy with only the start macro if it could somehow be put at the end of the variable declarations in each function.
0

For only 5-6 files I would manually go in and add a PRINT("name of function") macro in each function and then define that to either output the string, or nothing

You could use ctags to analyse the files and build this automatically but unless you have 100s of files it would be quicker to just do it by hand

1 Comment

yeah..that's the thing..these files have lots and lots of functions and I'm sure I will need to do this in the future too..that's why I was looking for doing it programmatically..if nothing else works then this is what I'll do :)
0

If vim is your favourite editor you can install this plugin: http://www.vim.org/scripts/script.php?script_id=213 and customize the related template. Can be useful for a lot of differents tasks. (It works for the new functions that you will define, that is not your case but might be useful for future use)

1 Comment

I use sourceinsight and notepad++ but I'll check this out. thanks!
0

I would recommend you using a debugger, like GBD. That way you can even run your program "step by step" and analyze such conditions. I don't see a reason to print something in every function.

3 Comments

That reason exists. When I wanted to find out why and where ghostscript made some radically different judgement based on a tiny tweak on the input side, comparing two 1.5 gb call traces quickly revealed the bifurcation point. Good luck stepping through such a program using gdb... ;-)
@mvds Certainly sir, reason accepted! :-) Anyway, I didn't want to mean that the program may run step by step, just that this can be a useful tool. Who knows this is not what the OP is looking for... But thanks for pointing out such case of affairs. I'm not a big C master, just learning every day.
I don't think I can use that in my case..It's on an embedded device. Apparently there are JTAG debuggers that can be used but I don't have one yet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.