Question

I would like to obtain a chronological log of the functions invoked by a C++ program, preferably in a text file, using valgrind.

For the example C++ program (simple.cpp) below:

void baz(){
}

void bar(){
  for(int i = 0; i < 3; i++)
    baz();
}

void foo(){
  bar();
}

int main(){
  foo();
  return 0;
}

I wish to obtain main() -> foo()-> bar->baz()*3

What I have tried:

Compile as g++ -g simple.cpp -o simple.out and run valgrind --tool=callgrind ./simple.out to obtain callgrind.out.3519

Running callgrind_annotate --tree=both callgrind.out.3519 | grep baz returns nothing.

Saying kcachegrind callgrind.out.3519 and then navigating within the source code view for the function main() allows me to see the calls in chronological order.

Is there a way to write just this information out to a log?

Was it helpful?

Solution

It looks like callgrind_annotate is meant to do exactly this with the data generated by callgrind. The reason it does not show "baz" given the trivial example is simply that your test code executes so quickly that the execution time spent within it pales in comparison to the time spent within the overhead code (e.g. dynamic-library loading code).

You can get callgrind_annotate to include your baz either by using the threshold parameter:

callgrind_annotate --threshold=100 --tree=both callgrind.out.3519 | grep baz

Or by altering the example:

int main(){
  for(int i=0;i<1000000;i++9 {
    foo();
  }
  return 0;
}

OTHER TIPS

Just write a clog << "functionname"; in every function. Would kill your performance, but that is what logging is for.

If you want to keep track of calling, i would rather recommend commenting and building doxygen doku, which can generate you caller-graphs as long as there are no function pointers.

If that doesn't fit you, we should know what exactly you need that logging for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top