tags:

views:

299

answers:

4

Do i have to add entry and exit logs in all the C functions?

If, so are there any possible ways to write the entry and exit of a function automatically (Similar to Automatic Entry/Exit using Aspect oriented Programming in java).

The application that I'm writing is time critical and adding entry/exit statements for each function might delay the performance and also makes the code non-readable.

+1  A: 

First of all. If you want to log something it will take time, no matter how you accomplish it. That being by aspect programming or something else. If your application is time critical you should have some switch to turn on/off debugging.

Update: Checkout http://perfinsp.sourceforge.net/hookit_linux.html

Subtwo
Subtwo has the google-foo. Nice catch.
dmckee
Thanks for the link. Let me have a look.
Vibin Vijay
A: 

Do you want this for a debugging environment (where ptrace or similar tools (dtrace, ...) might do) or for on-going use?

dmckee
Currently I require this for debugging (I do not prefer using ptrace or similar tools for the same).
Vibin Vijay
+2  A: 

C does not support any form of introspection or doing stuff automatically through magic in the runtime or on the virtual machine; there is no virtual machine, and the runtime support is basically just libraries of "dead" code that provide standard functionality.

And as Subtwo pointed out, if you want to do something (like log entry/exit), it will have to be done and thus take time. You can't just "hide" that penalty, there is very little overhead to put it in.

A standard profiler might give you some insight, by sampling your program statistically to see which functions execute, but that will not give you the call order, just random locations where your program was executing code at the time it was sampled.

You could perhaps turn to some preprocessor trickery, this is a common way to do "boilerplate" stuff like logging, by for instance defining some macros that you then put at the entry/exit points of each function. Still, you'd have to remember including the macros, and any performance penalty from doing the logging will of course be present.

unwind
A: 

Check out AspectC, a variant of C extended with aspect-oriented features (just like AspectJ is a variant of Java with aspect-oriented features).