How can gdb be used to watch for any changes in an

2019-05-20 08:57发布

问题:

For example, can I break on any change to memory in an address range from <startaddress> to <endaddress>? How about reads and/or writes?

回答1:

On Linux/x86, GDB uses the processor debug registers to implement hardware watchpoints. Such watchpoints are fast -- the program runs at full speed, until the processor stops and signals the application when the access or write watchpoint is triggered.

But such watchpoints can only work on 1-word sized data.

Recent Valgrind versions (SVN, but no released versions) implement GDB remote protocol stub, and allow you to set read or write watchpoints over arbitrary memory via special monitor commands.

So if you are on a platform that has Valgrind, and if your application runs acceptably fast under Valgrind, then yes: you can set watchpoints on arbitrary memory regions.