4
votes

gdb manual says the following.

Warning: In multi-threaded programs, software watchpoints have only limited usefulness. If gdb creates a software watchpoint, it can only watch the value of an expression in a single thread. If you are confident that the expression can only change due to the current thread's activity (and if you are also confident that no other thread can become current), then you can use software watchpoints as usual. However, gdb may not notice when a non-current thread's activity changes the expression. (Hardware watchpoints, in contrast, watch an expression in all threads.)

So, how can I use watchpoint with multiple threads using gdb, such that change to the watched variable from any thread is seen by gdb?

1

1 Answers

5
votes

In general, software watchpoints are really costly. You could use a hardware watchpoint, if you are watching a scalar data whose address is well defined, something like

  p &myvar
  $1 = (int*) 0x1234556
  watch *(int*) 0x1234556

This is relevant only for simple scalar data (single pointer, single integer, single byte...)