11

I'm just wondering if is it possible to set breakpoint on change of variable value (in any programming language and tool) ?

For example, I want to say: "Stop anywhere, when value of variable 'a' will be changed".

I know that there is ability to set condition breakpoint and to stop execution when a variable have some specific value, but I didn't hear about observing variable changes.

If it is not possible, why ?

ceth
  • 44,198
  • 62
  • 180
  • 289
  • At some level, if you can implement "stop when variable takes particular value", you probably do it by detecting all changes to the variable and checking whether or not it's the value required. So, yes, there do exist debuggers which can do this. – Steve Jessop Sep 24 '12 at 14:12
  • As I know there are dubuggers which can inplement "stop when variable takes particular value in THIS line of code", but I don't know if there are debuggers which can implement "stop when variable takes particular value in ANY line of code".... – ceth Sep 24 '12 at 18:04

2 Answers2

11

In my experience you can achieve this with a "memory breakpoint" or "memory watch point". For example gdb does it like this: Can I set a breakpoint on 'memory access' in GDB?

As far as I've seen with write watchpoints, the break actually triggers when a is written to, regardless of whether the new value is equal to the old value. So if by "changed" you really mean "changed" then there are fewer examples out there. Possibly even none, I'm not sure, although I don't suppose it would be technically difficult to implement change-only watchpoints, assuming that you were implementing write watchpoints.

For some languages it makes a difference what kind of variable a is. For example, in C or C++ variables can be "lifted" into registers when optimization is enabled, in which case hardware memory watchpoints on the address of the variable will not necessarily catch every change.

There's also a limitation with variables on the stack, that if your function exits but the watchpoint is still set, then it could catch access to the same address, now in use for a different variable in a different function. If your function is called again later (or recursively), it's not necessarily starting from the same stack position, and if not then your watchpoint would fail to catch access to the "same" variable at a different location.

"Stop when a particular condition is true at a particular line of code" is in my experience called a "conditional breakpoint". It generally uses a different mechanism -- the debugger will most likely put a breakpoint instruction at that line of code. Each time it triggers the debugger will check the condition and continue execution if it's false.

Community
  • 1
  • 1
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
3

Some processors support hardware breakpoints which will break when an address is read or written. For example, if I have a 4 byte variable at address 0x10005060, then I can set a hardware breakpoint like this (using windbg): ba w4 0x10005060. The processor will break if any of the 4 bytes are written. The following command instructs the processor to break when any of those 4 bytes a read or written: ba r4 0x10005060.

Marc Sherman
  • 2,303
  • 14
  • 22