How do I find the call sequence?

Posted by Hrishikesh on Fri 11 May 2007 10:12 AM — 3 posts, 17,365 views.

#0
Hi,

I am trying to find the sequence in which the functions in a C++ code are getting called. I was wondering if I could use gdb for this.
I was thinking of putting a tracepoint at each function, and then just displaying or dumping the function name into a file would help.
But unfortunately, I was not able to do so.

Can someone plz help me out ?

Regards,

Hrishikesh
USA #1
gdb is pretty powerful and probably could do it, though I wouldn't know how to do such a thing in gdb myself. Might I ssuggest that it'd probably be easier just to create a log file and add a line in each function to output to the log when the function runs?
Australia Forum Administrator #2
If you put a breakpoint on a function at the bottom of the chain, and then do a "bt" (backtrace) you will see the sequence of nested calls that got you there.

If you just want to see when functions are being called in general, I don't think there is a particularly easy way.

One method could be to make a small helper function like this one:

http://www.gammon.com.au/forum/bbshowpost.php?id=2998

Then you could put (in the source code) at the start of each function a line that shows that function is being called. Once you have debugged you could "define" it to do nothing.

Here is an example that shows what I mean:


#include <iostream>
#include <string>

void fname (const std::string s) 
  {
  std::cout << "Entering function: " << s << std::endl;
  }

void bar ()
  {
  fname ("bar");
  }

void foo ()
  {
  fname ("foo");
  bar ();
  }

int main ()
  {
   std::cout << "Program starting" << std::endl;
   foo ();
   return 0;
  }


Output

Program starting
Entering function: foo
Entering function: bar