1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 1988-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Sample Session</title>
18
19<meta name="description" content="Debugging with GDB: Sample Session">
20<meta name="keywords" content="Debugging with GDB: Sample Session">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="index.html#Top" rel="up" title="Top">
29<link href="Invocation.html#Invocation" rel="next" title="Invocation">
30<link href="Contributors.html#Contributors" rel="previous" title="Contributors">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Sample-Session"></a>
65<div class="header">
66<p>
67Next: <a href="Invocation.html#Invocation" accesskey="n" rel="next">Invocation</a>, Previous: <a href="Summary.html#Summary" accesskey="p" rel="previous">Summary</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
68</div>
69<hr>
70<a name="A-Sample-GDB-Session"></a>
71<h2 class="chapter">1 A Sample <small>GDB</small> Session</h2>
72
73<p>You can use this manual at your leisure to read all about <small>GDB</small>.
74However, a handful of commands are enough to get started using the
75debugger.  This chapter illustrates those commands.
76</p>
77
78
79<p>One of the preliminary versions of <small>GNU</small> <code>m4</code> (a generic macro
80processor) exhibits the following bug: sometimes, when we change its
81quote strings from the default, the commands used to capture one macro
82definition within another stop working.  In the following short <code>m4</code>
83session, we define a macro <code>foo</code> which expands to <code>0000</code>; we
84then use the <code>m4</code> built-in <code>defn</code> to define <code>bar</code> as the
85same thing.  However, when we change the open quote string to
86<code>&lt;QUOTE&gt;</code> and the close quote string to <code>&lt;UNQUOTE&gt;</code>, the same
87procedure fails to define a new synonym <code>baz</code>:
88</p>
89<div class="smallexample">
90<pre class="smallexample">$ <b>cd gnu/m4</b>
91$ <b>./m4</b>
92<b>define(foo,0000)</b>
93
94<b>foo</b>
950000
96<b>define(bar,defn(&lsquo;foo&rsquo;))</b>
97
98<b>bar</b>
990000
100<b>changequote(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
101
102<b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
103<b>baz</b>
104<b>Ctrl-d</b>
105m4: End of input: 0: fatal error: EOF in string
106</pre></div>
107
108<p>Let us use <small>GDB</small> to try to see what is going on.
109</p>
110<div class="smallexample">
111<pre class="smallexample">$ <b>gdb m4</b>
112GDB is free software and you are welcome to distribute copies
113 of it under certain conditions; type &quot;show copying&quot; to see
114 the conditions.
115There is absolutely no warranty for GDB; type &quot;show warranty&quot;
116 for details.
117
118GDB 10.2.90.20210621-git, Copyright 1999 Free Software Foundation, Inc...
119(gdb)
120</pre></div>
121
122<p><small>GDB</small> reads only enough symbol data to know where to find the
123rest when needed; as a result, the first prompt comes up very quickly.
124We now tell <small>GDB</small> to use a narrower display width than usual, so
125that examples fit in this manual.
126</p>
127<div class="smallexample">
128<pre class="smallexample">(gdb) <b>set width 70</b>
129</pre></div>
130
131<p>We need to see how the <code>m4</code> built-in <code>changequote</code> works.
132Having looked at the source, we know the relevant subroutine is
133<code>m4_changequote</code>, so we set a breakpoint there with the <small>GDB</small>
134<code>break</code> command.
135</p>
136<div class="smallexample">
137<pre class="smallexample">(gdb) <b>break m4_changequote</b>
138Breakpoint 1 at 0x62f4: file builtin.c, line 879.
139</pre></div>
140
141<p>Using the <code>run</code> command, we start <code>m4</code> running under <small>GDB</small>
142control; as long as control does not reach the <code>m4_changequote</code>
143subroutine, the program runs as usual:
144</p>
145<div class="smallexample">
146<pre class="smallexample">(gdb) <b>run</b>
147Starting program: /work/Editorial/gdb/gnu/m4/m4
148<b>define(foo,0000)</b>
149
150<b>foo</b>
1510000
152</pre></div>
153
154<p>To trigger the breakpoint, we call <code>changequote</code>.  <small>GDB</small>
155suspends execution of <code>m4</code>, displaying information about the
156context where it stops.
157</p>
158<div class="smallexample">
159<pre class="smallexample"><b>changequote(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
160
161Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
162    at builtin.c:879
163879         if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))
164</pre></div>
165
166<p>Now we use the command <code>n</code> (<code>next</code>) to advance execution to
167the next line of the current function.
168</p>
169<div class="smallexample">
170<pre class="smallexample">(gdb) <b>n</b>
171882         set_quotes((argc &gt;= 2) ? TOKEN_DATA_TEXT(argv[1])\
172 : nil,
173</pre></div>
174
175<p><code>set_quotes</code> looks like a promising subroutine.  We can go into it
176by using the command <code>s</code> (<code>step</code>) instead of <code>next</code>.
177<code>step</code> goes to the next line to be executed in <em>any</em>
178subroutine, so it steps into <code>set_quotes</code>.
179</p>
180<div class="smallexample">
181<pre class="smallexample">(gdb) <b>s</b>
182set_quotes (lq=0x34c78 &quot;&lt;QUOTE&gt;&quot;, rq=0x34c88 &quot;&lt;UNQUOTE&gt;&quot;)
183    at input.c:530
184530         if (lquote != def_lquote)
185</pre></div>
186
187<p>The display that shows the subroutine where <code>m4</code> is now
188suspended (and its arguments) is called a stack frame display.  It
189shows a summary of the stack.  We can use the <code>backtrace</code>
190command (which can also be spelled <code>bt</code>), to see where we are
191in the stack as a whole: the <code>backtrace</code> command displays a
192stack frame for each active subroutine.
193</p>
194<div class="smallexample">
195<pre class="smallexample">(gdb) <b>bt</b>
196#0  set_quotes (lq=0x34c78 &quot;&lt;QUOTE&gt;&quot;, rq=0x34c88 &quot;&lt;UNQUOTE&gt;&quot;)
197    at input.c:530
198#1  0x6344 in m4_changequote (argc=3, argv=0x33c70)
199    at builtin.c:882
200#2  0x8174 in expand_macro (sym=0x33320) at macro.c:242
201#3  0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30)
202    at macro.c:71
203#4  0x79dc in expand_input () at macro.c:40
204#5  0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195
205</pre></div>
206
207<p>We step through a few more lines to see what happens.  The first two
208times, we can use &lsquo;<samp>s</samp>&rsquo;; the next two times we use <code>n</code> to avoid
209falling into the <code>xstrdup</code> subroutine.
210</p>
211<div class="smallexample">
212<pre class="smallexample">(gdb) <b>s</b>
2130x3b5c  532         if (rquote != def_rquote)
214(gdb) <b>s</b>
2150x3b80  535         lquote = (lq == nil || *lq == '\0') ?  \
216def_lquote : xstrdup(lq);
217(gdb) <b>n</b>
218536         rquote = (rq == nil || *rq == '\0') ? def_rquote\
219 : xstrdup(rq);
220(gdb) <b>n</b>
221538         len_lquote = strlen(rquote);
222</pre></div>
223
224<p>The last line displayed looks a little odd; we can examine the variables
225<code>lquote</code> and <code>rquote</code> to see if they are in fact the new left
226and right quotes we specified.  We use the command <code>p</code>
227(<code>print</code>) to see their values.
228</p>
229<div class="smallexample">
230<pre class="smallexample">(gdb) <b>p lquote</b>
231$1 = 0x35d40 &quot;&lt;QUOTE&gt;&quot;
232(gdb) <b>p rquote</b>
233$2 = 0x35d50 &quot;&lt;UNQUOTE&gt;&quot;
234</pre></div>
235
236<p><code>lquote</code> and <code>rquote</code> are indeed the new left and right quotes.
237To look at some context, we can display ten lines of source
238surrounding the current line with the <code>l</code> (<code>list</code>) command.
239</p>
240<div class="smallexample">
241<pre class="smallexample">(gdb) <b>l</b>
242533             xfree(rquote);
243534
244535         lquote = (lq == nil || *lq == '\0') ? def_lquote\
245 : xstrdup (lq);
246536         rquote = (rq == nil || *rq == '\0') ? def_rquote\
247 : xstrdup (rq);
248537
249538         len_lquote = strlen(rquote);
250539         len_rquote = strlen(lquote);
251540     }
252541
253542     void
254</pre></div>
255
256<p>Let us step past the two lines that set <code>len_lquote</code> and
257<code>len_rquote</code>, and then examine the values of those variables.
258</p>
259<div class="smallexample">
260<pre class="smallexample">(gdb) <b>n</b>
261539         len_rquote = strlen(lquote);
262(gdb) <b>n</b>
263540     }
264(gdb) <b>p len_lquote</b>
265$3 = 9
266(gdb) <b>p len_rquote</b>
267$4 = 7
268</pre></div>
269
270<p>That certainly looks wrong, assuming <code>len_lquote</code> and
271<code>len_rquote</code> are meant to be the lengths of <code>lquote</code> and
272<code>rquote</code> respectively.  We can set them to better values using
273the <code>p</code> command, since it can print the value of
274any expression&mdash;and that expression can include subroutine calls and
275assignments.
276</p>
277<div class="smallexample">
278<pre class="smallexample">(gdb) <b>p len_lquote=strlen(lquote)</b>
279$5 = 7
280(gdb) <b>p len_rquote=strlen(rquote)</b>
281$6 = 9
282</pre></div>
283
284<p>Is that enough to fix the problem of using the new quotes with the
285<code>m4</code> built-in <code>defn</code>?  We can allow <code>m4</code> to continue
286executing with the <code>c</code> (<code>continue</code>) command, and then try the
287example that caused trouble initially:
288</p>
289<div class="smallexample">
290<pre class="smallexample">(gdb) <b>c</b>
291Continuing.
292
293<b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
294
295baz
2960000
297</pre></div>
298
299<p>Success!  The new quotes now work just as well as the default ones.  The
300problem seems to have been just the two typos defining the wrong
301lengths.  We allow <code>m4</code> exit by giving it an EOF as input:
302</p>
303<div class="smallexample">
304<pre class="smallexample"><b>Ctrl-d</b>
305Program exited normally.
306</pre></div>
307
308<p>The message &lsquo;<samp>Program exited normally.</samp>&rsquo; is from <small>GDB</small>; it
309indicates <code>m4</code> has finished executing.  We can end our <small>GDB</small>
310session with the <small>GDB</small> <code>quit</code> command.
311</p>
312<div class="smallexample">
313<pre class="smallexample">(gdb) <b>quit</b>
314</pre></div>
315
316<hr>
317<div class="header">
318<p>
319Next: <a href="Invocation.html#Invocation" accesskey="n" rel="next">Invocation</a>, Previous: <a href="Summary.html#Summary" accesskey="p" rel="previous">Summary</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
320</div>
321
322
323
324</body>
325</html>
326