No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

325 líneas
12KB

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