tags:

views:

209

answers:

2

I haven't worked with gdb for a long time and this feels like a basic question.

I am trying to observe a structure as it changes but rather than break at a specific point and print it out I'd rather let the application run as normal and give me a snapshot of the structure at a specific point. Think a breakpoint that performs an action (print struct) rather than pausing execution.

I am interested in looking at the changes to the structure all at once rather than incrementally. I can get what I want through printf, but gdb is much more elegant.

Update: Thank you for all the responses. I want to watch one struct at a particular point and the commands solution is just what I needed. This has been very helpful.

+7  A: 

A nice approach is to set a breakpoint with associated commands, e.g:

break main.c:100
commands 1
print data_structure
continue
end

This runs the two commands print data_structure and continue whenever breakpoint 1 is reached.

Jefromi
Or even better - set a watchpoint, so you can catch ALL places where the structure is being modified.
Nikolai N Fetissov
Right - but the original question used the words "specific point" twice. Often, a structure will be frequently touched by many pieces of code, but only one particular one is of interest.
Jefromi
+2  A: 

If the information held by your data structure might be changed by several code lines, you can also use gdb's watch. Note that it is horribly slow so it should be used carefully. The commands part is just the same.

(gdb) break main
Breakpoint 1 at 0x80483b5:
(gdb) run
Breakpoint 1, main ()
(gdb) watch data_structure
Hardware watchpoint 2: data_structure
(gdb) commands 2
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
> print data_structure
> continue
> end
(gdb) continue
eyalm