Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch a memory range in gdb?

I am debugging a program in gdb and I want the program to stop when the memory region 0x08049000 to 0x0804a000 is accessed. When I try to set memory breakpoints manually, gdb does not seem to support more than two locations at a time.

(gdb) awatch *0x08049000 Hardware access (read/write) watchpoint 1: *0x08049000 (gdb) awatch *0x08049001 Hardware access (read/write) watchpoint 2: *0x08049001 (gdb) awatch *0x08049002 Hardware access (read/write) watchpoint 3: *0x08049002 (gdb) run Starting program: /home/iblue/git/some-code/some-executable Warning: Could not insert hardware watchpoint 3. Could not insert hardware breakpoints: You may have requested too many hardware breakpoints/watchpoints. 

There is already a question where this has been asked and the answer was, that it may be possible to do this with valgrind. Unfortunately the answer does not contain any examples or reference to the valgrind manual, so it was not very enlightning: How can gdb be used to watch for any changes in an entire region of memory?

So: How can I watch the whole memory region?

like image 999
iblue Avatar asked Jun 12 '12 20:06

iblue


People also ask

How do I examine a memory address in GDB?

Use the x command to examine memory. n , f , and u are all optional parameters that specify how much memory to display and how to format it; addr is an expression giving the address where you want to start displaying memory.

How to use examine in GDB?

You can use the command x (for "examine") to examine memory in any of several formats, independently of your program's data types. Use the x command to examine memory.

How does watch work in GDB?

watch allows us to stop the execution every time the value of a variable changes. display prints variables every time the program's execution stops (i.e. at a watchpoint, breakpoint, etc…) Using both allows us to automatically stop at various points throughout a loop, and print all the relevant variables.

How do I set a watchpoint in GDB?

You can force GDB to use only software watchpoints with the set can-use-hw-watchpoints 0 command. With this variable set to zero, GDB will never try to use hardware watchpoints, even if the underlying system supports them.


1 Answers

If you use GDB 7.4 together with Valgrind 3.7.0, then you have unlimited "emulated" hardware watchpoints.

Start your program under Valgrind, giving the arguments --vgdb=full --vgdb-error=0 then use GDB to connect to it (target remote | vgdb). Then you can e.g. watch or awatch or rwatch a memory range by doing rwatch (char[100]) *0x5180040

See the Valgrind user manual on gdb integration for more details

like image 55
phd Avatar answered Sep 17 '22 20:09

phd