Sample-Session.html 12 KB

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