I believe that you cannot do this in the general case. First, if main returns some code, this is the exit code (if main does not have an explicit return , then for the latest C-standards, the compiler needs to add an implicit return 0; ). Then the function can store the exit address in some data (for example, a global function, a field in struct , ...), and some other functions can correctly call it through the function pointer. In practice, a program can load some plugins using dlopen and use the dlsym name to "exit" or just call exit inside the plugin, etc .... AFAIU, solving this problem (finding the actual exit calls in a dynamic sense) can be proved in full generality equivalent to stopting problem. See Also Rice Theorem .
Without pretending to be an exhaustive approach, I would suggest something else (assuming that you are interested in tool programs encoded in C or C ++, etc .... whose source code is available to you). You can customize the GCC compiler with MELT to modify the base blocks processed inside GCC to call some of your tool functions. This is not trivial, but it is doable ... Of course, you will need to recompile some C code with such configured GCC in order to measure it.
(Disclaimer, I am the main author of MELT , feel free to contact me for help ...)
By the way, do you know about atexit (3) ? This may be useful for your flushing problem ... And you can also use LD_PRELOAD tricks (read about dynamic linkers , see ld-linux (8) ).
source share