You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
8.8KB

  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>Dynamic Printf (Debugging with GDB)</title>
  17. <meta name="description" content="Dynamic Printf (Debugging with GDB)">
  18. <meta name="keywords" content="Dynamic Printf (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="Breakpoints.html#Breakpoints" rel="up" title="Breakpoints">
  26. <link href="Save-Breakpoints.html#Save-Breakpoints" rel="next" title="Save Breakpoints">
  27. <link href="Break-Commands.html#Break-Commands" rel="prev" title="Break Commands">
  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="Dynamic-Printf"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Save-Breakpoints.html#Save-Breakpoints" accesskey="n" rel="next">Save Breakpoints</a>, Previous: <a href="Break-Commands.html#Break-Commands" accesskey="p" rel="prev">Break Commands</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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="Dynamic-Printf-1"></a>
  64. <h4 class="subsection">5.1.8 Dynamic Printf</h4>
  65. <a name="index-dynamic-printf"></a>
  66. <a name="index-dprintf"></a>
  67. <p>The dynamic printf command <code>dprintf</code> combines a breakpoint with
  68. formatted printing of your program&rsquo;s data to give you the effect of
  69. inserting <code>printf</code> calls into your program on-the-fly, without
  70. having to recompile it.
  71. </p>
  72. <p>In its most basic form, the output goes to the GDB console. However,
  73. you can set the variable <code>dprintf-style</code> for alternate handling.
  74. For instance, you can ask to format the output by calling your
  75. program&rsquo;s <code>printf</code> function. This has the advantage that the
  76. characters go to the program&rsquo;s output device, so they can recorded in
  77. redirects to files and so forth.
  78. </p>
  79. <p>If you are doing remote debugging with a stub or agent, you can also
  80. ask to have the printf handled by the remote agent. In addition to
  81. ensuring that the output goes to the remote program&rsquo;s device along
  82. with any other output the program might produce, you can also ask that
  83. the dprintf remain active even after disconnecting from the remote
  84. target. Using the stub/agent is also more efficient, as it can do
  85. everything without needing to communicate with <small>GDB</small>.
  86. </p>
  87. <dl compact="compact">
  88. <dd><a name="index-dprintf-1"></a>
  89. </dd>
  90. <dt><code>dprintf <var>location</var>,<var>template</var>,<var>expression</var>[,<var>expression</var>&hellip;]</code></dt>
  91. <dd><p>Whenever execution reaches <var>location</var>, print the values of one or
  92. more <var>expressions</var> under the control of the string <var>template</var>.
  93. To print several values, separate them with commas.
  94. </p>
  95. </dd>
  96. <dt><code>set dprintf-style <var>style</var></code></dt>
  97. <dd><p>Set the dprintf output to be handled in one of several different
  98. styles enumerated below. A change of style affects all existing
  99. dynamic printfs immediately. (If you need individual control over the
  100. print commands, simply define normal breakpoints with
  101. explicitly-supplied command lists.)
  102. </p>
  103. <dl compact="compact">
  104. <dt><code>gdb</code></dt>
  105. <dd><a name="index-dprintf_002dstyle-gdb"></a>
  106. <p>Handle the output using the <small>GDB</small> <code>printf</code> command.
  107. </p>
  108. </dd>
  109. <dt><code>call</code></dt>
  110. <dd><a name="index-dprintf_002dstyle-call"></a>
  111. <p>Handle the output by calling a function in your program (normally
  112. <code>printf</code>).
  113. </p>
  114. </dd>
  115. <dt><code>agent</code></dt>
  116. <dd><a name="index-dprintf_002dstyle-agent"></a>
  117. <p>Have the remote debugging agent (such as <code>gdbserver</code>) handle
  118. the output itself. This style is only available for agents that
  119. support running commands on the target.
  120. </p></dd>
  121. </dl>
  122. </dd>
  123. <dt><code>set dprintf-function <var>function</var></code></dt>
  124. <dd><p>Set the function to call if the dprintf style is <code>call</code>. By
  125. default its value is <code>printf</code>. You may set it to any expression.
  126. that <small>GDB</small> can evaluate to a function, as per the <code>call</code>
  127. command.
  128. </p>
  129. </dd>
  130. <dt><code>set dprintf-channel <var>channel</var></code></dt>
  131. <dd><p>Set a &ldquo;channel&rdquo; for dprintf. If set to a non-empty value,
  132. <small>GDB</small> will evaluate it as an expression and pass the result as
  133. a first argument to the <code>dprintf-function</code>, in the manner of
  134. <code>fprintf</code> and similar functions. Otherwise, the dprintf format
  135. string will be the first argument, in the manner of <code>printf</code>.
  136. </p>
  137. <p>As an example, if you wanted <code>dprintf</code> output to go to a logfile
  138. that is a standard I/O stream assigned to the variable <code>mylog</code>,
  139. you could do the following:
  140. </p>
  141. <div class="example">
  142. <pre class="example">(gdb) set dprintf-style call
  143. (gdb) set dprintf-function fprintf
  144. (gdb) set dprintf-channel mylog
  145. (gdb) dprintf 25,&quot;at line 25, glob=%d\n&quot;,glob
  146. Dprintf 1 at 0x123456: file main.c, line 25.
  147. (gdb) info break
  148. 1 dprintf keep y 0x00123456 in main at main.c:25
  149. call (void) fprintf (mylog,&quot;at line 25, glob=%d\n&quot;,glob)
  150. continue
  151. (gdb)
  152. </pre></div>
  153. <p>Note that the <code>info break</code> displays the dynamic printf commands
  154. as normal breakpoint commands; you can thus easily see the effect of
  155. the variable settings.
  156. </p>
  157. </dd>
  158. <dt><code>set disconnected-dprintf on</code></dt>
  159. <dt><code>set disconnected-dprintf off</code></dt>
  160. <dd><a name="index-set-disconnected_002ddprintf"></a>
  161. <p>Choose whether <code>dprintf</code> commands should continue to run if
  162. <small>GDB</small> has disconnected from the target. This only applies
  163. if the <code>dprintf-style</code> is <code>agent</code>.
  164. </p>
  165. </dd>
  166. <dt><code>show disconnected-dprintf off</code></dt>
  167. <dd><a name="index-show-disconnected_002ddprintf"></a>
  168. <p>Show the current choice for disconnected <code>dprintf</code>.
  169. </p>
  170. </dd>
  171. </dl>
  172. <p><small>GDB</small> does not check the validity of function and channel,
  173. relying on you to supply values that are meaningful for the contexts
  174. in which they are being used. For instance, the function and channel
  175. may be the values of local variables, but if that is the case, then
  176. all enabled dynamic prints must be at locations within the scope of
  177. those locals. If evaluation fails, <small>GDB</small> will report an error.
  178. </p>
  179. <hr>
  180. <div class="header">
  181. <p>
  182. Next: <a href="Save-Breakpoints.html#Save-Breakpoints" accesskey="n" rel="next">Save Breakpoints</a>, Previous: <a href="Break-Commands.html#Break-Commands" accesskey="p" rel="prev">Break Commands</a>, Up: <a href="Breakpoints.html#Breakpoints" accesskey="u" rel="up">Breakpoints</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>
  183. </div>
  184. </body>
  185. </html>