Monitoring Variable Access in GDB
Breaking on variable access is a useful technique for debugging complex programs. Several watchpoint commands in GDB allow you to monitor variables for reads or writes.
watch breaks on writes, rwatch breaks on reads, and awatch breaks on read/write operations.
Memory Access Watchpoints:
You can set read watchpoints on memory locations using rwatch. The following command sets a read watchpoint on the address 0xfeedface:
gdb$ rwatch *0xfeedface
However, you cannot use GDB variables in expressions for rwatch and awatch. If you attempt to do so, you'll receive an error message. Instead, expand the expressions manually:
gdb$ print $ebx = 0x135700 gdb$ rwatch *0x135700+0xec1a04f
Hardware and Software Support:
Watchpoint support depends on either hardware or software support. Hardware watchpoints are much faster. To check if your OS supports hardware watchpoints, use the show can-use-hw-watchpoints command:
gdb$ show can-use-hw-watchpoints Debugger's willingness to use watchpoint hardware is 1.
If the output is 1, hardware watchpoints are enabled.
The above is the detailed content of How Can I Monitor Variable Access in GDB Using Watchpoints?. For more information, please follow other related articles on the PHP Chinese website!