Once the program is compiled for profiling, you must run it in order to
generate the information that gprof
needs. Simply run the program
as usual, using the normal arguments, file names, etc. The program should
run normally, producing the same output as usual. It will, however, run
somewhat slower than normal because of the time spent collecting and
writing the profile data.
The way you run the program—the arguments and input that you give it—may have a dramatic effect on what the profile information shows. The profile data will describe the parts of the program that were activated for the particular input you use. For example, if the first command you give to your program is to quit, the profile data will show the time used in initialization and in cleanup, but not much else.
Your program will write the profile data into a file called gmon.out just before exiting. If there is already a file called gmon.out, its contents are overwritten. You can rename the file afterwards if you are concerned that it may be overwritten. If your system libc allows you may be able to write the profile data under a different name. Set the GMON_OUT_PREFIX environment variable; this name will be appended with the PID of the running program.
In order to write the gmon.out file properly, your program must exit
normally: by returning from main
or by calling exit
. Calling
the low-level function _exit
does not write the profile data, and
neither does abnormal termination due to an unhandled signal.
The gmon.out file is written in the program’s current working
directory at the time it exits. This means that if your program calls
chdir
, the gmon.out file will be left in the last directory
your program chdir
’d to. If you don’t have permission to write in
this directory, the file is not written, and you will get an error message.
Older versions of the GNU profiling library may also write a file
called bb.out. This file, if present, contains an human-readable
listing of the basic-block execution counts. Unfortunately, the
appearance of a human-readable bb.out means the basic-block
counts didn’t get written into gmon.out.
The Perl script bbconv.pl
, included with the gprof
source distribution, will convert a bb.out file into
a format readable by gprof
. Invoke it like this:
bbconv.pl < bb.out > bh-data
This translates the information in bb.out into a form that
gprof
can understand. But you still need to tell gprof
about the existence of this translated information. To do that, include
bb-data on the gprof
command line, along with
gmon.out, like this:
gprof options executable-file gmon.out bb-data [yet-more-profile-data-files…] [> outfile]