blob: 735ba0f7dd6df3399c13a2dcf287e3727d375d05 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1988-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software" and "Free Software Needs
Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below.
(a) The FSF's Back-Cover Text is: "You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Debugging with GDB: Sample Session</title>
<meta name="description" content="Debugging with GDB: Sample Session">
<meta name="keywords" content="Debugging with GDB: Sample Session">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Invocation.html#Invocation" rel="next" title="Invocation">
<link href="Contributors.html#Contributors" rel="prev" title="Contributors">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Sample-Session"></a>
<div class="header">
<p>
Next: <a href="Invocation.html#Invocation" accesskey="n" rel="next">Invocation</a>, Previous: <a href="Summary.html#Summary" accesskey="p" rel="prev">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>
</div>
<hr>
<a name="A-Sample-GDB-Session"></a>
<h2 class="chapter">1 A Sample <small>GDB</small> Session</h2>
<p>You can use this manual at your leisure to read all about <small>GDB</small>.
However, a handful of commands are enough to get started using the
debugger. This chapter illustrates those commands.
</p>
<p>One of the preliminary versions of <small>GNU</small> <code>m4</code> (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 <code>m4</code>
session, we define a macro <code>foo</code> which expands to <code>0000</code>; we
then use the <code>m4</code> built-in <code>defn</code> to define <code>bar</code> as the
same thing. However, when we change the open quote string to
<code>&lt;QUOTE&gt;</code> and the close quote string to <code>&lt;UNQUOTE&gt;</code>, the same
procedure fails to define a new synonym <code>baz</code>:
</p>
<div class="smallexample">
<pre class="smallexample">$ <b>cd gnu/m4</b>
$ <b>./m4</b>
<b>define(foo,0000)</b>
<b>foo</b>
0000
<b>define(bar,defn(&lsquo;foo&rsquo;))</b>
<b>bar</b>
0000
<b>changequote(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
<b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
<b>baz</b>
<b>Ctrl-d</b>
m4: End of input: 0: fatal error: EOF in string
</pre></div>
<p>Let us use <small>GDB</small> to try to see what is going on.
</p>
<div class="smallexample">
<pre class="smallexample">$ <b>gdb m4</b>
GDB is free software and you are welcome to distribute copies
of it under certain conditions; type &quot;show copying&quot; to see
the conditions.
There is absolutely no warranty for GDB; type &quot;show warranty&quot;
for details.
GDB 7.10.1.20160210-cvs, Copyright 1999 Free Software Foundation, Inc...
(gdb)
</pre></div>
<p><small>GDB</small> reads only enough symbol data to know where to find the
rest when needed; as a result, the first prompt comes up very quickly.
We now tell <small>GDB</small> to use a narrower display width than usual, so
that examples fit in this manual.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>set width 70</b>
</pre></div>
<p>We need to see how the <code>m4</code> built-in <code>changequote</code> works.
Having looked at the source, we know the relevant subroutine is
<code>m4_changequote</code>, so we set a breakpoint there with the <small>GDB</small>
<code>break</code> command.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>break m4_changequote</b>
Breakpoint 1 at 0x62f4: file builtin.c, line 879.
</pre></div>
<p>Using the <code>run</code> command, we start <code>m4</code> running under <small>GDB</small>
control; as long as control does not reach the <code>m4_changequote</code>
subroutine, the program runs as usual:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>run</b>
Starting program: /work/Editorial/gdb/gnu/m4/m4
<b>define(foo,0000)</b>
<b>foo</b>
0000
</pre></div>
<p>To trigger the breakpoint, we call <code>changequote</code>. <small>GDB</small>
suspends execution of <code>m4</code>, displaying information about the
context where it stops.
</p>
<div class="smallexample">
<pre class="smallexample"><b>changequote(&lt;QUOTE&gt;,&lt;UNQUOTE&gt;)</b>
Breakpoint 1, m4_changequote (argc=3, argv=0x33c70)
at builtin.c:879
879 if (bad_argc(TOKEN_DATA_TEXT(argv[0]),argc,1,3))
</pre></div>
<p>Now we use the command <code>n</code> (<code>next</code>) to advance execution to
the next line of the current function.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>n</b>
882 set_quotes((argc &gt;= 2) ? TOKEN_DATA_TEXT(argv[1])\
: nil,
</pre></div>
<p><code>set_quotes</code> looks like a promising subroutine. We can go into it
by using the command <code>s</code> (<code>step</code>) instead of <code>next</code>.
<code>step</code> goes to the next line to be executed in <em>any</em>
subroutine, so it steps into <code>set_quotes</code>.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>s</b>
set_quotes (lq=0x34c78 &quot;&lt;QUOTE&gt;&quot;, rq=0x34c88 &quot;&lt;UNQUOTE&gt;&quot;)
at input.c:530
530 if (lquote != def_lquote)
</pre></div>
<p>The display that shows the subroutine where <code>m4</code> is now
suspended (and its arguments) is called a stack frame display. It
shows a summary of the stack. We can use the <code>backtrace</code>
command (which can also be spelled <code>bt</code>), to see where we are
in the stack as a whole: the <code>backtrace</code> command displays a
stack frame for each active subroutine.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>bt</b>
#0 set_quotes (lq=0x34c78 &quot;&lt;QUOTE&gt;&quot;, rq=0x34c88 &quot;&lt;UNQUOTE&gt;&quot;)
at input.c:530
#1 0x6344 in m4_changequote (argc=3, argv=0x33c70)
at builtin.c:882
#2 0x8174 in expand_macro (sym=0x33320) at macro.c:242
#3 0x7a88 in expand_token (obs=0x0, t=209696, td=0xf7fffa30)
at macro.c:71
#4 0x79dc in expand_input () at macro.c:40
#5 0x2930 in main (argc=0, argv=0xf7fffb20) at m4.c:195
</pre></div>
<p>We step through a few more lines to see what happens. The first two
times, we can use &lsquo;<samp>s</samp>&rsquo;; the next two times we use <code>n</code> to avoid
falling into the <code>xstrdup</code> subroutine.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>s</b>
0x3b5c 532 if (rquote != def_rquote)
(gdb) <b>s</b>
0x3b80 535 lquote = (lq == nil || *lq == '\0') ? \
def_lquote : xstrdup(lq);
(gdb) <b>n</b>
536 rquote = (rq == nil || *rq == '\0') ? def_rquote\
: xstrdup(rq);
(gdb) <b>n</b>
538 len_lquote = strlen(rquote);
</pre></div>
<p>The last line displayed looks a little odd; we can examine the variables
<code>lquote</code> and <code>rquote</code> to see if they are in fact the new left
and right quotes we specified. We use the command <code>p</code>
(<code>print</code>) to see their values.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>p lquote</b>
$1 = 0x35d40 &quot;&lt;QUOTE&gt;&quot;
(gdb) <b>p rquote</b>
$2 = 0x35d50 &quot;&lt;UNQUOTE&gt;&quot;
</pre></div>
<p><code>lquote</code> and <code>rquote</code> are indeed the new left and right quotes.
To look at some context, we can display ten lines of source
surrounding the current line with the <code>l</code> (<code>list</code>) command.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>l</b>
533 xfree(rquote);
534
535 lquote = (lq == nil || *lq == '\0') ? def_lquote\
: xstrdup (lq);
536 rquote = (rq == nil || *rq == '\0') ? def_rquote\
: xstrdup (rq);
537
538 len_lquote = strlen(rquote);
539 len_rquote = strlen(lquote);
540 }
541
542 void
</pre></div>
<p>Let us step past the two lines that set <code>len_lquote</code> and
<code>len_rquote</code>, and then examine the values of those variables.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>n</b>
539 len_rquote = strlen(lquote);
(gdb) <b>n</b>
540 }
(gdb) <b>p len_lquote</b>
$3 = 9
(gdb) <b>p len_rquote</b>
$4 = 7
</pre></div>
<p>That certainly looks wrong, assuming <code>len_lquote</code> and
<code>len_rquote</code> are meant to be the lengths of <code>lquote</code> and
<code>rquote</code> respectively. We can set them to better values using
the <code>p</code> command, since it can print the value of
any expression&mdash;and that expression can include subroutine calls and
assignments.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>p len_lquote=strlen(lquote)</b>
$5 = 7
(gdb) <b>p len_rquote=strlen(rquote)</b>
$6 = 9
</pre></div>
<p>Is that enough to fix the problem of using the new quotes with the
<code>m4</code> built-in <code>defn</code>? We can allow <code>m4</code> to continue
executing with the <code>c</code> (<code>continue</code>) command, and then try the
example that caused trouble initially:
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>c</b>
Continuing.
<b>define(baz,defn(&lt;QUOTE&gt;foo&lt;UNQUOTE&gt;))</b>
baz
0000
</pre></div>
<p>Success! The new quotes now work just as well as the default ones. The
problem seems to have been just the two typos defining the wrong
lengths. We allow <code>m4</code> exit by giving it an EOF as input:
</p>
<div class="smallexample">
<pre class="smallexample"><b>Ctrl-d</b>
Program exited normally.
</pre></div>
<p>The message &lsquo;<samp>Program exited normally.</samp>&rsquo; is from <small>GDB</small>; it
indicates <code>m4</code> has finished executing. We can end our <small>GDB</small>
session with the <small>GDB</small> <code>quit</code> command.
</p>
<div class="smallexample">
<pre class="smallexample">(gdb) <b>quit</b>
</pre></div>
<hr>
<div class="header">
<p>
Next: <a href="Invocation.html#Invocation" accesskey="n" rel="next">Invocation</a>, Previous: <a href="Summary.html#Summary" accesskey="p" rel="prev">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>
</div>
</body>
</html>