Running Linux, 5th Edition - Matthias Kalle Dalheimer [401]
Debuggers
Several interactive debuggers are available for Linux. The de facto standard debugger is gdb, which we just covered in detail.
In addition to gdb, there are several other debuggers, each with features very similar to gdb. DDD (Data Display Debugger) is a version of gdb with an X Window System interface similar to that found on the xdbx debugger on other Unix systems. There are several panes in the DDD debugger's window. One pane looks like the regular gdb text interface, allowing you to input commands manually to interact with the system. Another pane automatically displays the current source file along with a marker displaying the current line. You can use the source pane to set and select breakpoints, browse the source, and so on, while typing commands directly to gdb. The DDD window also contains several buttons that provide quick access to frequently used commands, such as step, next, and so on. Given the buttons, you can use the mouse in conjunction with the keyboard to debug your program within an easy-to-use X interface. Finally, DDD has a very useful mode that lets you explore data structures of an unknown program.
KDevelop, the IDE, comes with its own, very convenient gdb frontend; it is also fully integrated into the KDE Desktop. We cover KDevelop at the end of this chapter.
Profiling and Performance Tools
Several utilities exist that allow you to monitor and rate the performance of your program. These tools help you locate bottlenecks in your code—places where performance is lacking. These tools also give you a rundown on the call structure of your program, indicating what functions are called, from where, and how often (in other words, everything you ever wanted to know about your program, but were afraid to ask).
gprof is a profiling utility that gives you a detailed listing of the running statistics for your program, including how often each function was called, from where, the total amount of time that each function required, and so forth.
To use gprof with a program, you must compile the program using the -pg option with gcc. This adds profiling information to the object file and links the executable with standard libraries that have profiling information enabled.
Having compiled the program to profile with -pg, simply run it. If it exits normally, the file gmon.out will be written to the working directory of the program. This file contains profiling information for that run and can be used with gprof to display a table of statistics.
As an example, let's take a program called getstat, which gathers statistics about an image file. After compiling getstat with -pg, we run it:
papaya$ getstat image11.pgm > stats.dat
papaya$ ls -l gmon.out
-rw------- 1 mdw mdw 54448 Feb 5 17:00 gmon.out
papaya$
Indeed, the profiling information was written to gmon.out.
To examine the profiling data, we run gprof and give it the name of the executable and the profiling file gmon.out:
papaya$ gprof getstat gmon.out
If you do not specify the name of the profiling file, gprof assumes the name gmon.out. It also assumes the executable name a.out if you do not specify that, either.
gprof output is rather verbose, so you may want to redirect it to a file or pipe it through a pager. It comes in two parts. The first part is the "flat profile," which gives a one-line entry for each function, listing the percentage of time spent in that function, the time (in seconds) used to execute that function, the number of calls to the function, and other information. For example:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
45.11 27.49 27.49 41 670.51 903.13 GetComponent
16.25 37.40 9.91 mcount
10.72 43.93 6.54 1811863 0.00 0.00 Push
10.33 50.23 6.30 1811863 0.00 0.00 Pop
5.87 53.81 3.58 40 89.50 247.06 stackstats
4.92 56.81 3.00 1811863 0.00 0.00 TrimNeighbors
If any of the fields are blank in the output, gprof was unable to determine any further information about that function.