Debugging with GDB Manual The GNU Source-Level Debugger (769148-001, March 2014)

1 A Sample GDB Session
This chapter describes the most common GDB commands with the help of an example.
The following topics are discussed:
Loading the Executable
Setting the Display Width
Setting Breakpoints
Running the Executable under GDB
Stepping to the next line
Stepping into a Subroutine
Examining the Stack
Printing Variable Values
Listing the Source Code
Setting Variable Values During a Debug Session
In this sample session, we emphasize user input like this: input, to make it easier to pick out from
the surrounding output.
One of the preliminary versions of GNU m4 (a generic macro processor) exhibits the following bug:
sometimes, when we change its quote strings from the default, the commands used to capture one
macro definition within another stop working. In the following short m4 session, we define a macro
foo which expands to 0000; we then use the m4 built-in defn to define bar as the same
thing. However, when we change the open quote string to <QUOTE> and the close quote string
to <UNQUOTE>, the same procedure fails to define a new synonym baz:
$ cd gnu/m4 //change your current directory to the location where the m4 executable is stored.
$ ./m4 //run the m4 application
define(foo,0000)
foo
0000
define (bar,defn('foo'))
bar
0000
changequote(<QUOTE>,<UNQUOTE>)
define(baz,defn(<QUOTE>foo<UNQUOTE>))
baz
C-d
m4: End of input: 0: fatal error: EOF in string
Loading the Executable
Let us use GDB to try to see what is going on.
$ (gdb) m4
HP gdb 3.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 3.0 (based on GDB ) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
GDB reads only enough symbol data to know where to find the rest when needed; as a result, the
first prompt comes up very quickly.
Loading the Executable 15