The dynamic printf command dprintf
combines a breakpoint with
formatted printing of your program's data to give you the effect of
inserting printf
calls into your program on-the-fly, without
having to recompile it.
In its most basic form, the output goes to the GDB console. However,
you can set the variable dprintf-style
for alternate handling.
For instance, you can ask to format the output by calling your
program's printf
function. This has the advantage that the
characters go to the program's output device, so they can recorded in
redirects to files and so forth.
If you are doing remote debugging with a stub or agent, you can also ask to have the printf handled by the remote agent. In addition to ensuring that the output goes to the remote program's device along with any other output the program might produce, you can also ask that the dprintf remain active even after disconnecting from the remote target. Using the stub/agent is also more efficient, as it can do everything without needing to communicate with gdb.
dprintf
location,
template,
expression[,
expression...]
set dprintf-style
stylegdb
printf
command.
call
printf
).
agent
gdbserver
) handle
the output itself. This style is only available for agents that
support running commands on the target.
set dprintf-function
functioncall
. By
default its value is printf
. You may set it to any expression.
that gdb can evaluate to a function, as per the call
command.
set dprintf-channel
channeldprintf-function
, in the manner of
fprintf
and similar functions. Otherwise, the dprintf format
string will be the first argument, in the manner of printf
.
As an example, if you wanted dprintf
output to go to a logfile
that is a standard I/O stream assigned to the variable mylog
,
you could do the following:
(gdb) set dprintf-style call (gdb) set dprintf-function fprintf (gdb) set dprintf-channel mylog (gdb) dprintf 25,"at line 25, glob=%d\n",glob Dprintf 1 at 0x123456: file main.c, line 25. (gdb) info break 1 dprintf keep y 0x00123456 in main at main.c:25 call (void) fprintf (mylog,"at line 25, glob=%d\n",glob) continue (gdb)
Note that the info break
displays the dynamic printf commands
as normal breakpoint commands; you can thus easily see the effect of
the variable settings.
set disconnected-dprintf on
set disconnected-dprintf off
dprintf
commands should continue to run if
gdb has disconnected from the target. This only applies
if the dprintf-style
is agent
.
show disconnected-dprintf off
dprintf
.
gdb does not check the validity of function and channel, relying on you to supply values that are meaningful for the contexts in which they are being used. For instance, the function and channel may be the values of local variables, but if that is the case, then all enabled dynamic prints must be at locations within the scope of those locals. If evaluation fails, gdb will report an error.