<HTML> <HEAD> <!-- This HTML file has been created by texi2html 1.51 from /mnt/apple/gdb/source/gdb.apple/source/gdb/gdb/doc/gdb.texinfo on 23 November 1999 --> <TITLE>Debugging with GDB - A Sample GDB Session</TITLE> </HEAD> <BODY> Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_1.html">previous</A>, <A HREF="gdb_3.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>. <P><HR><P> <H1><A NAME="SEC4" HREF="gdb_toc.html#TOC4">A Sample GDB Session</A></H1> <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> In this sample session, we emphasize user input like this: <B>input</B>, to make it easier to pick out from the surrounding output. </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> <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>C-d</B> m4: End of input: 0: fatal error: EOF in string </PRE> <P> Let us use GDB to try to see what is going on. </P> <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 19990707, Copyright 1999 Free Software Foundation, Inc... (gdb) </PRE> <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> <PRE> (gdb) <B>set width 70</B> </PRE> <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> <PRE> (gdb) <B>break m4_changequote</B> Breakpoint 1 at 0x62f4: file builtin.c, line 879. </PRE> <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> <PRE> (gdb) <B>run</B> Starting program: /work/Editorial/gdb/gnu/m4/m4 <B>define(foo,0000)</B> <B>foo</B> 0000 </PRE> <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> <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)) </PRE> <P> Now we use the command <CODE>n</CODE> (<CODE>next</CODE>) to advance execution to the next line of the current function. </P> <PRE> (gdb) <B>n</B> 882 set_quotes((argc >= 2) ? TOKEN_DATA_TEXT(argv[1])\ : nil, </PRE> <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> <PRE> (gdb) <B>s</B> set_quotes (lq=0x34c78 "<QUOTE>", rq=0x34c88 "<UNQUOTE>") at input.c:530 530 if (lquote != def_lquote) </PRE> <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> <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 </PRE> <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> <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); </PRE> <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> <PRE> (gdb) <B>p lquote</B> $1 = 0x35d40 "<QUOTE>" (gdb) <B>p rquote</B> $2 = 0x35d50 "<UNQUOTE>" </PRE> <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> <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 </PRE> <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> <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 </PRE> <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> <PRE> (gdb) <B>p len_lquote=strlen(lquote)</B> $5 = 7 (gdb) <B>p len_rquote=strlen(rquote)</B> $6 = 9 </PRE> <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> <PRE> (gdb) <B>c</B> Continuing. <B>define(baz,defn(<QUOTE>foo<UNQUOTE>))</B> baz 0000 </PRE> <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> <PRE> <B>C-d</B> Program exited normally. </PRE> <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> <PRE> (gdb) <B>quit</B> </PRE> <P><HR><P> Go to the <A HREF="gdb_1.html">first</A>, <A HREF="gdb_1.html">previous</A>, <A HREF="gdb_3.html">next</A>, <A HREF="gdb_21.html">last</A> section, <A HREF="gdb_toc.html">table of contents</A>. </BODY> </HTML>