Written 23-Feb-2008 by muppet . This work is in the public domain. COUNT_CALLERS(): Find out who calls a given function. Yes, you can do this with fancier debugging tools, like gprof, or sysprof, or dtrace, or whatever it is that the cool kids use these days. However, some environments make it impossible to use these better tools, for a variety of strange and unnatural reasons. In such situations, one must improvise. Just #include this header in the source file where your function is implemented, and add a single call to COUNT_CALLER() somewhere near the beginning of that function. Make sure you do this in only one function in your program. If you don't, you'll fail an assertion Then, either link in countcallers.c, or, if you're feeling really sneaky, just #include the .c file where you #include'd the .h file. Also make sure you compile with debugging symbols. Your program will take a performance hit, because the algorithm for storing the caller addresses is rather dumb. But it shouldn't be bad. When the program exits, an atexit() function will dump information about who called that function to a text file named "callers.txt", or whatever file is named in the environment variable CALLER_COUNT_FILE. This file contains enough information for the utility program read_callers to extract the file, line, and function name of each caller in the list. This works for both static and dynamic executables. For example: $ ./myprogram $ read_callers or $ env CALLER_COUNT_FILE=/tmp/foo.txt ./myprogram $ read_callers /tmp/foo The read_callers utility requires perl and addr2line.