Dynamic-Printf.html 8.9 KB

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