You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.3KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  15. <title>Annotated Source (GNU gprof)</title>
  16. <meta name="description" content="Annotated Source (GNU gprof)">
  17. <meta name="keywords" content="Annotated Source (GNU gprof)">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  30. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  48. span.roman {font-family: initial; font-weight: normal}
  49. span.sansserif {font-family: sans-serif; font-weight: normal}
  50. ul.no-bullet {list-style: none}
  51. -->
  52. </style>
  53. </head>
  54. <body lang="en">
  55. <a name="Annotated-Source"></a>
  56. <div class="header">
  57. <p>
  58. 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>
  59. </div>
  60. <hr>
  61. <a name="The-Annotated-Source-Listing"></a>
  62. <h3 class="section">5.4 The Annotated Source Listing</h3>
  63. <p><code>gprof</code>&rsquo;s &lsquo;<samp>-A</samp>&rsquo; option triggers an annotated source listing,
  64. which lists the program&rsquo;s source code, each function labeled with the
  65. number of times it was called. You may also need to specify the
  66. &lsquo;<samp>-I</samp>&rsquo; option, if <code>gprof</code> can&rsquo;t find the source code files.
  67. </p>
  68. <p>With older versions of <code>gcc</code> compiling with &lsquo;<samp>gcc &hellip; -g
  69. -pg -a</samp>&rsquo; augments your program with basic-block counting code, in
  70. addition to function counting code. This enables <code>gprof</code> to
  71. determine how many times each line of code was executed. With newer
  72. versions of <code>gcc</code> support for displaying basic-block counts is
  73. provided by the <code>gcov</code> program.
  74. </p>
  75. <p>For example, consider the following function, taken from gzip,
  76. with line numbers added:
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample"> 1 ulg updcrc(s, n)
  80. 2 uch *s;
  81. 3 unsigned n;
  82. 4 {
  83. 5 register ulg c;
  84. 6
  85. 7 static ulg crc = (ulg)0xffffffffL;
  86. 8
  87. 9 if (s == NULL) {
  88. 10 c = 0xffffffffL;
  89. 11 } else {
  90. 12 c = crc;
  91. 13 if (n) do {
  92. 14 c = crc_32_tab[...];
  93. 15 } while (--n);
  94. 16 }
  95. 17 crc = c;
  96. 18 return c ^ 0xffffffffL;
  97. 19 }
  98. </pre></div>
  99. <p><code>updcrc</code> has at least five basic-blocks.
  100. One is the function itself. The
  101. <code>if</code> statement on line 9 generates two more basic-blocks, one
  102. for each branch of the <code>if</code>. A fourth basic-block results from
  103. the <code>if</code> on line 13, and the contents of the <code>do</code> loop form
  104. the fifth basic-block. The compiler may also generate additional
  105. basic-blocks to handle various special cases.
  106. </p>
  107. <p>A program augmented for basic-block counting can be analyzed with
  108. &lsquo;<samp>gprof -l -A</samp>&rsquo;.
  109. The &lsquo;<samp>-x</samp>&rsquo; option is also helpful,
  110. to ensure that each line of code is labeled at least once.
  111. Here is <code>updcrc</code>&rsquo;s
  112. annotated source listing for a sample <code>gzip</code> run:
  113. </p>
  114. <div class="smallexample">
  115. <pre class="smallexample"> ulg updcrc(s, n)
  116. uch *s;
  117. unsigned n;
  118. 2 -&gt;{
  119. register ulg c;
  120. static ulg crc = (ulg)0xffffffffL;
  121. 2 -&gt; if (s == NULL) {
  122. 1 -&gt; c = 0xffffffffL;
  123. 1 -&gt; } else {
  124. 1 -&gt; c = crc;
  125. 1 -&gt; if (n) do {
  126. 26312 -&gt; c = crc_32_tab[...];
  127. 26312,1,26311 -&gt; } while (--n);
  128. }
  129. 2 -&gt; crc = c;
  130. 2 -&gt; return c ^ 0xffffffffL;
  131. 2 -&gt;}
  132. </pre></div>
  133. <p>In this example, the function was called twice, passing once through
  134. each branch of the <code>if</code> statement. The body of the <code>do</code>
  135. loop was executed a total of 26312 times. Note how the <code>while</code>
  136. statement is annotated. It began execution 26312 times, once for
  137. each iteration through the loop. One of those times (the last time)
  138. it exited, while it branched back to the beginning of the loop 26311 times.
  139. </p>
  140. <hr>
  141. <div class="header">
  142. <p>
  143. 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>
  144. </div>
  145. </body>
  146. </html>