# Call graph

A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge (f, g) indicates that procedure f calls procedure g. Thus, a cycle in the graph indicates recursive procedure calls.

## Basic concepts

Call graphs can be dynamic or static. A dynamic call graph is a record of an execution of the program, for example as output by a profiler. Thus, a dynamic call graph can be exact, but only describes one run of the program. A static call graph is a call graph intended to represent every possible run of the program. The exact static call graph is an undecidable problem, so static call graph algorithms are generally overapproximations. That is, every call relationship that occurs is represented in the graph, and possibly also some call relationships that would never occur in actual runs of the program.
Call graphs can be defined to represent varying degrees of precision. A more precise call graph more precisely approximates the behavior of the real program, at the cost of taking longer to compute and more memory to store. The most precise call graph is fully context-sensitive, which means that for each procedure, the graph contains a separate node for each call stack that procedure can be activated with. A fully context-sensitive call graph is called a calling context tree. This can be computed dynamically easily, although it may take up a large amount of memory. Calling context trees are usually not computed statically, because it would take too long for a large program. The least precise call graph is context-insensitive, which means that there is only one node for each procedure.
With languages that feature dynamic dispatch (i.e. Java or C++), first-class functions (i.e. Python or Racket), or function pointers (i.e. C), computing a static call graph precisely requires alias analysis results. Conversely, computing precise aliasing requires a call graph. Many static analysis systems solve the apparent infinite regress by computing both simultaneously.

## Usages

Call graphs can be used in different ways. One simple application of call graphs is finding procedures that are never called. Call graphs can act as documentation for humans to understand programs. Call graphs can also be used to detect anomalies of program execution or code injection attacks.

## Software
### Free software call graph generators
#### Run-time call graph (most of tools listed are profilers with call graph functionality)

* gprof : included in BSD or part of the GNU Binary Utilities
* callgrind : part of Valgrind
* KCachegrind : powerful tool to generate and analyze call graphs based on data generated by callgrind
* Mac OS X Activity Monitor : Apple GUI process monitor Activity Monitor has a built-in call graph generator that can sample processes and return a call graph. This function is only available in Mac OS X Leopard
* OpenPAT : includes the control_flow tool which automatically creates a Graphviz call-graph picture from runtime measurements.
* pprof, open source tool for visualization and analysis of profile data, to be used in conjunction with gperftools.
* CodeAnalyst from AMD (released under GPL)
* makeppgraph is a dependency graph generator (at module level) for builds performed with makepp.
* Intel(R) Single Event API (free, open-source)

#### Static for getting call graphs without running application
##### C/C++

* Sourcetrail creates a static call graph, that can be dynamically explored by the user. Also supports Python and Java
* doxygen : Uses Graphviz to generate static call/inheritance diagrams
* Cally: a tool that uses GCC's Register Transfer Language (RTL) files to build a caller or callee call graphs for C projects.
* cflow : GNU cflow is able to generate the direct and inverted call graph of a C program
* egypt : a small Perl script that uses gcc and Graphviz to generate the static call graph of a C program.
* Analizo: calculates source code metrics, generates dependency graphs.
* CCTree : Native Vim plugin that can display static call graphs by reading a cscope database. Works for C programs.
* codeviz : a static call graph generator (the program is not run). Implemented as a patch to gcc; works for C and C++ programs.
* calltree.sh : Bash shell functions that glue together cscope, graphviz, and a sampling of dot-rendering tools to display "caller" and "callee" relationships above, below, and/or between the C functions you specify.
* tceetree : like calltree.sh, it connects Cscope and Graphviz, but it is an executable rather than a bash script.

##### Go

* go-callvis : an interactive call graph generator for Go programs whose output can be drawn with Graphviz

##### Multi-language

* callGraph : open-source call graph generator for awk, bash, basic, dart, fortran, go, lua, javascript, julia, kotlin, matlab, perl, pascal, php, python, R, raku, ruby, rust, scala, swift, tcl, and typescript.

##### .NET

* NDepend :is a static analysis tool for .NET code. This tool supports a large number of code metrics, allows for visualization of dependencies using directed graphs and dependency matrix.

##### PHP, Perl and Python

* Devel::NYTProf : a Perl performance analyser and call chart generator
* phpCallGraph : a call graph generator for PHP programs that uses Graphviz. It is written in PHP and requires at least PHP 5.2.
* pycallgraph Archived 2007-05-25 at the Wayback Machine : a call graph generator for Python programs that uses Graphviz.
* pyan : a static call graph generator for Python programs that uses Graphviz.
* gprof2dot : A call graph generator written in Python that converts profiling data for many languages/runtimes to a Graphviz callgraph.
* code2flow: A call graph generator for Python and Javascript programs that uses Graphviz
* rcviz : Python module for rendering runtime-generated call graphs with Graphviz. Each node represents an invocation of a function with the parameters passed to it and the return value.

##### XQuery

* XQuery Call Graphs from the XQuery Wikibook: A call graph generator for an XQuery function module that uses Graphviz

### Proprietary call graph generators
#### LDRA Testbed

    Static and dynamic analysis engines for both host and embedded software, with a myriad of reports including call graphs.

#### Project Analyzer

    Static code analyzer and call graph generator for Visual Basic code

#### Visual Expert

    Static code analyzer and call graph generator for Oracle PL/SQL, SQLServer Transact-SQL, C# and PowerBuilder code

#### Intel VTune Performance Analyzer

    Instrumenting profiler to show call graph and execution statistics

#### DMS Software Reengineering Toolkit

    Customizable program analysis tool with static whole-program global call graph extraction for C, Java and COBOL

### Other, related tools
#### Graphviz

    Turns a text representation of any graph (including a call graph) into a picture.

#### tsort

    Command-line utility that performs a topological sort.

## Sample graph

A sample call graph generated from gprof analyzing itself: