gprof: Annotated Source
1
1 5.4 The Annotated Source Listing
1 ================================
1
1 'gprof''s '-A' option triggers an annotated source listing, which lists
1 the program's source code, each function labeled with the number of
1 times it was called. You may also need to specify the '-I' option, if
1 'gprof' can't find the source code files.
1
1 With older versions of 'gcc' compiling with 'gcc ... -g -pg -a'
1 augments your program with basic-block counting code, in addition to
1 function counting code. This enables 'gprof' to determine how many
1 times each line of code was executed. With newer versions of 'gcc'
1 support for displaying basic-block counts is provided by the 'gcov'
1 program.
1
1 For example, consider the following function, taken from gzip, with
1 line numbers added:
1
1 1 ulg updcrc(s, n)
1 2 uch *s;
1 3 unsigned n;
1 4 {
1 5 register ulg c;
1 6
1 7 static ulg crc = (ulg)0xffffffffL;
1 8
1 9 if (s == NULL) {
1 10 c = 0xffffffffL;
1 11 } else {
1 12 c = crc;
1 13 if (n) do {
1 14 c = crc_32_tab[...];
1 15 } while (--n);
1 16 }
1 17 crc = c;
1 18 return c ^ 0xffffffffL;
1 19 }
1
1
1 'updcrc' has at least five basic-blocks. One is the function itself.
1 The 'if' statement on line 9 generates two more basic-blocks, one for
1 each branch of the 'if'. A fourth basic-block results from the 'if' on
1 line 13, and the contents of the 'do' loop form the fifth basic-block.
1 The compiler may also generate additional basic-blocks to handle various
1 special cases.
1
1 A program augmented for basic-block counting can be analyzed with
1 'gprof -l -A'. The '-x' option is also helpful, to ensure that each
1 line of code is labeled at least once. Here is 'updcrc''s annotated
1 source listing for a sample 'gzip' run:
1
1 ulg updcrc(s, n)
1 uch *s;
1 unsigned n;
1 2 ->{
1 register ulg c;
1
1 static ulg crc = (ulg)0xffffffffL;
1
1 2 -> if (s == NULL) {
1 1 -> c = 0xffffffffL;
1 1 -> } else {
1 1 -> c = crc;
1 1 -> if (n) do {
1 26312 -> c = crc_32_tab[...];
1 26312,1,26311 -> } while (--n);
1 }
1 2 -> crc = c;
1 2 -> return c ^ 0xffffffffL;
1 2 ->}
1
1 In this example, the function was called twice, passing once through
1 each branch of the 'if' statement. The body of the 'do' loop was
1 executed a total of 26312 times. Note how the 'while' statement is
1 annotated. It began execution 26312 times, once for each iteration
1 through the loop. One of those times (the last time) it exited, while
1 it branched back to the beginning of the loop 26311 times.
1