1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- This file documents the gprof profiler of the GNU system.
4
5Copyright (C) 1988-2021 Free Software Foundation, Inc.
6
7Permission is granted to copy, distribute and/or modify this document
8under the terms of the GNU Free Documentation License, Version 1.3
9or any later version published by the Free Software Foundation;
10with no Invariant Sections, with no Front-Cover Texts, and with no
11Back-Cover Texts.  A copy of the license is included in the
12section entitled "GNU Free Documentation License".
13 -->
14<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
15<head>
16<title>GNU gprof: Annotated Source</title>
17
18<meta name="description" content="GNU gprof: Annotated Source">
19<meta name="keywords" content="GNU gprof: Annotated Source">
20<meta name="resource-type" content="document">
21<meta name="distribution" content="global">
22<meta name="Generator" content="makeinfo">
23<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
24<link href="index.html#Top" rel="start" title="Top">
25<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
26<link href="Output.html#Output" rel="up" title="Output">
27<link href="Inaccuracy.html#Inaccuracy" rel="next" title="Inaccuracy">
28<link href="Line_002dby_002dline.html#Line_002dby_002dline" rel="previous" title="Line-by-line">
29<style type="text/css">
30<!--
31a.summary-letter {text-decoration: none}
32blockquote.smallquotation {font-size: smaller}
33div.display {margin-left: 3.2em}
34div.example {margin-left: 3.2em}
35div.indentedblock {margin-left: 3.2em}
36div.lisp {margin-left: 3.2em}
37div.smalldisplay {margin-left: 3.2em}
38div.smallexample {margin-left: 3.2em}
39div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
40div.smalllisp {margin-left: 3.2em}
41kbd {font-style:oblique}
42pre.display {font-family: inherit}
43pre.format {font-family: inherit}
44pre.menu-comment {font-family: serif}
45pre.menu-preformatted {font-family: serif}
46pre.smalldisplay {font-family: inherit; font-size: smaller}
47pre.smallexample {font-size: smaller}
48pre.smallformat {font-family: inherit; font-size: smaller}
49pre.smalllisp {font-size: smaller}
50span.nocodebreak {white-space:nowrap}
51span.nolinebreak {white-space:nowrap}
52span.roman {font-family:serif; font-weight:normal}
53span.sansserif {font-family:sans-serif; font-weight:normal}
54ul.no-bullet {list-style: none}
55-->
56</style>
57
58
59</head>
60
61<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
62<a name="Annotated-Source"></a>
63<div class="header">
64<p>
65Previous: <a href="Line_002dby_002dline.html#Line_002dby_002dline" accesskey="p" rel="previous">Line-by-line</a>, Up: <a href="Output.html#Output" accesskey="u" rel="up">Output</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
66</div>
67<hr>
68<a name="The-Annotated-Source-Listing"></a>
69<h3 class="section">5.4 The Annotated Source Listing</h3>
70
71<p><code>gprof</code>&rsquo;s &lsquo;<samp>-A</samp>&rsquo; option triggers an annotated source listing,
72which lists the program&rsquo;s source code, each function labeled with the
73number of times it was called.  You may also need to specify the
74&lsquo;<samp>-I</samp>&rsquo; option, if <code>gprof</code> can&rsquo;t find the source code files.
75</p>
76<p>With older versions of <code>gcc</code> compiling with &lsquo;<samp>gcc &hellip; -g
77-pg -a</samp>&rsquo; augments your program with basic-block counting code, in
78addition to function counting code.  This enables <code>gprof</code> to
79determine how many times each line of code was executed.  With newer
80versions of <code>gcc</code> support for displaying basic-block counts is
81provided by the <code>gcov</code> program.
82</p>
83<p>For example, consider the following function, taken from gzip,
84with line numbers added:
85</p>
86<div class="smallexample">
87<pre class="smallexample"> 1 ulg updcrc(s, n)
88 2     uch *s;
89 3     unsigned n;
90 4 {
91 5     register ulg c;
92 6
93 7     static ulg crc = (ulg)0xffffffffL;
94 8
95 9     if (s == NULL) {
9610         c = 0xffffffffL;
9711     } else {
9812         c = crc;
9913         if (n) do {
10014             c = crc_32_tab[...];
10115         } while (--n);
10216     }
10317     crc = c;
10418     return c ^ 0xffffffffL;
10519 }
106
107</pre></div>
108
109<p><code>updcrc</code> has at least five basic-blocks.
110One is the function itself.  The
111<code>if</code> statement on line 9 generates two more basic-blocks, one
112for each branch of the <code>if</code>.  A fourth basic-block results from
113the <code>if</code> on line 13, and the contents of the <code>do</code> loop form
114the fifth basic-block.  The compiler may also generate additional
115basic-blocks to handle various special cases.
116</p>
117<p>A program augmented for basic-block counting can be analyzed with
118&lsquo;<samp>gprof -l -A</samp>&rsquo;.
119The &lsquo;<samp>-x</samp>&rsquo; option is also helpful,
120to ensure that each line of code is labeled at least once.
121Here is <code>updcrc</code>&rsquo;s
122annotated source listing for a sample <code>gzip</code> run:
123</p>
124<div class="smallexample">
125<pre class="smallexample">                ulg updcrc(s, n)
126                    uch *s;
127                    unsigned n;
128            2 -&gt;{
129                    register ulg c;
130
131                    static ulg crc = (ulg)0xffffffffL;
132
133            2 -&gt;    if (s == NULL) {
134            1 -&gt;        c = 0xffffffffL;
135            1 -&gt;    } else {
136            1 -&gt;        c = crc;
137            1 -&gt;        if (n) do {
138        26312 -&gt;            c = crc_32_tab[...];
13926312,1,26311 -&gt;        } while (--n);
140                    }
141            2 -&gt;    crc = c;
142            2 -&gt;    return c ^ 0xffffffffL;
143            2 -&gt;}
144</pre></div>
145
146<p>In this example, the function was called twice, passing once through
147each branch of the <code>if</code> statement.  The body of the <code>do</code>
148loop was executed a total of 26312 times.  Note how the <code>while</code>
149statement is annotated.  It began execution 26312 times, once for
150each iteration through the loop.  One of those times (the last time)
151it exited, while it branched back to the beginning of the loop 26311 times.
152</p>
153<hr>
154<div class="header">
155<p>
156Previous: <a href="Line_002dby_002dline.html#Line_002dby_002dline" accesskey="p" rel="previous">Line-by-line</a>, Up: <a href="Output.html#Output" accesskey="u" rel="up">Output</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
157</div>
158
159
160
161</body>
162</html>
163