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?
Able to set a watchpoint on a variable in order to break a program when a variable changes. Use display to automatically print how variables change throughout the program's execution. watch allows us to stop the execution every time the value of a variable changes.
If GDB cannot set a hardware watchpoint, it sets a software watchpoint, which executes more slowly and reports the change in value at the next statement, not the instruction, after the change occurs. You can force GDB to use only software watchpoints with the set can-use-hw-watchpoints 0 command.
The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With