gdb watchpoint on struct variables

gdb, linux, watchpoint

Solution

First set a breakpoint where you create an instance of your struct using break, like

break myfile.c:9

Then just use watch to set a watchpoint, like

watch myStructInstance.a

for variable a or

watch *0x7ffff75177f0

for a memory address. The memory address can be obtained easily by using print, like

print &myStructInstance.a

Now every time variable a or the given memory address gets modified gdb will break.

Problem

I have a structure like this : ``` struct A { int a; char b; }; ``` this structure is referenced at various places in a large code. The pointer to this struct is passed on to different functions and accordingly the variables in this structure are updated. i want to set a watchpoint on variable a in this struct as it travels across many functions. to see how a changes. How do I set this watch point ?

Original source