source: branches/libc-0.6/src/binutils/gprof/gprof.texi@ 3102

Last change on this file since 3102 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 104.6 KB
Line 
1\input texinfo @c -*-texinfo-*-
2@setfilename gprof.info
3@c Copyright 1988, 1992, 1993, 1998, 1999, 2000, 2001
4@c Free Software Foundation, Inc.
5@settitle GNU gprof
6@setchapternewpage odd
7
8@ifinfo
9@c This is a dir.info fragment to support semi-automated addition of
10@c manuals to an info tree. [email protected] is developing this facility.
11@format
12START-INFO-DIR-ENTRY
13* gprof: (gprof). Profiling your program's execution
14END-INFO-DIR-ENTRY
15@end format
16@end ifinfo
17
18@ifinfo
19This file documents the gprof profiler of the GNU system.
20
21@c man begin COPYRIGHT
22Copyright (C) 1988, 92, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc.
23
24Permission is granted to copy, distribute and/or modify this document
25under the terms of the GNU Free Documentation License, Version 1.1
26or any later version published by the Free Software Foundation;
27with no Invariant Sections, with no Front-Cover Texts, and with no
28Back-Cover Texts. A copy of the license is included in the
29section entitled "GNU Free Documentation License".
30
31@c man end
32
33@ignore
34Permission is granted to process this file through Tex and print the
35results, provided the printed document carries copying permission
36notice identical to this one except for the removal of this paragraph
37(this paragraph not being relevant to the printed manual).
38
39@end ignore
40@end ifinfo
41
42@finalout
43@smallbook
44
45@titlepage
46@title GNU gprof
47@subtitle The @sc{gnu} Profiler
48@author Jay Fenlason and Richard Stallman
49
50@page
51
52This manual describes the @sc{gnu} profiler, @code{gprof}, and how you
53can use it to determine which parts of a program are taking most of the
54execution time. We assume that you know how to write, compile, and
55execute programs. @sc{gnu} @code{gprof} was written by Jay Fenlason.
56
57@vskip 0pt plus 1filll
58Copyright @copyright{} 1988, 92, 97, 98, 99, 2000 Free Software Foundation, Inc.
59
60 Permission is granted to copy, distribute and/or modify this document
61 under the terms of the GNU Free Documentation License, Version 1.1
62 or any later version published by the Free Software Foundation;
63 with no Invariant Sections, with no Front-Cover Texts, and with no
64 Back-Cover Texts. A copy of the license is included in the
65 section entitled "GNU Free Documentation License".
66
67@end titlepage
68
69@ifnottex
70@node Top
71@top Profiling a Program: Where Does It Spend Its Time?
72
73This manual describes the @sc{gnu} profiler, @code{gprof}, and how you
74can use it to determine which parts of a program are taking most of the
75execution time. We assume that you know how to write, compile, and
76execute programs. @sc{gnu} @code{gprof} was written by Jay Fenlason.
77
78This document is distributed under the terms of the GNU Free
79Documentation License. A copy of the license is included in the
80section entitled "GNU Free Documentation License".
81
82@menu
83* Introduction:: What profiling means, and why it is useful.
84
85* Compiling:: How to compile your program for profiling.
86* Executing:: Executing your program to generate profile data
87* Invoking:: How to run @code{gprof}, and its options
88
89* Output:: Interpreting @code{gprof}'s output
90
91* Inaccuracy:: Potential problems you should be aware of
92* How do I?:: Answers to common questions
93* Incompatibilities:: (between @sc{gnu} @code{gprof} and Unix @code{gprof}.)
94* Details:: Details of how profiling is done
95* GNU Free Documentation License:: GNU Free Documentation License
96@end menu
97@end ifnottex
98
99@node Introduction
100@chapter Introduction to Profiling
101
102@ifset man
103@c man title gprof display call graph profile data
104
105@smallexample
106@c man begin SYNOPSIS
107gprof [ -[abcDhilLsTvwxyz] ] [ -[ACeEfFJnNOpPqQZ][@var{name}] ]
108 [ -I @var{dirs} ] [ -d[@var{num}] ] [ -k @var{from/to} ]
109 [ -m @var{min-count} ] [ -t @var{table-length} ]
110 [ --[no-]annotated-source[=@var{name}] ]
111 [ --[no-]exec-counts[=@var{name}] ]
112 [ --[no-]flat-profile[=@var{name}] ] [ --[no-]graph[=@var{name}] ]
113 [ --[no-]time=@var{name}] [ --all-lines ] [ --brief ]
114 [ --debug[=@var{level}] ] [ --function-ordering ]
115 [ --file-ordering ] [ --directory-path=@var{dirs} ]
116 [ --display-unused-functions ] [ --file-format=@var{name} ]
117 [ --file-info ] [ --help ] [ --line ] [ --min-count=@var{n} ]
118 [ --no-static ] [ --print-path ] [ --separate-files ]
119 [ --static-call-graph ] [ --sum ] [ --table-length=@var{len} ]
120 [ --traditional ] [ --version ] [ --width=@var{n} ]
121 [ --ignore-non-functions ] [ --demangle[=@var{STYLE}] ]
122 [ --no-demangle ] [ @var{image-file} ] [ @var{profile-file} @dots{} ]
123@c man end
124@end smallexample
125
126@c man begin DESCRIPTION
127@code{gprof} produces an execution profile of C, Pascal, or Fortran77
128programs. The effect of called routines is incorporated in the profile
129of each caller. The profile data is taken from the call graph profile file
130(@file{gmon.out} default) which is created by programs
131that are compiled with the @samp{-pg} option of
132@code{cc}, @code{pc}, and @code{f77}.
133The @samp{-pg} option also links in versions of the library routines
134that are compiled for profiling. @code{Gprof} reads the given object
135file (the default is @code{a.out}) and establishes the relation between
136its symbol table and the call graph profile from @file{gmon.out}.
137If more than one profile file is specified, the @code{gprof}
138output shows the sum of the profile information in the given profile files.
139
140@code{Gprof} calculates the amount of time spent in each routine.
141Next, these times are propagated along the edges of the call graph.
142Cycles are discovered, and calls into a cycle are made to share the time
143of the cycle.
144
145@c man end
146
147@c man begin BUGS
148The granularity of the sampling is shown, but remains
149statistical at best.
150We assume that the time for each execution of a function
151can be expressed by the total time for the function divided
152by the number of times the function is called.
153Thus the time propagated along the call graph arcs to the function's
154parents is directly proportional to the number of times that
155arc is traversed.
156
157Parents that are not themselves profiled will have the time of
158their profiled children propagated to them, but they will appear
159to be spontaneously invoked in the call graph listing, and will
160not have their time propagated further.
161Similarly, signal catchers, even though profiled, will appear
162to be spontaneous (although for more obscure reasons).
163Any profiled children of signal catchers should have their times
164propagated properly, unless the signal catcher was invoked during
165the execution of the profiling routine, in which case all is lost.
166
167The profiled program must call @code{exit}(2)
168or return normally for the profiling information to be saved
169in the @file{gmon.out} file.
170@c man end
171
172@c man begin FILES
173@table @code
174@item @file{a.out}
175the namelist and text space.
176@item @file{gmon.out}
177dynamic call graph and profile.
178@item @file{gmon.sum}
179summarized dynamic call graph and profile.
180@end table
181@c man end
182
183@c man begin SEEALSO
184monitor(3), profil(2), cc(1), prof(1), and the Info entry for @file{gprof}.
185
186``An Execution Profiler for Modular Programs'',
187by S. Graham, P. Kessler, M. McKusick;
188Software - Practice and Experience,
189Vol. 13, pp. 671-685, 1983.
190
191``gprof: A Call Graph Execution Profiler'',
192by S. Graham, P. Kessler, M. McKusick;
193Proceedings of the SIGPLAN '82 Symposium on Compiler Construction,
194SIGPLAN Notices, Vol. 17, No 6, pp. 120-126, June 1982.
195@c man end
196@end ifset
197
198Profiling allows you to learn where your program spent its time and which
199functions called which other functions while it was executing. This
200information can show you which pieces of your program are slower than you
201expected, and might be candidates for rewriting to make your program
202execute faster. It can also tell you which functions are being called more
203or less often than you expected. This may help you spot bugs that had
204otherwise been unnoticed.
205
206Since the profiler uses information collected during the actual execution
207of your program, it can be used on programs that are too large or too
208complex to analyze by reading the source. However, how your program is run
209will affect the information that shows up in the profile data. If you
210don't use some feature of your program while it is being profiled, no
211profile information will be generated for that feature.
212
213Profiling has several steps:
214
215@itemize @bullet
216@item
217You must compile and link your program with profiling enabled.
218@xref{Compiling}.
219
220@item
221You must execute your program to generate a profile data file.
222@xref{Executing}.
223
224@item
225You must run @code{gprof} to analyze the profile data.
226@xref{Invoking}.
227@end itemize
228
229The next three chapters explain these steps in greater detail.
230
231@c man begin DESCRIPTION
232
233Several forms of output are available from the analysis.
234
235The @dfn{flat profile} shows how much time your program spent in each function,
236and how many times that function was called. If you simply want to know
237which functions burn most of the cycles, it is stated concisely here.
238@xref{Flat Profile}.
239
240The @dfn{call graph} shows, for each function, which functions called it, which
241other functions it called, and how many times. There is also an estimate
242of how much time was spent in the subroutines of each function. This can
243suggest places where you might try to eliminate function calls that use a
244lot of time. @xref{Call Graph}.
245
246The @dfn{annotated source} listing is a copy of the program's
247source code, labeled with the number of times each line of the
248program was executed. @xref{Annotated Source}.
249@c man end
250
251To better understand how profiling works, you may wish to read
252a description of its implementation.
253@xref{Implementation}.
254
255@node Compiling
256@chapter Compiling a Program for Profiling
257
258The first step in generating profile information for your program is
259to compile and link it with profiling enabled.
260
261To compile a source file for profiling, specify the @samp{-pg} option when
262you run the compiler. (This is in addition to the options you normally
263use.)
264
265To link the program for profiling, if you use a compiler such as @code{cc}
266to do the linking, simply specify @samp{-pg} in addition to your usual
267options. The same option, @samp{-pg}, alters either compilation or linking
268to do what is necessary for profiling. Here are examples:
269
270@example
271cc -g -c myprog.c utils.c -pg
272cc -o myprog myprog.o utils.o -pg
273@end example
274
275The @samp{-pg} option also works with a command that both compiles and links:
276
277@example
278cc -o myprog myprog.c utils.c -g -pg
279@end example
280
281If you run the linker @code{ld} directly instead of through a compiler
282such as @code{cc}, you may have to specify a profiling startup file
283@file{gcrt0.o} as the first input file instead of the usual startup
284file @file{crt0.o}. In addition, you would probably want to
285specify the profiling C library, @file{libc_p.a}, by writing
286@samp{-lc_p} instead of the usual @samp{-lc}. This is not absolutely
287necessary, but doing this gives you number-of-calls information for
288standard library functions such as @code{read} and @code{open}. For
289example:
290
291@example
292ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
293@end example
294
295If you compile only some of the modules of the program with @samp{-pg}, you
296can still profile the program, but you won't get complete information about
297the modules that were compiled without @samp{-pg}. The only information
298you get for the functions in those modules is the total time spent in them;
299there is no record of how many times they were called, or from where. This
300will not affect the flat profile (except that the @code{calls} field for
301the functions will be blank), but will greatly reduce the usefulness of the
302call graph.
303
304If you wish to perform line-by-line profiling,
305you will also need to specify the @samp{-g} option,
306instructing the compiler to insert debugging symbols into the program
307that match program addresses to source code lines.
308@xref{Line-by-line}.
309
310In addition to the @samp{-pg} and @samp{-g} options,
311you may also wish to specify the @samp{-a} option when compiling.
312This will instrument
313the program to perform basic-block counting. As the program runs,
314it will count how many times it executed each branch of each @samp{if}
315statement, each iteration of each @samp{do} loop, etc. This will
316enable @code{gprof} to construct an annotated source code
317listing showing how many times each line of code was executed.
318
319@node Executing
320@chapter Executing the Program
321
322Once the program is compiled for profiling, you must run it in order to
323generate the information that @code{gprof} needs. Simply run the program
324as usual, using the normal arguments, file names, etc. The program should
325run normally, producing the same output as usual. It will, however, run
326somewhat slower than normal because of the time spent collecting and the
327writing the profile data.
328
329The way you run the program---the arguments and input that you give
330it---may have a dramatic effect on what the profile information shows. The
331profile data will describe the parts of the program that were activated for
332the particular input you use. For example, if the first command you give
333to your program is to quit, the profile data will show the time used in
334initialization and in cleanup, but not much else.
335
336Your program will write the profile data into a file called @file{gmon.out}
337just before exiting. If there is already a file called @file{gmon.out},
338its contents are overwritten. There is currently no way to tell the
339program to write the profile data under a different name, but you can rename
340the file afterward if you are concerned that it may be overwritten.
341
342In order to write the @file{gmon.out} file properly, your program must exit
343normally: by returning from @code{main} or by calling @code{exit}. Calling
344the low-level function @code{_exit} does not write the profile data, and
345neither does abnormal termination due to an unhandled signal.
346
347The @file{gmon.out} file is written in the program's @emph{current working
348directory} at the time it exits. This means that if your program calls
349@code{chdir}, the @file{gmon.out} file will be left in the last directory
350your program @code{chdir}'d to. If you don't have permission to write in
351this directory, the file is not written, and you will get an error message.
352
353Older versions of the @sc{gnu} profiling library may also write a file
354called @file{bb.out}. This file, if present, contains an human-readable
355listing of the basic-block execution counts. Unfortunately, the
356appearance of a human-readable @file{bb.out} means the basic-block
357counts didn't get written into @file{gmon.out}.
358The Perl script @code{bbconv.pl}, included with the @code{gprof}
359source distribution, will convert a @file{bb.out} file into
360a format readable by @code{gprof}. Invoke it like this:
361
362@smallexample
363bbconv.pl < bb.out > @var{bh-data}
364@end smallexample
365
366This translates the information in @file{bb.out} into a form that
367@code{gprof} can understand. But you still need to tell @code{gprof}
368about the existence of this translated information. To do that, include
369@var{bb-data} on the @code{gprof} command line, @emph{along with
370@file{gmon.out}}, like this:
371
372@smallexample
373gprof @var{options} @var{executable-file} gmon.out @var{bb-data} [@var{yet-more-profile-data-files}@dots{}] [> @var{outfile}]
374@end smallexample
375
376@node Invoking
377@chapter @code{gprof} Command Summary
378
379After you have a profile data file @file{gmon.out}, you can run @code{gprof}
380to interpret the information in it. The @code{gprof} program prints a
381flat profile and a call graph on standard output. Typically you would
382redirect the output of @code{gprof} into a file with @samp{>}.
383
384You run @code{gprof} like this:
385
386@smallexample
387gprof @var{options} [@var{executable-file} [@var{profile-data-files}@dots{}]] [> @var{outfile}]
388@end smallexample
389
390@noindent
391Here square-brackets indicate optional arguments.
392
393If you omit the executable file name, the file @file{a.out} is used. If
394you give no profile data file name, the file @file{gmon.out} is used. If
395any file is not in the proper format, or if the profile data file does not
396appear to belong to the executable file, an error message is printed.
397
398You can give more than one profile data file by entering all their names
399after the executable file name; then the statistics in all the data files
400are summed together.
401
402The order of these options does not matter.
403
404@menu
405* Output Options:: Controlling @code{gprof}'s output style
406* Analysis Options:: Controlling how @code{gprof} analyses its data
407* Miscellaneous Options::
408* Deprecated Options:: Options you no longer need to use, but which
409 have been retained for compatibility
410* Symspecs:: Specifying functions to include or exclude
411@end menu
412
413@node Output Options,Analysis Options,,Invoking
414@section Output Options
415
416@c man begin OPTIONS
417These options specify which of several output formats
418@code{gprof} should produce.
419
420Many of these options take an optional @dfn{symspec} to specify
421functions to be included or excluded. These options can be
422specified multiple times, with different symspecs, to include
423or exclude sets of symbols. @xref{Symspecs}.
424
425Specifying any of these options overrides the default (@samp{-p -q}),
426which prints a flat profile and call graph analysis
427for all functions.
428
429@table @code
430
431@item -A[@var{symspec}]
432@itemx --annotated-source[=@var{symspec}]
433The @samp{-A} option causes @code{gprof} to print annotated source code.
434If @var{symspec} is specified, print output only for matching symbols.
435@xref{Annotated Source}.
436
437@item -b
438@itemx --brief
439If the @samp{-b} option is given, @code{gprof} doesn't print the
440verbose blurbs that try to explain the meaning of all of the fields in
441the tables. This is useful if you intend to print out the output, or
442are tired of seeing the blurbs.
443
444@item -C[@var{symspec}]
445@itemx --exec-counts[=@var{symspec}]
446The @samp{-C} option causes @code{gprof} to
447print a tally of functions and the number of times each was called.
448If @var{symspec} is specified, print tally only for matching symbols.
449
450If the profile data file contains basic-block count records, specifying
451the @samp{-l} option, along with @samp{-C}, will cause basic-block
452execution counts to be tallied and displayed.
453
454@item -i
455@itemx --file-info
456The @samp{-i} option causes @code{gprof} to display summary information
457about the profile data file(s) and then exit. The number of histogram,
458call graph, and basic-block count records is displayed.
459
460@item -I @var{dirs}
461@itemx --directory-path=@var{dirs}
462The @samp{-I} option specifies a list of search directories in
463which to find source files. Environment variable @var{GPROF_PATH}
464can also be used to convey this information.
465Used mostly for annotated source output.
466
467@item -J[@var{symspec}]
468@itemx --no-annotated-source[=@var{symspec}]
469The @samp{-J} option causes @code{gprof} not to
470print annotated source code.
471If @var{symspec} is specified, @code{gprof} prints annotated source,
472but excludes matching symbols.
473
474@item -L
475@itemx --print-path
476Normally, source filenames are printed with the path
477component suppressed. The @samp{-L} option causes @code{gprof}
478to print the full pathname of
479source filenames, which is determined
480from symbolic debugging information in the image file
481and is relative to the directory in which the compiler
482was invoked.
483
484@item -p[@var{symspec}]
485@itemx --flat-profile[=@var{symspec}]
486The @samp{-p} option causes @code{gprof} to print a flat profile.
487If @var{symspec} is specified, print flat profile only for matching symbols.
488@xref{Flat Profile}.
489
490@item -P[@var{symspec}]
491@itemx --no-flat-profile[=@var{symspec}]
492The @samp{-P} option causes @code{gprof} to suppress printing a flat profile.
493If @var{symspec} is specified, @code{gprof} prints a flat profile,
494but excludes matching symbols.
495
496@item -q[@var{symspec}]
497@itemx --graph[=@var{symspec}]
498The @samp{-q} option causes @code{gprof} to print the call graph analysis.
499If @var{symspec} is specified, print call graph only for matching symbols
500and their children.
501@xref{Call Graph}.
502
503@item -Q[@var{symspec}]
504@itemx --no-graph[=@var{symspec}]
505The @samp{-Q} option causes @code{gprof} to suppress printing the
506call graph.
507If @var{symspec} is specified, @code{gprof} prints a call graph,
508but excludes matching symbols.
509
510@item -y
511@itemx --separate-files
512This option affects annotated source output only.
513Normally, @code{gprof} prints annotated source files
514to standard-output. If this option is specified,
515annotated source for a file named @file{path/@var{filename}}
516is generated in the file @file{@var{filename}-ann}. If the underlying
517filesystem would truncate @file{@var{filename}-ann} so that it
518overwrites the original @file{@var{filename}}, @code{gprof} generates
519annotated source in the file @file{@var{filename}.ann} instead (if the
520original file name has an extension, that extension is @emph{replaced}
521with @file{.ann}).
522
523@item -Z[@var{symspec}]
524@itemx --no-exec-counts[=@var{symspec}]
525The @samp{-Z} option causes @code{gprof} not to
526print a tally of functions and the number of times each was called.
527If @var{symspec} is specified, print tally, but exclude matching symbols.
528
529@item --function-ordering
530The @samp{--function-ordering} option causes @code{gprof} to print a
531suggested function ordering for the program based on profiling data.
532This option suggests an ordering which may improve paging, tlb and
533cache behavior for the program on systems which support arbitrary
534ordering of functions in an executable.
535
536The exact details of how to force the linker to place functions
537in a particular order is system dependent and out of the scope of this
538manual.
539
540@item --file-ordering @var{map_file}
541The @samp{--file-ordering} option causes @code{gprof} to print a
542suggested .o link line ordering for the program based on profiling data.
543This option suggests an ordering which may improve paging, tlb and
544cache behavior for the program on systems which do not support arbitrary
545ordering of functions in an executable.
546
547Use of the @samp{-a} argument is highly recommended with this option.
548
549The @var{map_file} argument is a pathname to a file which provides
550function name to object file mappings. The format of the file is similar to
551the output of the program @code{nm}.
552
553@smallexample
554@group
555c-parse.o:00000000 T yyparse
556c-parse.o:00000004 C yyerrflag
557c-lang.o:00000000 T maybe_objc_method_name
558c-lang.o:00000000 T print_lang_statistics
559c-lang.o:00000000 T recognize_objc_keyword
560c-decl.o:00000000 T print_lang_identifier
561c-decl.o:00000000 T print_lang_type
562@dots{}
563
564@end group
565@end smallexample
566
567To create a @var{map_file} with @sc{gnu} @code{nm}, type a command like
568@kbd{nm --extern-only --defined-only -v --print-file-name program-name}.
569
570@item -T
571@itemx --traditional
572The @samp{-T} option causes @code{gprof} to print its output in
573``traditional'' BSD style.
574
575@item -w @var{width}
576@itemx --width=@var{width}
577Sets width of output lines to @var{width}.
578Currently only used when printing the function index at the bottom
579of the call graph.
580
581@item -x
582@itemx --all-lines
583This option affects annotated source output only.
584By default, only the lines at the beginning of a basic-block
585are annotated. If this option is specified, every line in
586a basic-block is annotated by repeating the annotation for the
587first line. This behavior is similar to @code{tcov}'s @samp{-a}.
588
589@item --demangle[=@var{style}]
590@itemx --no-demangle
591These options control whether C++ symbol names should be demangled when
592printing output. The default is to demangle symbols. The
593@code{--no-demangle} option may be used to turn off demangling. Different
594compilers have different mangling styles. The optional demangling style
595argument can be used to choose an appropriate demangling style for your
596compiler.
597@end table
598
599@node Analysis Options,Miscellaneous Options,Output Options,Invoking
600@section Analysis Options
601
602@table @code
603
604@item -a
605@itemx --no-static
606The @samp{-a} option causes @code{gprof} to suppress the printing of
607statically declared (private) functions. (These are functions whose
608names are not listed as global, and which are not visible outside the
609file/function/block where they were defined.) Time spent in these
610functions, calls to/from them, etc, will all be attributed to the
611function that was loaded directly before it in the executable file.
612@c This is compatible with Unix @code{gprof}, but a bad idea.
613This option affects both the flat profile and the call graph.
614
615@item -c
616@itemx --static-call-graph
617The @samp{-c} option causes the call graph of the program to be
618augmented by a heuristic which examines the text space of the object
619file and identifies function calls in the binary machine code.
620Since normal call graph records are only generated when functions are
621entered, this option identifies children that could have been called,
622but never were. Calls to functions that were not compiled with
623profiling enabled are also identified, but only if symbol table
624entries are present for them.
625Calls to dynamic library routines are typically @emph{not} found
626by this option.
627Parents or children identified via this heuristic
628are indicated in the call graph with call counts of @samp{0}.
629
630@item -D
631@itemx --ignore-non-functions
632The @samp{-D} option causes @code{gprof} to ignore symbols which
633are not known to be functions. This option will give more accurate
634profile data on systems where it is supported (Solaris and HPUX for
635example).
636
637@item -k @var{from}/@var{to}
638The @samp{-k} option allows you to delete from the call graph any arcs from
639symbols matching symspec @var{from} to those matching symspec @var{to}.
640
641@item -l
642@itemx --line
643The @samp{-l} option enables line-by-line profiling, which causes
644histogram hits to be charged to individual source code lines,
645instead of functions.
646If the program was compiled with basic-block counting enabled,
647this option will also identify how many times each line of
648code was executed.
649While line-by-line profiling can help isolate where in a large function
650a program is spending its time, it also significantly increases
651the running time of @code{gprof}, and magnifies statistical
652inaccuracies.
653@xref{Sampling Error}.
654
655@item -m @var{num}
656@itemx --min-count=@var{num}
657This option affects execution count output only.
658Symbols that are executed less than @var{num} times are suppressed.
659
660@item -n[@var{symspec}]
661@itemx --time[=@var{symspec}]
662The @samp{-n} option causes @code{gprof}, in its call graph analysis,
663to only propagate times for symbols matching @var{symspec}.
664
665@item -N[@var{symspec}]
666@itemx --no-time[=@var{symspec}]
667The @samp{-n} option causes @code{gprof}, in its call graph analysis,
668not to propagate times for symbols matching @var{symspec}.
669
670@item -z
671@itemx --display-unused-functions
672If you give the @samp{-z} option, @code{gprof} will mention all
673functions in the flat profile, even those that were never called, and
674that had no time spent in them. This is useful in conjunction with the
675@samp{-c} option for discovering which routines were never called.
676
677@end table
678
679@node Miscellaneous Options,Deprecated Options,Analysis Options,Invoking
680@section Miscellaneous Options
681
682@table @code
683
684@item -d[@var{num}]
685@itemx --debug[=@var{num}]
686The @samp{-d @var{num}} option specifies debugging options.
687If @var{num} is not specified, enable all debugging.
688@xref{Debugging}.
689
690@item -O@var{name}
691@itemx --file-format=@var{name}
692Selects the format of the profile data files. Recognized formats are
693@samp{auto} (the default), @samp{bsd}, @samp{4.4bsd}, @samp{magic}, and
694@samp{prof} (not yet supported).
695
696@item -s
697@itemx --sum
698The @samp{-s} option causes @code{gprof} to summarize the information
699in the profile data files it read in, and write out a profile data
700file called @file{gmon.sum}, which contains all the information from
701the profile data files that @code{gprof} read in. The file @file{gmon.sum}
702may be one of the specified input files; the effect of this is to
703merge the data in the other input files into @file{gmon.sum}.
704
705Eventually you can run @code{gprof} again without @samp{-s} to analyze the
706cumulative data in the file @file{gmon.sum}.
707
708@item -v
709@itemx --version
710The @samp{-v} flag causes @code{gprof} to print the current version
711number, and then exit.
712
713@end table
714
715@node Deprecated Options,Symspecs,Miscellaneous Options,Invoking
716@section Deprecated Options
717
718@table @code
719
720These options have been replaced with newer versions that use symspecs.
721
722@item -e @var{function_name}
723The @samp{-e @var{function}} option tells @code{gprof} to not print
724information about the function @var{function_name} (and its
725children@dots{}) in the call graph. The function will still be listed
726as a child of any functions that call it, but its index number will be
727shown as @samp{[not printed]}. More than one @samp{-e} option may be
728given; only one @var{function_name} may be indicated with each @samp{-e}
729option.
730
731@item -E @var{function_name}
732The @code{-E @var{function}} option works like the @code{-e} option, but
733time spent in the function (and children who were not called from
734anywhere else), will not be used to compute the percentages-of-time for
735the call graph. More than one @samp{-E} option may be given; only one
736@var{function_name} may be indicated with each @samp{-E} option.
737
738@item -f @var{function_name}
739The @samp{-f @var{function}} option causes @code{gprof} to limit the
740call graph to the function @var{function_name} and its children (and
741their children@dots{}). More than one @samp{-f} option may be given;
742only one @var{function_name} may be indicated with each @samp{-f}
743option.
744
745@item -F @var{function_name}
746The @samp{-F @var{function}} option works like the @code{-f} option, but
747only time spent in the function and its children (and their
748children@dots{}) will be used to determine total-time and
749percentages-of-time for the call graph. More than one @samp{-F} option
750may be given; only one @var{function_name} may be indicated with each
751@samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
752
753@end table
754
755@c man end
756
757Note that only one function can be specified with each @code{-e},
758@code{-E}, @code{-f} or @code{-F} option. To specify more than one
759function, use multiple options. For example, this command:
760
761@example
762gprof -e boring -f foo -f bar myprogram > gprof.output
763@end example
764
765@noindent
766lists in the call graph all functions that were reached from either
767@code{foo} or @code{bar} and were not reachable from @code{boring}.
768
769@node Symspecs,,Deprecated Options,Invoking
770@section Symspecs
771
772Many of the output options allow functions to be included or excluded
773using @dfn{symspecs} (symbol specifications), which observe the
774following syntax:
775
776@example
777 filename_containing_a_dot
778| funcname_not_containing_a_dot
779| linenumber
780| ( [ any_filename ] `:' ( any_funcname | linenumber ) )
781@end example
782
783Here are some sample symspecs:
784
785@table @samp
786@item main.c
787Selects everything in file @file{main.c}---the
788dot in the string tells @code{gprof} to interpret
789the string as a filename, rather than as
790a function name. To select a file whose
791name does not contain a dot, a trailing colon
792should be specified. For example, @samp{odd:} is
793interpreted as the file named @file{odd}.
794
795@item main
796Selects all functions named @samp{main}.
797
798Note that there may be multiple instances of the same function name
799because some of the definitions may be local (i.e., static). Unless a
800function name is unique in a program, you must use the colon notation
801explained below to specify a function from a specific source file.
802
803Sometimes, function names contain dots. In such cases, it is necessary
804to add a leading colon to the name. For example, @samp{:.mul} selects
805function @samp{.mul}.
806
807In some object file formats, symbols have a leading underscore.
808@code{gprof} will normally not print these underscores. When you name a
809symbol in a symspec, you should type it exactly as @code{gprof} prints
810it in its output. For example, if the compiler produces a symbol
811@samp{_main} from your @code{main} function, @code{gprof} still prints
812it as @samp{main} in its output, so you should use @samp{main} in
813symspecs.
814
815@item main.c:main
816Selects function @samp{main} in file @file{main.c}.
817
818@item main.c:134
819Selects line 134 in file @file{main.c}.
820@end table
821
822@node Output
823@chapter Interpreting @code{gprof}'s Output
824
825@code{gprof} can produce several different output styles, the
826most important of which are described below. The simplest output
827styles (file information, execution count, and function and file ordering)
828are not described here, but are documented with the respective options
829that trigger them.
830@xref{Output Options}.
831
832@menu
833* Flat Profile:: The flat profile shows how much time was spent
834 executing directly in each function.
835* Call Graph:: The call graph shows which functions called which
836 others, and how much time each function used
837 when its subroutine calls are included.
838* Line-by-line:: @code{gprof} can analyze individual source code lines
839* Annotated Source:: The annotated source listing displays source code
840 labeled with execution counts
841@end menu
842
843
844@node Flat Profile,Call Graph,,Output
845@section The Flat Profile
846@cindex flat profile
847
848The @dfn{flat profile} shows the total amount of time your program
849spent executing each function. Unless the @samp{-z} option is given,
850functions with no apparent time spent in them, and no apparent calls
851to them, are not mentioned. Note that if a function was not compiled
852for profiling, and didn't run long enough to show up on the program
853counter histogram, it will be indistinguishable from a function that
854was never called.
855
856This is part of a flat profile for a small program:
857
858@smallexample
859@group
860Flat profile:
861
862Each sample counts as 0.01 seconds.
863 % cumulative self self total
864 time seconds seconds calls ms/call ms/call name
865 33.34 0.02 0.02 7208 0.00 0.00 open
866 16.67 0.03 0.01 244 0.04 0.12 offtime
867 16.67 0.04 0.01 8 1.25 1.25 memccpy
868 16.67 0.05 0.01 7 1.43 1.43 write
869 16.67 0.06 0.01 mcount
870 0.00 0.06 0.00 236 0.00 0.00 tzset
871 0.00 0.06 0.00 192 0.00 0.00 tolower
872 0.00 0.06 0.00 47 0.00 0.00 strlen
873 0.00 0.06 0.00 45 0.00 0.00 strchr
874 0.00 0.06 0.00 1 0.00 50.00 main
875 0.00 0.06 0.00 1 0.00 0.00 memcpy
876 0.00 0.06 0.00 1 0.00 10.11 print
877 0.00 0.06 0.00 1 0.00 0.00 profil
878 0.00 0.06 0.00 1 0.00 50.00 report
879@dots{}
880@end group
881@end smallexample
882
883@noindent
884The functions are sorted by first by decreasing run-time spent in them,
885then by decreasing number of calls, then alphabetically by name. The
886functions @samp{mcount} and @samp{profil} are part of the profiling
887apparatus and appear in every flat profile; their time gives a measure of
888the amount of overhead due to profiling.
889
890Just before the column headers, a statement appears indicating
891how much time each sample counted as.
892This @dfn{sampling period} estimates the margin of error in each of the time
893figures. A time figure that is not much larger than this is not
894reliable. In this example, each sample counted as 0.01 seconds,
895suggesting a 100 Hz sampling rate.
896The program's total execution time was 0.06
897seconds, as indicated by the @samp{cumulative seconds} field. Since
898each sample counted for 0.01 seconds, this means only six samples
899were taken during the run. Two of the samples occurred while the
900program was in the @samp{open} function, as indicated by the
901@samp{self seconds} field. Each of the other four samples
902occurred one each in @samp{offtime}, @samp{memccpy}, @samp{write},
903and @samp{mcount}.
904Since only six samples were taken, none of these values can
905be regarded as particularly reliable.
906In another run,
907the @samp{self seconds} field for
908@samp{mcount} might well be @samp{0.00} or @samp{0.02}.
909@xref{Sampling Error}, for a complete discussion.
910
911The remaining functions in the listing (those whose
912@samp{self seconds} field is @samp{0.00}) didn't appear
913in the histogram samples at all. However, the call graph
914indicated that they were called, so therefore they are listed,
915sorted in decreasing order by the @samp{calls} field.
916Clearly some time was spent executing these functions,
917but the paucity of histogram samples prevents any
918determination of how much time each took.
919
920Here is what the fields in each line mean:
921
922@table @code
923@item % time
924This is the percentage of the total execution time your program spent
925in this function. These should all add up to 100%.
926
927@item cumulative seconds
928This is the cumulative total number of seconds the computer spent
929executing this functions, plus the time spent in all the functions
930above this one in this table.
931
932@item self seconds
933This is the number of seconds accounted for by this function alone.
934The flat profile listing is sorted first by this number.
935
936@item calls
937This is the total number of times the function was called. If the
938function was never called, or the number of times it was called cannot
939be determined (probably because the function was not compiled with
940profiling enabled), the @dfn{calls} field is blank.
941
942@item self ms/call
943This represents the average number of milliseconds spent in this
944function per call, if this function is profiled. Otherwise, this field
945is blank for this function.
946
947@item total ms/call
948This represents the average number of milliseconds spent in this
949function and its descendants per call, if this function is profiled.
950Otherwise, this field is blank for this function.
951This is the only field in the flat profile that uses call graph analysis.
952
953@item name
954This is the name of the function. The flat profile is sorted by this
955field alphabetically after the @dfn{self seconds} and @dfn{calls}
956fields are sorted.
957@end table
958
959@node Call Graph,Line-by-line,Flat Profile,Output
960@section The Call Graph
961@cindex call graph
962
963The @dfn{call graph} shows how much time was spent in each function
964and its children. From this information, you can find functions that,
965while they themselves may not have used much time, called other
966functions that did use unusual amounts of time.
967
968Here is a sample call from a small program. This call came from the
969same @code{gprof} run as the flat profile example in the previous
970chapter.
971
972@smallexample
973@group
974granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds
975
976index % time self children called name
977 <spontaneous>
978[1] 100.0 0.00 0.05 start [1]
979 0.00 0.05 1/1 main [2]
980 0.00 0.00 1/2 on_exit [28]
981 0.00 0.00 1/1 exit [59]
982-----------------------------------------------
983 0.00 0.05 1/1 start [1]
984[2] 100.0 0.00 0.05 1 main [2]
985 0.00 0.05 1/1 report [3]
986-----------------------------------------------
987 0.00 0.05 1/1 main [2]
988[3] 100.0 0.00 0.05 1 report [3]
989 0.00 0.03 8/8 timelocal [6]
990 0.00 0.01 1/1 print [9]
991 0.00 0.01 9/9 fgets [12]
992 0.00 0.00 12/34 strncmp <cycle 1> [40]
993 0.00 0.00 8/8 lookup [20]
994 0.00 0.00 1/1 fopen [21]
995 0.00 0.00 8/8 chewtime [24]
996 0.00 0.00 8/16 skipspace [44]
997-----------------------------------------------
998[4] 59.8 0.01 0.02 8+472 <cycle 2 as a whole> [4]
999 0.01 0.02 244+260 offtime <cycle 2> [7]
1000 0.00 0.00 236+1 tzset <cycle 2> [26]
1001-----------------------------------------------
1002@end group
1003@end smallexample
1004