3 <title>Debugging with GDB
</title>
4 <meta http-equiv=
"Content-Type" content=
"text/html">
5 <meta name=
"description" content=
"Debugging with GDB">
6 <meta name=
"generator" content=
"makeinfo 4.3">
7 <link href=
"http://www.gnu.org/software/texinfo/" rel=
"generator-home">
12 Node:
<a name=
"Sample%20Session">Sample Session
</a>,
13 Next:
<a rel=
"next" accesskey=
"n" href=
"Invocation.html#Invocation">Invocation
</a>,
14 Previous:
<a rel=
"previous" accesskey=
"p" href=
"Summary.html#Summary">Summary
</a>,
15 Up:
<a rel=
"up" accesskey=
"u" href=
"index.html#Top">Top
</a>
19 <h2 class=
"chapter">A Sample GDB Session
</h2>
21 <p>You can use this manual at your leisure to read all about GDB.
22 However, a handful of commands are enough to get started using the
23 debugger. This chapter illustrates those commands.
25 <p>One of the preliminary versions of
<small>GNU
</small> <code>m4
</code> (a generic macro
26 processor) exhibits the following bug: sometimes, when we change its
27 quote strings from the default, the commands used to capture one macro
28 definition within another stop working. In the following short
<code>m4
</code>
29 session, we define a macro
<code>foo
</code> which expands to
<code>0000</code>; we
30 then use the
<code>m4
</code> built-in
<code>defn
</code> to define
<code>bar
</code> as the
31 same thing. However, when we change the open quote string to
32 <code><QUOTE
></code> and the close quote string to
<code><UNQUOTE
></code>, the same
33 procedure fails to define a new synonym
<code>baz
</code>:
35 <pre class=
"smallexample"> $
<b>cd gnu/m4
</b>
37 <b>define(foo,
0000)
</b>
41 <b>define(bar,defn(`foo'))
</b>
45 <b>changequote(
<QUOTE
>,
<UNQUOTE
>)
</b>
47 <b>define(baz,defn(
<QUOTE
>foo
<UNQUOTE
>))
</b>
50 m4: End of input:
0: fatal error: EOF in string
53 <p>Let us use GDB to try to see what is going on.
55 <pre class=
"smallexample"> $
<b>gdb m4
</b>
56 GDB is free software and you are welcome to distribute copies
57 of it under certain conditions; type
"show copying" to see
59 There is absolutely no warranty for GDB; type
"show warranty"
62 GDB
5.2.1, Copyright
1999 Free Software Foundation, Inc...
66 GDB reads only enough symbol data to know where to find the
67 rest when needed; as a result, the first prompt comes up very quickly.
68 We now tell GDB to use a narrower display width than usual, so
69 that examples fit in this manual.
71 <pre class=
"smallexample"> (gdb)
<b>set width
70</b>
74 <p>We need to see how the
<code>m4
</code> built-in
<code>changequote
</code> works.
75 Having looked at the source, we know the relevant subroutine is
76 <code>m4_changequote
</code>, so we set a breakpoint there with the GDB
77 <code>break
</code> command.
79 <pre class=
"smallexample"> (gdb)
<b>break m4_changequote
</b>
80 Breakpoint
1 at
0x62f4: file builtin.c, line
879.
83 <p>Using the
<code>run
</code> command, we start
<code>m4
</code> running under GDB
84 control; as long as control does not reach the
<code>m4_changequote
</code>
85 subroutine, the program runs as usual:
87 <pre class=
"smallexample"> (gdb)
<b>run
</b>
88 Starting program: /work/Editorial/gdb/gnu/m4/m4
89 <b>define(foo,
0000)
</b>
95 <p>To trigger the breakpoint, we call
<code>changequote
</code>. GDB
96 suspends execution of
<code>m4
</code>, displaying information about the
97 context where it stops.
99 <pre class=
"smallexample"> <b>changequote(
<QUOTE
>,
<UNQUOTE
>)
</b>
101 Breakpoint
1, m4_changequote (argc=
3, argv=
0x33c70)
103 879 if (bad_argc(TOKEN_DATA_TEXT(argv[
0]),argc,
1,
3))
106 <p>Now we use the command
<code>n
</code> (
<code>next
</code>) to advance execution to
107 the next line of the current function.
109 <pre class=
"smallexample"> (gdb)
<b>n
</b>
110 882 set_quotes((argc
>=
2) ? TOKEN_DATA_TEXT(argv[
1])\
114 <p><code>set_quotes
</code> looks like a promising subroutine. We can go into it
115 by using the command
<code>s
</code> (
<code>step
</code>) instead of
<code>next
</code>.
116 <code>step
</code> goes to the next line to be executed in
<em>any
</em>
117 subroutine, so it steps into
<code>set_quotes
</code>.
119 <pre class=
"smallexample"> (gdb)
<b>s
</b>
120 set_quotes (lq=
0x34c78 "<QUOTE>", rq=
0x34c88 "<UNQUOTE>")
122 530 if (lquote != def_lquote)
125 <p>The display that shows the subroutine where
<code>m4
</code> is now
126 suspended (and its arguments) is called a stack frame display. It
127 shows a summary of the stack. We can use the
<code>backtrace
</code>
128 command (which can also be spelled
<code>bt
</code>), to see where we are
129 in the stack as a whole: the
<code>backtrace
</code> command displays a
130 stack frame for each active subroutine.
132 <pre class=
"smallexample"> (gdb)
<b>bt
</b>
133 #
0 set_quotes (lq=
0x34c78 "<QUOTE>", rq=
0x34c88 "<UNQUOTE>")
135 #
1 0x6344 in m4_changequote (argc=
3, argv=
0x33c70)
137 #
2 0x8174 in expand_macro (sym=
0x33320) at macro.c:
242
138 #
3 0x7a88 in expand_token (obs=
0x0, t=
209696, td=
0xf7fffa30)
140 #
4 0x79dc in expand_input () at macro.c:
40
141 #
5 0x2930 in main (argc=
0, argv=
0xf7fffb20) at m4.c:
195
144 <p>We step through a few more lines to see what happens. The first two
145 times, we can use
<code>s
</code>; the next two times we use
<code>n
</code> to avoid
146 falling into the
<code>xstrdup
</code> subroutine.
148 <pre class=
"smallexample"> (gdb)
<b>s
</b>
149 0x3b5c 532 if (rquote != def_rquote)
151 0x3b80 535 lquote = (lq == nil || *lq == '\
0') ? \
152 def_lquote : xstrdup(lq);
154 536 rquote = (rq == nil || *rq == '\
0') ? def_rquote\
157 538 len_lquote = strlen(rquote);
160 <p>The last line displayed looks a little odd; we can examine the variables
161 <code>lquote
</code> and
<code>rquote
</code> to see if they are in fact the new left
162 and right quotes we specified. We use the command
<code>p
</code>
163 (
<code>print
</code>) to see their values.
165 <pre class=
"smallexample"> (gdb)
<b>p lquote
</b>
166 $
1 =
0x35d40 "<QUOTE>"
167 (gdb)
<b>p rquote
</b>
168 $
2 =
0x35d50 "<UNQUOTE>"
171 <p><code>lquote
</code> and
<code>rquote
</code> are indeed the new left and right quotes.
172 To look at some context, we can display ten lines of source
173 surrounding the current line with the
<code>l
</code> (
<code>list
</code>) command.
175 <pre class=
"smallexample"> (gdb)
<b>l
</b>
178 535 lquote = (lq == nil || *lq == '\
0') ? def_lquote\
180 536 rquote = (rq == nil || *rq == '\
0') ? def_rquote\
183 538 len_lquote = strlen(rquote);
184 539 len_rquote = strlen(lquote);
190 <p>Let us step past the two lines that set
<code>len_lquote
</code> and
191 <code>len_rquote
</code>, and then examine the values of those variables.
193 <pre class=
"smallexample"> (gdb)
<b>n
</b>
194 539 len_rquote = strlen(lquote);
197 (gdb)
<b>p len_lquote
</b>
199 (gdb)
<b>p len_rquote
</b>
203 <p>That certainly looks wrong, assuming
<code>len_lquote
</code> and
204 <code>len_rquote
</code> are meant to be the lengths of
<code>lquote
</code> and
205 <code>rquote
</code> respectively. We can set them to better values using
206 the
<code>p
</code> command, since it can print the value of
207 any expression--and that expression can include subroutine calls and
210 <pre class=
"smallexample"> (gdb)
<b>p len_lquote=strlen(lquote)
</b>
212 (gdb)
<b>p len_rquote=strlen(rquote)
</b>
216 <p>Is that enough to fix the problem of using the new quotes with the
217 <code>m4
</code> built-in
<code>defn
</code>? We can allow
<code>m4
</code> to continue
218 executing with the
<code>c
</code> (
<code>continue
</code>) command, and then try the
219 example that caused trouble initially:
221 <pre class=
"smallexample"> (gdb)
<b>c
</b>
224 <b>define(baz,defn(
<QUOTE
>foo
<UNQUOTE
>))
</b>
230 <p>Success! The new quotes now work just as well as the default ones. The
231 problem seems to have been just the two typos defining the wrong
232 lengths. We allow
<code>m4
</code> exit by giving it an EOF as input:
234 <pre class=
"smallexample"> <b>C-d
</b>
235 Program exited normally.
238 <p>The message
<code>Program exited normally.
</code> is from GDB; it
239 indicates
<code>m4
</code> has finished executing. We can end our GDB
240 session with the GDB
<code>quit
</code> command.
242 <pre class=
"smallexample"> (gdb)
<b>quit
</b>