Section 4.3. The GNU Debugger


4.3. The GNU Debugger

Gdb is the Free Software Foundation's debugger. It is a good command-line debugger, on which several tools have been built, including Emacs' gdb mode, the graphical Data Display Debugger (DDD),[5] and built-in de-buggers in several graphical IDEs. We cover only gdb in this section.


Start gdb by running gdb progname. Gdb will not search the PATH looking for the executable file. Gdb will load the executable's symbols and then prompt you for what to do next.

There are three ways to inspect a process with gdb:

  • Use the run command to start the program normally.

  • Use the attach command to start inspecting an already-running process. When you attach to a process, the process will be stopped.

  • Inspect an existing core file to determine the state of the process when it was killed. To inspect a core file, start gdb with the command gdb progname corefile.

Before you run a program or attach to an already-running program, you can set breakpoints, list source code, and do anything else that does not necessarily involve a running process.

Gdb does not require that you type entire command names; r suffices for run, n for next, s for step. Furthermore, to repeat the most recent command, simply hit Return. This makes single-stepping easy.

A short selection of useful gdb commands is included here; gdb includes a comprehensive online manual in GNU info format (run info gdb) that explains all of gdb's options in detail in a tutorial format. Programming with GNU Software [Loukides, 1997] contains a good detailed tutorial on using gdb. Gdb also includes extensive online help available from within gdb; access it with the help command. Specific help on each command is available with help commandname or help topic.

Just like shell commands, gdb commands may take arguments. We use "call help with an argument of command" to mean the same as "type help command".

Some gdb commands also take format identifiers to identify how to print values. Format identifiers immediately follow the command name and are separated from the command name by a slash. Once you have chosen a format, you do not have to use it each time you repeat the command; gdb will remember the format you chose as the default.

Format identifiers are separated from commands by a / character and are composed of three elements: a count, a format letter, and a size letter. The count and size letters are optional; count defaults to 1, and the size has reasonable defaults based on the format letter.

The format letters are o for octal, x for hexadecimal, d for decimal, u for unsigned decimal, t for binary, f for floating-point, a for address, i for instruction, c for character, and s for string.

The size letters are b for byte, h for half word (2 bytes), w for word (4 bytes), and g for giant (8 bytes).

attach, at

Attach to an already-running process. The only argument is the pid of the process to which to attach. This stops the processes to which you attach, interrupting any sleep or other interruptible system call in progress. See detach.

backtrace, bt, where, w

Print a stack trace.

break, b

Set a breakpoint. You can specify a function name, a line number of the current file (the file containing the currently executing code), a filename: linenumber pair, or even an arbitrary address with *address. Gdb assigns and tells you a unique number for each breakpoint. See condition, clear, and delete.


Clear a breakpoint. Takes the same arguments as break. See delete.


Changes a breakpoint specified by number (see break) to break only if a condition is true. The condition is expressed as an arbitrary expression.


 (gdb) b 664 Breakpoint 3 at 0x804a5c0: file ladsh4.c, line 664. (gdb) condition 3 status == 0 


Clear a breakpoint by number.


Detach from the currently attached process.


Display the value of an expression every time execution stops. Takes the same arguments (including format modifiers) as print. Prints a display number that can be used later to cancel the display. See undisplay.


Get help. Called with no argument, provides a summary of the help available. Called with another command as an argument, provides help on that command. Extensively cross-referenced.


Jump to an arbitrary address and continue execution there. The address is the only argument, and it can be specified either as a line number or as an address specified as *address.

list, l

With no argument, list first lists the 10 lines surrounding the current address. Subsequent calls to list list subsequent sections of 10 lines. With an argument of -, lists the previous 10 lines.


With a line number, lists the 10 lines surrounding that line. With a filename: linenumber pair, lists the 10 lines surrounding that line. With a function name, lists the 10 lines surrounding the beginning of the function. With an address specified as *address, specifies the 10 lines surrounding the code found at that address.


With two line specifications separated by commas, lists all the lines between the two specified lines.

next, n

Step to the next line of source code in the current function; make function calls without stepping. See step.


Step to the next machine language instruction; make function calls without stepping. See stepi.

print, p

Print the value of an expression in a comprehensible representation. If you have a char *c, the command print c will print the address of the string, and print *c will print the string itself. Printing structures will expand the structures. You can include casts in your expressions, and gdb will honor them. If the code was compiled with the -ggdb option, enumerated values and preprocessor definitions will be available for you to use in your expressions. See display.


The print command takes format identifiers, although with proper types and with typecasts, the format identifiers are rarely necessary. See x.

run, r

Run the current program from the beginning. The arguments to the run command are the arguments that would be used to run the program on the command line. Gdb will do shell-style globbing with * and [], and it will do shell-style redirection with <,>, and >>, but it will not do pipes or here documents.


With no arguments, run uses the arguments that were specified in the most recent run command, or in the most recent set args command. To run with no arguments after running with arguments, use the set args command with no extra arguments.


Gdb allows you to change the values of variables, like this:


 (gdb) set a = argv[5] 


Also, whenever you print an expression, gdb gives you a shorthand variable, like $1, that you can use to refer to it later. So if you had previously printed argv[5] and gdb had told you that it was $6, you could write the previous assignment as


 (gdb) set a = $ 6 


The set command also has many subcommands, far too numerous to list here. Use help set for more information.

step, s

Step the program instruction by instruction until it reaches a new line of source code. See next.


Execute exactly one machine language instruction; traces into function calls. See nexti.


Without any argument, cancels all displays. Otherwise, cancels the displays whose numbers are given as arguments. See display.


Prints the data type of an expression given as its argument.

where, w

See backtrace.


The x command is like the print command, except that it is explicitly limited to printing the contents of an address in some arbitrary format. If you do not use a format identifier, gdb will use the most recently specified format identifier.


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: