pjaall
pjaall

Reputation: 389

Print out entering/exiting message in all functions in C code

We've been tracking a crash for months, and we're not really getting any closer. The program crashes at seemingly random times. For some reason, we're not able to print out a stack backtrace. And to make it even harder, the crashes only occur on a specific hardware, which we haven't figured out how to debug, or how to use Valgrind and the likes.

Binary search through revisions could be an option. But as it usually takes around one workday to produce a crash, it will be extremely time-consuming.

So. I would like to print log messages (or something like that) in all/most the functions throughout our code. Of the type "Entering function X", "Leaving function Y" etc. Hopefully, that could give some pointers on which haystack to go looking for the needle in. Is there any simple way to do this, without having to manually go through (literally) thousands of functions? Anybody knows of any program or script or macro magic that could be used for this?

Thanks

Upvotes: 1

Views: 693

Answers (1)

Brandon Yates
Brandon Yates

Reputation: 2052

You need to read your compilers manual. There probably are function entry/exit hooks that you can declare that will do exactly what you're looking for.

For example, in GCC you can use -finstrument-functions https://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Code-Gen-Options.html. You can code in a printf for every function entry that dumps the function address to stdout (or wherever you like). You can then use addr2line (from binutils) to translate address to function name.

Upvotes: 1

Related Questions