How do GDB watchpoints work? Can similar functionality be implemented to harness byte level access at defined locations?
Data Watchpoint and Trace (DWT) DWT module provides means for generating various hardware trace events which are user configurable like hardware access breakpoints (see Access Breakpoints for more information). This is due to the same hardware comparators used for hardware access breakpoints and trace event generation.
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.
You can set a watchpoint on a global variable by highlighting the variable in the editor, or by selecting it in the Outline view. To set a watchpoint on a global variable: Highlight the variable in the editor, or select it in the Outline view. Click Run > Toggle Watchpoint.
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.
On x86 there are CPU debug registers D0-D3 that track memory address.
This explains how hardware breakpoints are implemented in Linux and also gives details of what processor specific features are used.
Another article on hardware breakpoints.
I believe gdb uses the MMU so that the memory pages containing watched address ranges are marked as protected - then when an exception occurs for a write to a protected pages gdb handles the exception, checks to see whether the address of the write corresponds to a particular watchpoint, and then either resumes or drops to the gdb command prompt accordingly.
You can implement something similar for your own debugging code or test harness using mprotect, although you'll need to implement an exception handler if you want to do anything more sophisticated than just fail on a bad write.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With