Introduction

gprof2dot and xdot can be used to visually inspect the output generated by gprof. Given a gprof output file, the former generates the call graph of the program in a DOT file, while the latter is an interactive viewer for such files.

Both programs are maintained by José Fronseca, and are hosted at Google Code (http://code.google.com/p/jrfonseca/w/list)

First step: generating profiling data with gprof

The program has to be compiled with the -pg and optionally with the -g flag:

Example compilation commands:

$ gcc -pg -g source.c -o binary
$ nvcc -Xcompiler "-g -pg" -g -pg -arch=sm_20 source.cu -o binary

The program must then be run once like in a normal situation. This generated a file named gmon.out in the same folder as the binary.

Finally, gprof can be used to generate the profiling data (the command-line arguments that are normally needed by the program don't have to be provided) :

$ gprof ./binary > gprof_output.txt

Second step: generating the call graph

Gprof2Dot can now be used to generate the call graph. It is a python script which requires Graphviz. More information on how to install it can be found in the program's documentation. Chances are both gprof2dot and Graphviz are readily available in the repository of your Linux distribution.

For example gprof2dot is avilable on Gentoo in the dev-util/gprof2dot ebuild. Note that graph2dot seems to have compatibility issues with Python 3.x.

If Python 3 is the default python interpreter on your system, try installing the 2.7 version and modify the hashbang in /usr/bin/gprof2dot (typically, it should be something like #!/usr/bin/python2.7). On Gentoo, you can use eselect to change the default interpreter:

# eselect python list
Available Python interpreters:
 [1]   python2.7
 [2]   python3.2 *
# eselect python set 1

The call graph can be generated with the following command:

$ graph2dot gprof_output.txt > call_graph.dot

Third step: visualizing the graph

Finally, use XDot to interactively visualize the graph. It is also a python script, and it requires PyGTK as well as GraphViz. PyGTK is the name of the Python GTK+ binding, and is included in the repository of virtually all Linux distributions (the corresponding ebuild is dev-python/pygtk on Gentoo). It seems XDot is not compatible with Python 3.x either. The program's documentation provides further information.

The following command will display the call graph:

$ xdot call_graph.dot

Note that XDot can be used to visualize any DOT file.

Fourth step: converting the graph to an image

Graphviz includes a command-line program which allows to save a DOT graph as an image file:

PNG file

$ dot -Tpng call_graph.dot -o call_graph.png

PostScript file

$ dot -Tps call_graph.dot -o call_graph.ps

see man dot for more details...