17

is there such a thing as a (free) tool that would display a graph of all functions call in a given function? For instance, if I use it on a complex function fun() I'm trying to understand, it would show me all the functions called by fun() in order, then I would have the possibility to see the same thing for the function called by fun(), and so on. I'm looking for this for C++ code. Does such a thing even exist?

edit : I am using VS 2008 if that helps, but I was thinking that such a software would work on the source files anyway

lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • For any nontrivial program, the output would have to be unhandably clunky... though, with intelligent ommision heuristics – could work. – leftaroundabout Mar 09 '12 at 10:20
  • Well, you don't need very intelligent heuristics. A cut-off at two or three levels deep would be sufficient. You obviously can't show everything from `main` down. The main problem I see is `virtual` functions, where the actual implementation called is determined at runtime. – MSalters Mar 09 '12 at 10:28
  • possible duplicate of [Tools to get a pictorial function call graph of code](http://stackoverflow.com/questions/517589/tools-to-get-a-pictorial-function-call-graph-of-code) – finnw Jun 04 '12 at 14:14

8 Answers8

19

Doxygen can do this. See the CALL_GRAPH configuration option:

If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will generate a call dependency graph for every global function or class method. Note that enabling this option will significantly increase the time of a run. So in most cases it will be better to enable call graphs for selected functions only using the \callgraph command.

albert
  • 8,285
  • 3
  • 19
  • 32
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
6

Yes, Eclipse CDT Call Hierarchy view provides exactly this. Moreover, this view has 2 options:

  • Show Callers
  • Show Callees

You are asking about second one, but I am prefer the first one in code analysis.

ks1322
  • 33,961
  • 14
  • 109
  • 164
4

Intel(R) Single Event API is free open-source project that utilises GraphVis for call-graph visualisation. It takes a bit of labour to do manual or compiler-automated instrumentation, but beside statistics and call-graphs you will get the overtime views as well. Example of the image you can get

araud
  • 171
  • 1
  • 6
3

Yes such things exist. Google under the heading static code analysis. There are, for example, tools such as Understand, and it is extremely likely that your compiler can do this too for which I refer you to its documentation.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • 1
    (What compiler are *you* using? :-/) –  Mar 09 '12 at 10:22
  • 1
    I mostly use the Intel Fortran compiler but the Intel C/C++ compilers can also produce call-graphs. But Doxygen, as suggested by Greg, is probably a better answer. – High Performance Mark Mar 09 '12 at 10:27
  • How can Doxygen be a better answer, if a) there are overloads or multiple inheritance (what, Doxygen does a full Koenig lookup?) or there's an indirect function call? – Ira Baxter Mar 13 '12 at 04:44
3

You can use callgrind, and it's GUI tool kcachegrind.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
2

g++ and most compilers can do what you want. It is called profiling. Also there is the oprofile. A profiler gives you the call graph of an application after its execution. This is very useful to study code, you can also walk through the [debug] output as you look at the graph. A code analyzer, in contrast, will give you all possible call paths however, you will not be able to see the significant path easily.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • 2
    But profiling will only show those call graphs, that are actually executed, not "all possible". – Christian.K Mar 09 '12 at 10:24
  • I believe profiler is enough to understand code. It is simple and fast to do and will give you the basic idea. Doxygen output can be messy for large graphs. – perreal Mar 09 '12 at 10:25
2

I don't know of any tool specially desgined for this. However, there are a few ways of doing it:

  • Using a IDE (QtCreator is free, Visual Studio Express might also be helpful, Eclipse CDT)
  • Using (ctags)[http://ctags.sourceforge.net/] and a able text editor.
  • Using callgrind and the several views it brings. Advantage: you get to see the functions that are really called. Disadvantage: only runs in unixes, and you have to profile.
  • Using Doxygen... this one is really fancy, as it generates an html "view" of your code, provided that you supply the correct options.
dsign
  • 12,340
  • 6
  • 59
  • 82
1

VC++2008/2010 profiler generates among others the file *CallerCalleeSummary.csv, that contains this information. And this is the link to the article explaining how to use it with sample program: Profiling of C++ Applications in Visual Studio

SChepurin
  • 1,814
  • 25
  • 17