kp11
kp11

Reputation: 2125

How to monitor variables in GDB and log it if it meets certain conditions?

I would like to know if there is any way in which I can monitor a value of a variable until for example a counter reaches a value and then log the output of variable value during each counter value?

Upvotes: 32

Views: 56466

Answers (2)

ninjalj
ninjalj

Reputation: 43688

Set a watch point on the counter:

(gdb) watch var

And make that watchpoint conditional:

(gdb) cond <watchpoint_number> var>=value

If you want to log to a file:

(gdb) set logging file <filename>
(gdb) set logging on

By default gdb logs to gdb.txt

Upvotes: 43

unwind
unwind

Reputation: 399703

You can use watchpoints to make gdb monitor the value of a variable, and break execution of the program when the value changes. Once execution is stopped, you can use gdb's command set to inspect and print the value. I'm not sure if you can script gdb to do this automatically each time it breaks.

Upvotes: 4

Related Questions