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.

Compiling.html 7.9KB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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>Compiling (GNU gprof)</title>
  16. <meta name="description" content="Compiling (GNU gprof)">
  17. <meta name="keywords" content="Compiling (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="index.html#Top" rel="up" title="Top">
  24. <link href="Executing.html#Executing" rel="next" title="Executing">
  25. <link href="Introduction.html#Introduction" rel="prev" title="Introduction">
  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="Compiling"></a>
  56. <div class="header">
  57. <p>
  58. Next: <a href="Executing.html#Executing" accesskey="n" rel="next">Executing</a>, Previous: <a href="Introduction.html#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  59. </div>
  60. <hr>
  61. <a name="Compiling-a-Program-for-Profiling"></a>
  62. <h2 class="chapter">2 Compiling a Program for Profiling</h2>
  63. <p>The first step in generating profile information for your program is
  64. to compile and link it with profiling enabled.
  65. </p>
  66. <p>To compile a source file for profiling, specify the &lsquo;<samp>-pg</samp>&rsquo; option when
  67. you run the compiler. (This is in addition to the options you normally
  68. use.)
  69. </p>
  70. <p>To link the program for profiling, if you use a compiler such as <code>cc</code>
  71. to do the linking, simply specify &lsquo;<samp>-pg</samp>&rsquo; in addition to your usual
  72. options. The same option, &lsquo;<samp>-pg</samp>&rsquo;, alters either compilation or linking
  73. to do what is necessary for profiling. Here are examples:
  74. </p>
  75. <div class="example">
  76. <pre class="example">cc -g -c myprog.c utils.c -pg
  77. cc -o myprog myprog.o utils.o -pg
  78. </pre></div>
  79. <p>The &lsquo;<samp>-pg</samp>&rsquo; option also works with a command that both compiles and links:
  80. </p>
  81. <div class="example">
  82. <pre class="example">cc -o myprog myprog.c utils.c -g -pg
  83. </pre></div>
  84. <p>Note: The &lsquo;<samp>-pg</samp>&rsquo; option must be part of your compilation options
  85. as well as your link options. If it is not then no call-graph data
  86. will be gathered and when you run <code>gprof</code> you will get an error
  87. message like this:
  88. </p>
  89. <div class="example">
  90. <pre class="example">gprof: gmon.out file is missing call-graph data
  91. </pre></div>
  92. <p>If you add the &lsquo;<samp>-Q</samp>&rsquo; switch to suppress the printing of the call
  93. graph data you will still be able to see the time samples:
  94. </p>
  95. <div class="example">
  96. <pre class="example">Flat profile:
  97. Each sample counts as 0.01 seconds.
  98. % cumulative self self total
  99. time seconds seconds calls Ts/call Ts/call name
  100. 44.12 0.07 0.07 zazLoop
  101. 35.29 0.14 0.06 main
  102. 20.59 0.17 0.04 bazMillion
  103. </pre></div>
  104. <p>If you run the linker <code>ld</code> directly instead of through a compiler
  105. such as <code>cc</code>, you may have to specify a profiling startup file
  106. <samp>gcrt0.o</samp> as the first input file instead of the usual startup
  107. file <samp>crt0.o</samp>. In addition, you would probably want to
  108. specify the profiling C library, <samp>libc_p.a</samp>, by writing
  109. &lsquo;<samp>-lc_p</samp>&rsquo; instead of the usual &lsquo;<samp>-lc</samp>&rsquo;. This is not absolutely
  110. necessary, but doing this gives you number-of-calls information for
  111. standard library functions such as <code>read</code> and <code>open</code>. For
  112. example:
  113. </p>
  114. <div class="example">
  115. <pre class="example">ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
  116. </pre></div>
  117. <p>If you are running the program on a system which supports shared
  118. libraries you may run into problems with the profiling support code in
  119. a shared library being called before that library has been fully
  120. initialised. This is usually detected by the program encountering a
  121. segmentation fault as soon as it is run. The solution is to link
  122. against a static version of the library containing the profiling
  123. support code, which for <code>gcc</code> users can be done via the
  124. &lsquo;<samp>-static</samp>&rsquo; or &lsquo;<samp>-static-libgcc</samp>&rsquo; command-line option. For
  125. example:
  126. </p>
  127. <div class="example">
  128. <pre class="example">gcc -g -pg -static-libgcc myprog.c utils.c -o myprog
  129. </pre></div>
  130. <p>If you compile only some of the modules of the program with &lsquo;<samp>-pg</samp>&rsquo;, you
  131. can still profile the program, but you won&rsquo;t get complete information about
  132. the modules that were compiled without &lsquo;<samp>-pg</samp>&rsquo;. The only information
  133. you get for the functions in those modules is the total time spent in them;
  134. there is no record of how many times they were called, or from where. This
  135. will not affect the flat profile (except that the <code>calls</code> field for
  136. the functions will be blank), but will greatly reduce the usefulness of the
  137. call graph.
  138. </p>
  139. <p>If you wish to perform line-by-line profiling you should use the
  140. <code>gcov</code> tool instead of <code>gprof</code>. See that tool&rsquo;s manual or
  141. info pages for more details of how to do this.
  142. </p>
  143. <p>Note, older versions of <code>gcc</code> produce line-by-line profiling
  144. information that works with <code>gprof</code> rather than <code>gcov</code> so
  145. there is still support for displaying this kind of information in
  146. <code>gprof</code>. See <a href="Line_002dby_002dline.html#Line_002dby_002dline">Line-by-line Profiling</a>.
  147. </p>
  148. <p>It also worth noting that <code>gcc</code> implements a
  149. &lsquo;<samp>-finstrument-functions</samp>&rsquo; command-line option which will insert
  150. calls to special user supplied instrumentation routines at the entry
  151. and exit of every function in their program. This can be used to
  152. implement an alternative profiling scheme.
  153. </p>
  154. <hr>
  155. <div class="header">
  156. <p>
  157. Next: <a href="Executing.html#Executing" accesskey="n" rel="next">Executing</a>, Previous: <a href="Introduction.html#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>]</p>
  158. </div>
  159. </body>
  160. </html>