gprof: Compiling
1
1 2 Compiling a Program for Profiling
1 ***********************************
1
1 The first step in generating profile information for your program is to
1 compile and link it with profiling enabled.
1
1 To compile a source file for profiling, specify the '-pg' option when
1 you run the compiler. (This is in addition to the options you normally
1 use.)
1
1 To link the program for profiling, if you use a compiler such as 'cc'
1 to do the linking, simply specify '-pg' in addition to your usual
1 options. The same option, '-pg', alters either compilation or linking
1 to do what is necessary for profiling. Here are examples:
1
1 cc -g -c myprog.c utils.c -pg
1 cc -o myprog myprog.o utils.o -pg
1
1 The '-pg' option also works with a command that both compiles and
1 links:
1
1 cc -o myprog myprog.c utils.c -g -pg
1
1 Note: The '-pg' option must be part of your compilation options as
1 well as your link options. If it is not then no call-graph data will be
1 gathered and when you run 'gprof' you will get an error message like
1 this:
1
1 gprof: gmon.out file is missing call-graph data
1
1 If you add the '-Q' switch to suppress the printing of the call graph
1 data you will still be able to see the time samples:
1
1 Flat profile:
1
1 Each sample counts as 0.01 seconds.
1 % cumulative self self total
1 time seconds seconds calls Ts/call Ts/call name
1 44.12 0.07 0.07 zazLoop
1 35.29 0.14 0.06 main
1 20.59 0.17 0.04 bazMillion
1
1 If you run the linker 'ld' directly instead of through a compiler
1 such as 'cc', you may have to specify a profiling startup file 'gcrt0.o'
1 as the first input file instead of the usual startup file 'crt0.o'. In
1 addition, you would probably want to specify the profiling C library,
1 'libc_p.a', by writing '-lc_p' instead of the usual '-lc'. This is not
1 absolutely necessary, but doing this gives you number-of-calls
1 information for standard library functions such as 'read' and 'open'.
1 For example:
1
1 ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p
1
1 If you are running the program on a system which supports shared
1 libraries you may run into problems with the profiling support code in a
1 shared library being called before that library has been fully
1 initialised. This is usually detected by the program encountering a
1 segmentation fault as soon as it is run. The solution is to link
1 against a static version of the library containing the profiling support
1 code, which for 'gcc' users can be done via the '-static' or
1 '-static-libgcc' command line option. For example:
1
1 gcc -g -pg -static-libgcc myprog.c utils.c -o myprog
1
1 If you compile only some of the modules of the program with '-pg',
1 you can still profile the program, but you won't get complete
1 information about the modules that were compiled without '-pg'. The
1 only information you get for the functions in those modules is the total
1 time spent in them; there is no record of how many times they were
1 called, or from where. This will not affect the flat profile (except
1 that the 'calls' field for the functions will be blank), but will
1 greatly reduce the usefulness of the call graph.
1
1 If you wish to perform line-by-line profiling you should use the
1 'gcov' tool instead of 'gprof'. See that tool's manual or info pages
1 for more details of how to do this.
1
1 Note, older versions of 'gcc' produce line-by-line profiling
1 information that works with 'gprof' rather than 'gcov' so there is still
11 support for displaying this kind of information in 'gprof'. ⇒
Line-by-line Profiling Line-by-line.
1
1 It also worth noting that 'gcc' implements a '-finstrument-functions'
1 command line option which will insert calls to special user supplied
1 instrumentation routines at the entry and exit of every function in
1 their program. This can be used to implement an alternative profiling
1 scheme.
1