gdbwatchpoint

How are GDB Hardware Watchpoints working?


I am interested in how a hardware watchpoint is realized. If I watch for a uint8_t variable by watch varName (watch -> write watch) so in case any data is changed on any bit within the range of the memory location it will be detected?

And how is the memory location/range handled by GDB?


Solution

  • I am interested in how a hardware watchpoint is realized.

    On platforms which support hardware watchpoints, GDB uses the processor features which enable them (duh!).

    For example, on x86, there are special debug registers, which can be programmed to have the processor halt execution when the address bus matches the given address and the access is a write (used for watchpoints), a read (used to implement access watchpoints), etc.

    If I watch for a uint8_t variable ... any data is changed on any bit within the range of the memory location it will be detected?

    The x86 processors do not write single bits into memory. The least you can write is a byte. The processor will stop after writing the byte (if the debug registers are so configured). GDB can then compare the new value with the old one, and if they differ, stop execution (otherwise GDB will swallow the watchpoint and continue as of nothing happened).

    Yes, this can work on single-bit changes -- all it requires is for GDB to remember the old value (which it does when you set up the breakpoint).

    Note: there are some gaps in this -- if the value is changed by the kernel (e.g. as a result of read system call), GDB watchpoint will not stop when that happens.