Annotated-Source.html 6.4 KB

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