<HTML> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- Created on March, 27 2008 by texi2html 1.64 --> <!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others. Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de> Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --> <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="texi2html 1.64"> </HEAD> <BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000"> <A NAME="SEC5"></A> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_1.html#SEC4"> < </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_3.html#SEC6"> > </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top"> Up </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_3.html#SEC6"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <H1> 1. A Sample GDB Session </H1> <!--docid::SEC5::--> <P> You can use this manual at your leisure to read all about GDB. 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 GNU <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><QUOTE></CODE> and the close quote string to <CODE><UNQUOTE></CODE>, the same procedure fails to define a new synonym <CODE>baz</CODE>: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>$ <B>cd gnu/m4</B> $ <B>./m4</B> <B>define(foo,0000)</B> <B>foo</B> 0000 <B>define(bar,defn(`foo'))</B> <B>bar</B> 0000 <B>changequote(<QUOTE>,<UNQUOTE>)</B> <B>define(baz,defn(<QUOTE>foo<UNQUOTE>))</B> <B>baz</B> <B>Ctrl-d</B> m4: End of input: 0: fatal error: EOF in string </FONT></pre></td></tr></table></P><P> Let us use GDB to try to see what is going on. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>$ <B>gdb m4</B> GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions. There is absolutely no warranty for GDB; type "show warranty" for details. GDB 6.8, Copyright 1999 Free Software Foundation, Inc... (gdb) </FONT></pre></td></tr></table></P><P> 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. We now tell GDB to use a narrower display width than usual, so that examples fit in this manual. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>set width 70</B> </FONT></pre></td></tr></table></P><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 GDB <CODE>break</CODE> command. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>break m4_changequote</B> Breakpoint 1 at 0x62f4: file builtin.c, line 879. </FONT></pre></td></tr></table></P><P> Using the <CODE>run</CODE> command, we start <CODE>m4</CODE> running under GDB control; as long as control does not reach the <CODE>m4_changequote</CODE> subroutine, the program runs as usual: </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>run</B> Starting program: /work/Editorial/gdb/gnu/m4/m4 <B>define(foo,0000)</B> <B>foo</B> 0000 </FONT></pre></td></tr></table></P><P> To trigger the breakpoint, we call <CODE>changequote</CODE>. GDB suspends execution of <CODE>m4</CODE>, displaying information about the context where it stops. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre><B>changequote(<QUOTE>,<UNQUOTE>)</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)) </FONT></pre></td></tr></table></P><P> Now we use the command <CODE>n</CODE> (<CODE>next</CODE>) to advance execution to the next line of the current function. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>n</B> 882 set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])\ : nil, </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>s</B> set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>") at input.c:530 530 if (lquote != def_lquote) </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>bt</B> #0 set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>") 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 </FONT></pre></td></tr></table></P><P> We step through a few more lines to see what happens. The first two times, we can use <SAMP>`s'</SAMP>; the next two times we use <CODE>n</CODE> to avoid falling into the <CODE>xstrdup</CODE> subroutine. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(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); </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>p lquote</B> $1 = 0x35d40 "<QUOTE>" (gdb) <B>p rquote</B> $2 = 0x35d50 "<UNQUOTE>" </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(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 </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(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 </FONT></pre></td></tr></table></P><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--and that expression can include subroutine calls and assignments. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>p len_lquote=strlen(lquote)</B> $5 = 7 (gdb) <B>p len_rquote=strlen(rquote)</B> $6 = 9 </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>c</B> Continuing. <B>define(baz,defn(<QUOTE>foo<UNQUOTE>))</B> baz 0000 </FONT></pre></td></tr></table></P><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><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre><B>Ctrl-d</B> Program exited normally. </FONT></pre></td></tr></table></P><P> The message <SAMP>`Program exited normally.'</SAMP> is from GDB; it indicates <CODE>m4</CODE> has finished executing. We can end our GDB session with the GDB <CODE>quit</CODE> command. </P><P> <TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>(gdb) <B>quit</B> </FONT></pre></td></tr></table></P><P> <A NAME="Invocation"></A> <HR SIZE="6"> <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[ << ]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_3.html#SEC6"> >> </A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb.html#SEC_Top">Top</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_toc.html#SEC_Contents">Contents</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_38.html#SEC764">Index</A>]</TD> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gdb_abt.html#SEC_About"> ? </A>]</TD> </TR></TABLE> <BR> <FONT SIZE="-1"> <address> <p>Please send FSF & GNU inquiries & questions to <a href="mailto:gnu@gnu.org">gnu@gnu.org</a>. There are also <a href="http://www.gnu.org/home.html#ContactInfo">other ways to contact</a> the FSF.</p> <p>These pages are maintained by <a href="http://www.gnu.org/software/gdb/">the GDB developers</a>.</p> <p>Copyright Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.</p> <p>Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.</p> </address> This document was generated by <I>GDB Administrator</I> on <I>March, 27 2008</I> using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html "><I>texi2html</I></A> </BODY> </HTML>