Work around MinGW mangling of "host:/path"
[msysgit/historical-msysgit.git] / mingw / info / gdb / Sample-Session.html
blob125374871837175f4752c0d832a3556c1977b55d
1 <html lang="en">
2 <head>
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">
8 </head>
9 <body>
10 <div class="node">
11 <p>
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>
16 <hr><br>
17 </div>
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>&lt;QUOTE&gt;</code> and the close quote string to <code>&lt;UNQUOTE&gt;</code>, the same
33 procedure fails to define a new synonym <code>baz</code>:
35 <pre class="smallexample"> $ <b>cd gnu/m4</b>
36 $ <b>./m4</b>
37 <b>define(foo,0000)</b>
39 <b>foo</b>
40 0000
41 <b>define(bar,defn(`foo'))</b>
43 <b>bar</b>
44 0000
45 <b>changequote(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
47 <b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
48 <b>baz</b>
49 <b>C-d</b>
50 m4: End of input: 0: fatal error: EOF in string
51 </pre>
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
58 the conditions.
59 There is absolutely no warranty for GDB; type "show warranty"
60 for details.
62 GDB 5.2.1, Copyright 1999 Free Software Foundation, Inc...
63 (gdb)
64 </pre>
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>
72 </pre>
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.
81 </pre>
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>
91 <b>foo</b>
92 0000
93 </pre>
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(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
101 Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
102 at builtin.c:879
103 879 if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))
104 </pre>
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 &gt;= 2) ? TOKEN_DATA_TEXT(argv[1])\
111 : nil,
112 </pre>
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 "&lt;QUOTE&gt;", rq=0x34c88 "&lt;UNQUOTE&gt;")
121 at input.c:530
122 530 if (lquote != def_lquote)
123 </pre>
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 "&lt;QUOTE&gt;", rq=0x34c88 "&lt;UNQUOTE&gt;")
134 at input.c:530
135 #1 0x6344 in m4_changequote (argc=3, argv=0x33c70)
136 at builtin.c:882
137 #2 0x8174 in expand_macro (sym=0x33320) at macro.c:242
138 #3 0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30)
139 at macro.c:71
140 #4 0x79dc in expand_input () at macro.c:40
141 #5 0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195
142 </pre>
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)
150 (gdb) <b>s</b>
151 0x3b80 535 lquote = (lq == nil || *lq == '\0') ? \
152 def_lquote : xstrdup(lq);
153 (gdb) <b>n</b>
154 536 rquote = (rq == nil || *rq == '\0') ? def_rquote\
155 : xstrdup(rq);
156 (gdb) <b>n</b>
157 538 len_lquote = strlen(rquote);
158 </pre>
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 "&lt;QUOTE&gt;"
167 (gdb) <b>p rquote</b>
168 $2 = 0x35d50 "&lt;UNQUOTE&gt;"
169 </pre>
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>
176 533 xfree(rquote);
178 535 lquote = (lq == nil || *lq == '\0') ? def_lquote\
179 : xstrdup (lq);
180 536 rquote = (rq == nil || *rq == '\0') ? def_rquote\
181 : xstrdup (rq);
183 538 len_lquote = strlen(rquote);
184 539 len_rquote = strlen(lquote);
185 540 }
187 542 void
188 </pre>
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);
195 (gdb) <b>n</b>
196 540 }
197 (gdb) <b>p len_lquote</b>
198 $3 = 9
199 (gdb) <b>p len_rquote</b>
200 $4 = 7
201 </pre>
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
208 assignments.
210 <pre class="smallexample"> (gdb) <b>p len_lquote=strlen(lquote)</b>
211 $5 = 7
212 (gdb) <b>p len_rquote=strlen(rquote)</b>
213 $6 = 9
214 </pre>
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>
222 Continuing.
224 <b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
227 0000
228 </pre>
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.
236 </pre>
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>
243 </pre>
245 </body></html>