Macros.html 13 KB

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