I have a process which exchanges data with some devices(OS Linux). I would like to attach to this process (not stopping it), and watch values of certain variables and other information in real time. Is it possible to do it with GDB or something else and if yes how to do it? So far I'm using printfs for this purpose.
gdb has two facilities that help with this debugging scenario.
One is the relatively new dprintf
command. This is basically like printf
debugging -- except you can choose what to print during your gdb session, and you don't need to recompile.
dprintf
isn't always very "realtime". By default it involves stopping your inferior so that gdb can extract information and print it. You may want set dprintf-style agent
, though I think this implies using a remote agent, and not all of those support this feature.
The other facility is the gdb "tracepoint" feature. Tracepoints were originally designed to support just this situation -- logging of data from realtime servers that could not be stopped.
Tracepoints also only work with a suitable remote agent. You can just use gdbserver
, though -- this is easy to do and works just fine locally.
There's extensive documentation of tracepoints in the gdb manual, which I suggest reading for an understanding of how to use them.