I am running an application through gdb and I want to set a breakpoint for any time a specific variable is accessed / changed. Is there a good method for doing this? I would also be interested in other ways to monitor a variable in C/C++ to see if/when it changes.
相关问题
- How to compile C++ code in GDB?
- Pass custom debug information to Microsoft bot fra
- How do I identify what code is generating “ '&
- Invoke and control GDB from c++
- Don't want netbeans debugger to go to disassem
相关文章
- How to handle all signals in GDB
- How do I get to see DbgPrint output from my kernel
- Breakpoint in ASP.NET MVC Razor view will not be h
- Advanced profiling is unavailable for the selected
- Can't Inspect Variables When Debugging .NET As
- What is the difference between glibc's MALLOC_
- Embedding a program's source code into its bin
- How to execute another python script from your scr
Assuming the first answer is referring to the C-like syntax
(char *)(0x135700 +0xec1a04f)
then the answer to dorwatch *0x135700+0xec1a04f
is incorrect. The correct syntax isrwatch *(0x135700+0xec1a04f)
.The lack of
()
s there caused me a great deal of pain trying to use watchpoints myself.What you're looking for is called a watchpoint.
Usage
(gdb) watch foo
: watch the value of variablefoo
(gdb) watch *(int*)0x12345678
: watch the value pointed by an address, casted to whatever type you want(gdb) watch a*b + c/d
: watch an arbitrarily complex expression, valid in the program's native languageWatchpoints are of three kinds:
You may choose the more appropriate for your needs.
For more information, check this out.
I just tried the following:
So it seems possible, but you do appear to need some hardware support.
watch only breaks on write, rwatch let you break on read, and awatch let you break on read/write.
You can set read watchpoints on memory locations:
but one limitation applies to the rwatch and awatch commands; you can't use gdb variables in expressions:
So you have to expand them yourself:
Edit: Oh, and by the way. You need either hardware or software support. Software is obviously much slower. To find out if your OS supports hardware watchpoints you can see the can-use-hw-watchpoints environment setting.
Use watch to see when a variable is written to, rwatch when it is read and awatch when it is read/written from/to, as noted above. However, please note that to use this command, you must break the program, and the variable must be in scope when you've broken the program: