LinuxCommandLibrary

gprof

Profile program execution time and function calls

TLDR

Compile binary to default a.out with gprof information and run it to get gmon.out

$ gcc [[-p|-pg]] [program.c] && ./a.out
copy

Run gprof on default a.out and gmon.out to obtain profile output
$ gprof
copy

Run gprof on a named binary
$ gprof [path/to/binary] [path/to/gmon.out]
copy

Suppress profile field's description
$ gprof [[-b|--brief]]
copy

Display routines that have zero usage
$ gprof [[-bz|--brief --display-unused-functions]]
copy

SYNOPSIS

gprof [options] executable-file [profile-file …]

PARAMETERS

-a
    suppress toggle between static/dynamic call graph

-b
    brief output (flat profile only)

-C filename
    write call graph data to filename

-D
    display summary of total time by function

-d
    debug: display all options

-e function_name
    exclude function_name from flat profile and call graph

-E function_name
    exclude function_name from flat profile only

-f function_name
    display only function_name call graph

-F function_name
    display call graph for function_name only

-G
    ignore shared library symbols

-h
    print brief help

-i
    print call chain summary

-I directory
    search directory for symbols

-k from/to
    subtract time for arc from from to to

-K function_name
    omit function_name from call graph

-l
    line-by-line profiling

-L
    no recursion in call graph

-m num
    demangle C++ names up to num times

-M demangling
    use specific demangling style

-n function_name
    omit function_name from flat profile

-N function_name
    omit function_name from call graph

-O search_path
    override default search path

-p
    propagate times in call graph

-P function_name
    omit function_name from propagated times

-r
    generate call graph in reverse

-s
    sum multiple profile files

-S filename
    read static call graph from filename

-t function_name
    limit call graph to function_name subtree

-T function_name
    limit call graph to ancestors of function_name

-u call_graph_file
    use call_graph_file for static calls

-v
    verbose

-w
    suppress column headers

-x
    exclude symbols matching pattern

-X pattern
    exclude symbols from flat profile

-y
    exclude external symbols

-Y pattern
    exclude external from flat profile

-z
    show zero-count functions

-Z
    omit unimplemented symbols

DESCRIPTION

gprof is a tool from the GNU Binutils suite for analyzing the runtime performance of programs. It generates execution profiles by processing data from gmon.out files, which are produced when running executables compiled with the GCC -pg flag (e.g., gcc -pg -o prog prog.c; then ./prog creates gmon.out).

gprof displays two main reports: a flat profile listing functions by time spent (self-time and total), sorted by decreasing usage; and a call graph showing caller-callee relationships with estimated call counts and times propagated along paths. It uses statistical sampling (typically 10ms intervals via mcount calls), providing approximate but useful insights into hotspots.

Output includes an index, flat profile, call graph listings, and summaries. It's ideal for identifying bottlenecks in single-threaded C/C++ programs but less accurate for short runs or I/O-heavy code. Profiles can combine multiple gmon.out files from different executions.

CAVEATS

Requires compilation with -pg; sampling-based (10ms), so approximate for short/low-CPU runs; poor multi-threaded support; ignores dynamic allocation times; needs symbols (no -fomit-frame-pointer).

TYPICAL USAGE

gcc -pg -o prog prog.c
./prog # generates gmon.out
gprof prog gmon.out > profile.txt

OUTPUT STRUCTURE

Flat profile (time per function); Call graph (caller/callee arcs); Index by function.

HISTORY

Developed by the Free Software Foundation; first in GCC 1.25 (1987). Integrated into GNU Binutils since early 1990s; maintains compatibility with SVR4/4.4BSD profiling.

SEE ALSO

gcc(1), ld(1), nm(1), objdump(1), perf(1)

Copied to clipboard