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-2021 Free Software Foundation, Inc.
4
5Permission is granted to copy, distribute and/or modify this document
6under the terms of the GNU Free Documentation License, Version 1.3 or
7any later version published by the Free Software Foundation; with the
8Invariant Sections being "Free Software" and "Free Software Needs
9Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
10and with the Back-Cover Texts as in (a) below.
11
12(a) The FSF's Back-Cover Text is: "You are free to copy and modify
13this GNU Manual.  Buying copies from GNU Press supports the FSF in
14developing GNU and promoting software freedom." -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>Debugging with GDB: Dynamic printf</title>
18
19<meta name="description" content="Debugging with GDB: Dynamic printf">
20<meta name="keywords" content="Debugging with GDB: Dynamic printf">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Breakpoints.html#Breakpoints" rel="up" title="Breakpoints">
29<link href="Save-Breakpoints.html#Save-Breakpoints" rel="next" title="Save Breakpoints">
30<link href="Break-Commands.html#Break-Commands" rel="previous" title="Break Commands">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Dynamic-printf"></a>
65<div class="header">
66<p>
67Next: <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>
68</div>
69<hr>
70<a name="Dynamic-printf-1"></a>
71<h4 class="subsection">5.1.8 Dynamic printf</h4>
72
73<a name="index-dynamic-printf"></a>
74<a name="index-dprintf"></a>
75<p>The dynamic printf command <code>dprintf</code> combines a breakpoint with
76formatted printing of your program&rsquo;s data to give you the effect of
77inserting <code>printf</code> calls into your program on-the-fly, without
78having to recompile it.
79</p>
80<p>In its most basic form, the output goes to the GDB console.  However,
81you can set the variable <code>dprintf-style</code> for alternate handling.
82For instance, you can ask to format the output by calling your
83program&rsquo;s <code>printf</code> function.  This has the advantage that the
84characters go to the program&rsquo;s output device, so they can recorded in
85redirects to files and so forth.
86</p>
87<p>If you are doing remote debugging with a stub or agent, you can also
88ask to have the printf handled by the remote agent.  In addition to
89ensuring that the output goes to the remote program&rsquo;s device along
90with any other output the program might produce, you can also ask that
91the dprintf remain active even after disconnecting from the remote
92target.  Using the stub/agent is also more efficient, as it can do
93everything without needing to communicate with <small>GDB</small>.
94</p>
95<dl compact="compact">
96<dd><a name="index-dprintf-1"></a>
97</dd>
98<dt><code>dprintf <var>location</var>,<var>template</var>,<var>expression</var>[,<var>expression</var>&hellip;]</code></dt>
99<dd><p>Whenever execution reaches <var>location</var>, print the values of one or
100more <var>expressions</var> under the control of the string <var>template</var>.
101To print several values, separate them with commas.
102</p>
103</dd>
104<dt><code>set dprintf-style <var>style</var></code></dt>
105<dd><p>Set the dprintf output to be handled in one of several different
106styles enumerated below.  A change of style affects all existing
107dynamic printfs immediately.  (If you need individual control over the
108print commands, simply define normal breakpoints with
109explicitly-supplied command lists.)
110</p>
111<dl compact="compact">
112<dt><code>gdb</code></dt>
113<dd><a name="index-dprintf_002dstyle-gdb"></a>
114<p>Handle the output using the <small>GDB</small> <code>printf</code> command.
115</p>
116</dd>
117<dt><code>call</code></dt>
118<dd><a name="index-dprintf_002dstyle-call"></a>
119<p>Handle the output by calling a function in your program (normally
120<code>printf</code>).
121</p>
122</dd>
123<dt><code>agent</code></dt>
124<dd><a name="index-dprintf_002dstyle-agent"></a>
125<p>Have the remote debugging agent (such as <code>gdbserver</code>) handle
126the output itself.  This style is only available for agents that
127support running commands on the target.
128</p></dd>
129</dl>
130
131</dd>
132<dt><code>set dprintf-function <var>function</var></code></dt>
133<dd><p>Set the function to call if the dprintf style is <code>call</code>.  By
134default its value is <code>printf</code>.  You may set it to any expression.
135that <small>GDB</small> can evaluate to a function, as per the <code>call</code>
136command.
137</p>
138</dd>
139<dt><code>set dprintf-channel <var>channel</var></code></dt>
140<dd><p>Set a &ldquo;channel&rdquo; for dprintf.  If set to a non-empty value,
141<small>GDB</small> will evaluate it as an expression and pass the result as
142a first argument to the <code>dprintf-function</code>, in the manner of
143<code>fprintf</code> and similar functions.  Otherwise, the dprintf format
144string will be the first argument, in the manner of <code>printf</code>.
145</p>
146<p>As an example, if you wanted <code>dprintf</code> output to go to a logfile
147that is a standard I/O stream assigned to the variable <code>mylog</code>,
148you could do the following:
149</p>
150<div class="example">
151<pre class="example">(gdb) set dprintf-style call
152(gdb) set dprintf-function fprintf
153(gdb) set dprintf-channel mylog
154(gdb) dprintf 25,&quot;at line 25, glob=%d\n&quot;,glob
155Dprintf 1 at 0x123456: file main.c, line 25.
156(gdb) info break
1571       dprintf        keep y   0x00123456 in main at main.c:25
158        call (void) fprintf (mylog,&quot;at line 25, glob=%d\n&quot;,glob)
159        continue
160(gdb)
161</pre></div>
162
163<p>Note that the <code>info break</code> displays the dynamic printf commands
164as normal breakpoint commands; you can thus easily see the effect of
165the variable settings.
166</p>
167</dd>
168<dt><code>set disconnected-dprintf on</code></dt>
169<dt><code>set disconnected-dprintf off</code></dt>
170<dd><a name="index-set-disconnected_002ddprintf"></a>
171<p>Choose whether <code>dprintf</code> commands should continue to run if
172<small>GDB</small> has disconnected from the target.  This only applies
173if the <code>dprintf-style</code> is <code>agent</code>.
174</p>
175</dd>
176<dt><code>show disconnected-dprintf off</code></dt>
177<dd><a name="index-show-disconnected_002ddprintf"></a>
178<p>Show the current choice for disconnected <code>dprintf</code>.
179</p>
180</dd>
181</dl>
182
183<p><small>GDB</small> does not check the validity of function and channel,
184relying on you to supply values that are meaningful for the contexts
185in which they are being used.  For instance, the function and channel
186may be the values of local variables, but if that is the case, then
187all enabled dynamic prints must be at locations within the scope of
188those locals.  If evaluation fails, <small>GDB</small> will report an error.
189</p>
190<hr>
191<div class="header">
192<p>
193Next: <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>
194</div>
195
196
197
198</body>
199</html>
200