Call Graphs

A call graph is a directed graph where the vertices (nodes) are functions and the edges indicates a caller-callee relationship.  In other words, a call graph illustrates which functions call each other in a program.

To generate a call graph, your code can record the caller-callee relationships as it runs.  This is done like this:

// pseudo-code:
function bar(caller)
  foo("bar")
  record(caller, "bar")
end

In the above pseudo-code, the function bar calls foo.  Bar records who called it.  Foo should do the same.

In C++, you can use the callgraph class provided in the source code attached below.  You can use it like this:

callgraph cg;

void foo(std::string caller) {
  cg.add(caller, "foo");
}

void bar(std::string caller) {
  foo("bar");
  cg.add(caller, "bar");
}

int main() {
  bar("main");
  cg.display();
}

This will generate graphviz output.  Use the following commands to generate a pdf:

$ ./a.out > out.gv
$ dot -Tpdf out.gv > out.pdf

This will generate a call graph visualization like this:

More complex programs might generate a call graph like this:


ċ
cg.hpp
(0k)
Ryanne Dolan,
Jan 20, 2012, 9:58 AM
ċ
gvexample.cpp
(0k)
Ryanne Dolan,
Jan 20, 2012, 9:58 AM
Comments