1

I want to trace all the user defined functions that have been called (in order, and preferably with the input params). Is there a way to do this using gdb? OR is there a better free/opensource application out there to do this job? Please note that I want to print only the user defined function calls.

for example:

int abc(int a, char b) {
  return xyz(a+b);
}

int xyz(int theta) {
  return theta * theta;
}

I need the following output:

abc(a, b); xyz(theta);

My codebase is pretty huge and is compiled in various pieces and hence I want to avoid using a tool which needs me to compile my source code again with some options enabled.

PS: I found that there are ways where you can define functions in gdb and pass in function names as params to find out there call trace. But in my case the code base is pretty huge and I'm starting off with it, so I'm not even sure what all functions are called etc. It wouldn't be practical to list all the functions in here.

TIA,

3
  • You do understand that it's quite possible to call a billion functions per second nowadays? That would translate to several gigabytes of output per second. Part of the problem is probably in your "user-defined" limitation; 3rd-party libraries are also user-defined. Perhaps you meant something like "functions defined in my own sources" - you can probably restrict the logging to functions defined in your own .o files. Commented Nov 15, 2010 at 9:41
  • You might want to look at gprof for this rather than gdb Commented Nov 15, 2010 at 9:44
  • As far as I remember, the only gdb command that lets you set some selection of breakpoints is rbreak, which breaks on functions matching a regular expression. Are your required breakpoints ammenable to that - e.g. a common namespace prefix? Commented Nov 15, 2010 at 9:51

1 Answer 1

2

You need to run some form of third-party tool against your binary, something like Quantify (IBM) or Callgrind, (or as @Paul R mentioned above gprof). They will generate a call tree, which will give you the information you need, google: "call tree C functions" for example, will reveal lots of goodies you can link against your code...

If you want to roll your own, you'd need to add one line to to the top of each of your functions which creates a stack allocated object and you can catch the ctor/dtor sequence to know when you've entered and exited the function and then maintain a "stack" of these to generate your own call tree... pretty easy to do (in single threaded, complex in multi-threaded)...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.