I am looking for a tool like ltrace or strace that can trace locally defined functions in an executable. ltrace only traces dynamic library calls and strace only traces system calls. For example, given the following C program:
#include <stdio.h> int triple ( int x ) { return 3 * x; } int main (void) { printf('%d\n', triple(10)); return 0; }
Running the program with ltrace will show the call to printf since that is a standard library function (which is a dynamic library on my system) and strace will show all the system calls from the startup code, the system calls used to implement printf, and the shutdown code, but I want something that will show me that the function triple was called. Assuming that the local functions have not been inlined by an optimizing compiler and that the binary has not been stripped (symbols removed), is there a tool that can do this?
Edit
A couple of clarifications:
- It is okay if the tool also provides trace information for non-local functions.
- I don’t want to have to recompile the program(s) with support for specific tools, the symbol information in the executable should be enough.
- I would be really nice if I could use the tool to attach to existing processes like I can with ltrace/strace.
Assuming you only want to be notified for specific functions, you can do it like this:
compile with debug informations (as you already have symbol informations, you probably also have enough debugs in)
given
Use gdb to trace:
Here is what i do to collect all function’s addresses:
Note that instead of just printing the current frame(
bt 1), you can do anything you like, printing the value of some global, executing some shell command or mailing something if it hits thefatal_bomb_explodedfunction 🙂 Sadly, gcc outputs some ‘Current Language changed’ messages in between. But that’s easily grepped out. No big deal.