Annotated Source - GNU gprof
Description: GNU gprof

Previous: Line-by-line, Up: Output

### 5.4 The Annotated Source Listing

`gprof`'s ‘`-A`’ option triggers an annotated source listing, which lists the program's source code, each function labeled with the number of times it was called. You may also need to specify the ‘`-I`’ option, if `gprof` can't find the source code files.

With older versions of `gcc` compiling with ‘`gcc ... -g -pg -a`’ augments your program with basic-block counting code, in addition to function counting code. This enables `gprof` to determine how many times each line of code was executed. With newer versions of `gcc` support for displaying basic-block counts is provided by the `gcov` program.

For example, consider the following function, taken from gzip, with line numbers added:

          1 ulg updcrc(s, n)
          2     uch *s;
          3     unsigned n;
          4 {
          5     register ulg c;
          6
          7     static ulg crc = (ulg)0xffffffffL;
          8
          9     if (s == NULL) {
         10         c = 0xffffffffL;
         11     } else {
         12         c = crc;
         13         if (n) do {
         14             c = crc_32_tab[...];
         15         } while (--n);
         16     }
         17     crc = c;
         18     return c ^ 0xffffffffL;
         19 }

`updcrc` has at least five basic-blocks. One is the function itself. The `if` statement on line 9 generates two more basic-blocks, one for each branch of the `if`. A fourth basic-block results from the `if` on line 13, and the contents of the `do` loop form the fifth basic-block. The compiler may also generate additional basic-blocks to handle various special cases.

A program augmented for basic-block counting can be analyzed with ‘`gprof -l -A`’. The ‘`-x`’ option is also helpful, to ensure that each line of code is labeled at least once. Here is `updcrc`'s annotated source listing for a sample `gzip` run:

                         ulg updcrc(s, n)
                             uch *s;
                             unsigned n;
                     2 ->{
                             register ulg c;

                             static ulg crc = (ulg)0xffffffffL;

                     2 ->    if (s == NULL) {
                     1 ->        c = 0xffffffffL;
                     1 ->    } else {
                     1 ->        c = crc;
                     1 ->        if (n) do {
                 26312 ->            c = crc_32_tab[...];
         26312,1,26311 ->        } while (--n);
                             }
                     2 ->    crc = c;
                     2 ->    return c ^ 0xffffffffL;
                     2 ->}

In this example, the function was called twice, passing once through each branch of the `if` statement. The body of the `do` loop was executed a total of 26312 times. Note how the `while` statement is annotated. It began execution 26312 times, once for each iteration through the loop. One of those times (the last time) it exited, while it branched back to the beginning of the loop 26311 times.