Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gdb

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?

like image 218
kp11 Avatar asked Jun 23 '10 07:06

kp11


People also ask

How do I show the value of a variable in GDB?

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.

How do you use watchpoints in GDB?

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.

Which command is used to determine the variable in GDB?

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.


2 Answers

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

like image 64
ninjalj Avatar answered Sep 24 '22 02:09

ninjalj


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.

like image 22
unwind Avatar answered Sep 26 '22 02:09

unwind