Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hardware watchpoints - how do they work?

How do GDB watchpoints work? Can similar functionality be implemented to harness byte level access at defined locations?

like image 523
Kapil Avatar asked Oct 18 '11 10:10

Kapil


People also ask

What is a data watchpoint?

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.

How do you use watchpoints in GDB?

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.

How do you set a watch point?

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.

What does GDB's Watch Command do?

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.


2 Answers

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.

like image 180
takladev Avatar answered Sep 27 '22 22:09

takladev


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.

like image 42
Paul R Avatar answered Sep 27 '22 21:09

Paul R