您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

342 行
13KB

  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>Macros (Debugging with GDB)</title>
  17. <meta name="description" content="Macros (Debugging with GDB)">
  18. <meta name="keywords" content="Macros (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="Tracepoints.html#Tracepoints" rel="next" title="Tracepoints">
  27. <link href="Tail-Call-Frames.html#Tail-Call-Frames" rel="prev" title="Tail Call Frames">
  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="Macros"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Tracepoints.html#Tracepoints" accesskey="n" rel="next">Tracepoints</a>, Previous: <a href="Optimized-Code.html#Optimized-Code" accesskey="p" rel="prev">Optimized Code</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="C-Preprocessor-Macros"></a>
  64. <h2 class="chapter">12 C Preprocessor Macros</h2>
  65. <p>Some languages, such as C and C<tt>++</tt>, provide a way to define and invoke
  66. &ldquo;preprocessor macros&rdquo; which expand into strings of tokens.
  67. <small>GDB</small> can evaluate expressions containing macro invocations, show
  68. the result of macro expansion, and show a macro&rsquo;s definition, including
  69. where it was defined.
  70. </p>
  71. <p>You may need to compile your program specially to provide <small>GDB</small>
  72. with information about preprocessor macros. Most compilers do not
  73. include macros in their debugging information, even when you compile
  74. with the <samp>-g</samp> flag. See <a href="Compilation.html#Compilation">Compilation</a>.
  75. </p>
  76. <p>A program may define a macro at one point, remove that definition later,
  77. and then provide a different definition after that. Thus, at different
  78. points in the program, a macro may have different definitions, or have
  79. no definition at all. If there is a current stack frame, <small>GDB</small>
  80. uses the macros in scope at that frame&rsquo;s source code line. Otherwise,
  81. <small>GDB</small> uses the macros in scope at the current listing location;
  82. see <a href="List.html#List">List</a>.
  83. </p>
  84. <p>Whenever <small>GDB</small> evaluates an expression, it always expands any
  85. macro invocations present in the expression. <small>GDB</small> also provides
  86. the following commands for working with macros explicitly.
  87. </p>
  88. <dl compact="compact">
  89. <dd>
  90. <a name="index-macro-expand"></a>
  91. <a name="index-macro-expansion_002c-showing-the-results-of-preprocessor"></a>
  92. <a name="index-preprocessor-macro-expansion_002c-showing-the-results-of"></a>
  93. <a name="index-expanding-preprocessor-macros"></a>
  94. </dd>
  95. <dt><code>macro expand <var>expression</var></code></dt>
  96. <dt><code>macro exp <var>expression</var></code></dt>
  97. <dd><p>Show the results of expanding all preprocessor macro invocations in
  98. <var>expression</var>. Since <small>GDB</small> simply expands macros, but does
  99. not parse the result, <var>expression</var> need not be a valid expression;
  100. it can be any string of tokens.
  101. </p>
  102. <a name="index-macro-exp1"></a>
  103. </dd>
  104. <dt><code>macro expand-once <var>expression</var></code></dt>
  105. <dt><code>macro exp1 <var>expression</var></code></dt>
  106. <dd><a name="index-expand-macro-once"></a>
  107. <p><i>(This command is not yet implemented.)</i> Show the results of
  108. expanding those preprocessor macro invocations that appear explicitly in
  109. <var>expression</var>. Macro invocations appearing in that expansion are
  110. left unchanged. This command allows you to see the effect of a
  111. particular macro more clearly, without being confused by further
  112. expansions. Since <small>GDB</small> simply expands macros, but does not
  113. parse the result, <var>expression</var> need not be a valid expression; it
  114. can be any string of tokens.
  115. </p>
  116. <a name="index-info-macro"></a>
  117. <a name="index-macro-definition_002c-showing"></a>
  118. <a name="index-definition-of-a-macro_002c-showing"></a>
  119. <a name="index-macros_002c-from-debug-info"></a>
  120. </dd>
  121. <dt><code>info macro [-a|-all] [--] <var>macro</var></code></dt>
  122. <dd><p>Show the current definition or all definitions of the named <var>macro</var>,
  123. and describe the source location or compiler command-line where that
  124. definition was established. The optional double dash is to signify the end of
  125. argument processing and the beginning of <var>macro</var> for non C-like macros where
  126. the macro may begin with a hyphen.
  127. </p>
  128. <a name="index-info-macros"></a>
  129. </dd>
  130. <dt><code>info macros <var>location</var></code></dt>
  131. <dd><p>Show all macro definitions that are in effect at the location specified
  132. by <var>location</var>, and describe the source location or compiler
  133. command-line where those definitions were established.
  134. </p>
  135. <a name="index-macro-define"></a>
  136. <a name="index-user_002ddefined-macros"></a>
  137. <a name="index-defining-macros-interactively"></a>
  138. <a name="index-macros_002c-user_002ddefined"></a>
  139. </dd>
  140. <dt><code>macro define <var>macro</var> <var>replacement-list</var></code></dt>
  141. <dt><code>macro define <var>macro</var>(<var>arglist</var>) <var>replacement-list</var></code></dt>
  142. <dd><p>Introduce a definition for a preprocessor macro named <var>macro</var>,
  143. invocations of which are replaced by the tokens given in
  144. <var>replacement-list</var>. The first form of this command defines an
  145. &ldquo;object-like&rdquo; macro, which takes no arguments; the second form
  146. defines a &ldquo;function-like&rdquo; macro, which takes the arguments given in
  147. <var>arglist</var>.
  148. </p>
  149. <p>A definition introduced by this command is in scope in every
  150. expression evaluated in <small>GDB</small>, until it is removed with the
  151. <code>macro undef</code> command, described below. The definition overrides
  152. all definitions for <var>macro</var> present in the program being debugged,
  153. as well as any previous user-supplied definition.
  154. </p>
  155. <a name="index-macro-undef"></a>
  156. </dd>
  157. <dt><code>macro undef <var>macro</var></code></dt>
  158. <dd><p>Remove any user-supplied definition for the macro named <var>macro</var>.
  159. This command only affects definitions provided with the <code>macro
  160. define</code> command, described above; it cannot remove definitions present
  161. in the program being debugged.
  162. </p>
  163. <a name="index-macro-list"></a>
  164. </dd>
  165. <dt><code>macro list</code></dt>
  166. <dd><p>List all the macros defined using the <code>macro define</code> command.
  167. </p></dd>
  168. </dl>
  169. <a name="index-macros_002c-example-of-debugging-with"></a>
  170. <p>Here is a transcript showing the above commands in action. First, we
  171. show our source files:
  172. </p>
  173. <div class="smallexample">
  174. <pre class="smallexample">$ cat sample.c
  175. #include &lt;stdio.h&gt;
  176. #include &quot;sample.h&quot;
  177. #define M 42
  178. #define ADD(x) (M + x)
  179. main ()
  180. {
  181. #define N 28
  182. printf (&quot;Hello, world!\n&quot;);
  183. #undef N
  184. printf (&quot;We're so creative.\n&quot;);
  185. #define N 1729
  186. printf (&quot;Goodbye, world!\n&quot;);
  187. }
  188. $ cat sample.h
  189. #define Q &lt;
  190. $
  191. </pre></div>
  192. <p>Now, we compile the program using the <small>GNU</small> C compiler,
  193. <small>GCC</small>. We pass the <samp>-gdwarf-2</samp><a name="DOCF14" href="#FOOT14"><sup>14</sup></a> <em>and</em> <samp>-g3</samp> flags to ensure the compiler
  194. includes information about preprocessor macros in the debugging
  195. information.
  196. </p>
  197. <div class="smallexample">
  198. <pre class="smallexample">$ gcc -gdwarf-2 -g3 sample.c -o sample
  199. $
  200. </pre></div>
  201. <p>Now, we start <small>GDB</small> on our sample program:
  202. </p>
  203. <div class="smallexample">
  204. <pre class="smallexample">$ gdb -nw sample
  205. GNU gdb 2002-05-06-cvs
  206. Copyright 2002 Free Software Foundation, Inc.
  207. GDB is free software, &hellip;
  208. (gdb)
  209. </pre></div>
  210. <p>We can expand macros and examine their definitions, even when the
  211. program is not running. <small>GDB</small> uses the current listing position
  212. to decide which macro definitions are in scope:
  213. </p>
  214. <div class="smallexample">
  215. <pre class="smallexample">(gdb) list main
  216. 3
  217. 4 #define M 42
  218. 5 #define ADD(x) (M + x)
  219. 6
  220. 7 main ()
  221. 8 {
  222. 9 #define N 28
  223. 10 printf (&quot;Hello, world!\n&quot;);
  224. 11 #undef N
  225. 12 printf (&quot;We're so creative.\n&quot;);
  226. (gdb) info macro ADD
  227. Defined at /home/jimb/gdb/macros/play/sample.c:5
  228. #define ADD(x) (M + x)
  229. (gdb) info macro Q
  230. Defined at /home/jimb/gdb/macros/play/sample.h:1
  231. included at /home/jimb/gdb/macros/play/sample.c:2
  232. #define Q &lt;
  233. (gdb) macro expand ADD(1)
  234. expands to: (42 + 1)
  235. (gdb) macro expand-once ADD(1)
  236. expands to: once (M + 1)
  237. (gdb)
  238. </pre></div>
  239. <p>In the example above, note that <code>macro expand-once</code> expands only
  240. the macro invocation explicit in the original text &mdash; the invocation of
  241. <code>ADD</code> &mdash; but does not expand the invocation of the macro <code>M</code>,
  242. which was introduced by <code>ADD</code>.
  243. </p>
  244. <p>Once the program is running, <small>GDB</small> uses the macro definitions in
  245. force at the source line of the current stack frame:
  246. </p>
  247. <div class="smallexample">
  248. <pre class="smallexample">(gdb) break main
  249. Breakpoint 1 at 0x8048370: file sample.c, line 10.
  250. (gdb) run
  251. Starting program: /home/jimb/gdb/macros/play/sample
  252. Breakpoint 1, main () at sample.c:10
  253. 10 printf (&quot;Hello, world!\n&quot;);
  254. (gdb)
  255. </pre></div>
  256. <p>At line 10, the definition of the macro <code>N</code> at line 9 is in force:
  257. </p>
  258. <div class="smallexample">
  259. <pre class="smallexample">(gdb) info macro N
  260. Defined at /home/jimb/gdb/macros/play/sample.c:9
  261. #define N 28
  262. (gdb) macro expand N Q M
  263. expands to: 28 &lt; 42
  264. (gdb) print N Q M
  265. $1 = 1
  266. (gdb)
  267. </pre></div>
  268. <p>As we step over directives that remove <code>N</code>&rsquo;s definition, and then
  269. give it a new definition, <small>GDB</small> finds the definition (or lack
  270. thereof) in force at each point:
  271. </p>
  272. <div class="smallexample">
  273. <pre class="smallexample">(gdb) next
  274. Hello, world!
  275. 12 printf (&quot;We're so creative.\n&quot;);
  276. (gdb) info macro N
  277. The symbol `N' has no definition as a C/C++ preprocessor macro
  278. at /home/jimb/gdb/macros/play/sample.c:12
  279. (gdb) next
  280. We're so creative.
  281. 14 printf (&quot;Goodbye, world!\n&quot;);
  282. (gdb) info macro N
  283. Defined at /home/jimb/gdb/macros/play/sample.c:13
  284. #define N 1729
  285. (gdb) macro expand N Q M
  286. expands to: 1729 &lt; 42
  287. (gdb) print N Q M
  288. $2 = 0
  289. (gdb)
  290. </pre></div>
  291. <p>In addition to source files, macros can be defined on the compilation command
  292. line using the <samp>-D<var>name</var>=<var>value</var></samp> syntax. For macros defined in
  293. such a way, <small>GDB</small> displays the location of their definition as line zero
  294. of the source file submitted to the compiler.
  295. </p>
  296. <div class="smallexample">
  297. <pre class="smallexample">(gdb) info macro __STDC__
  298. Defined at /home/jimb/gdb/macros/play/sample.c:0
  299. -D__STDC__=1
  300. (gdb)
  301. </pre></div>
  302. <div class="footnote">
  303. <hr>
  304. <h4 class="footnotes-heading">Footnotes</h4>
  305. <h3><a name="FOOT14" href="#DOCF14">(14)</a></h3>
  306. <p>This is the
  307. minimum. Recent versions of <small>GCC</small> support <samp>-gdwarf-3</samp>
  308. and <samp>-gdwarf-4</samp>; we recommend always choosing the most recent
  309. version of DWARF.</p>
  310. </div>
  311. <hr>
  312. <div class="header">
  313. <p>
  314. Next: <a href="Tracepoints.html#Tracepoints" accesskey="n" rel="next">Tracepoints</a>, Previous: <a href="Optimized-Code.html#Optimized-Code" accesskey="p" rel="prev">Optimized Code</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>
  315. </div>
  316. </body>
  317. </html>