Compiling.html 8.0 KB

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