問題描述
我正在通過 gdb 運行一個應用程序,我想在任何時候訪問/更改特定變量時設置斷點.有沒有好的方法可以做到這一點?我也對在 C/C++ 中監視變量的其他方法感興趣,以查看它是否/何時發生變化.
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.
推薦答案
watch 只在寫入時中斷,rwatch 讓你在讀取時中斷,awatch 讓你中斷讀/寫.
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:
gdb$ rwatch *0xfeedface
Hardware read watchpoint 2: *0xfeedface
但有一個限制適用于 rwatch 和 awatch 命令;你不能使用 gdb 變量在表達式中:
but one limitation applies to the rwatch and awatch commands; you can't use gdb variables in expressions:
gdb$ rwatch $ebx+0xec1a04f
Expression cannot be implemented with read/access watchpoint.
所以你必須自己擴展它們:
So you have to expand them yourself:
gdb$ print $ebx
$13 = 0x135700
gdb$ rwatch *0x135700+0xec1a04f
Hardware read watchpoint 3: *0x135700 + 0xec1a04f
gdb$ c
Hardware read watchpoint 3: *0x135700 + 0xec1a04f
Value = 0xec34daf
0x9527d6e7 in objc_msgSend ()
哦,順便說一下.您需要硬件或軟件支持.軟件顯然要慢得多.要了解您的操作系統是否支持硬件觀察點,您可以查看 can-use-hw-watchpoints 環境設置.
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.
gdb$ show can-use-hw-watchpoints
Debugger's willingness to use watchpoint hardware is 1.
這篇關于我可以在 GDB 中的“內存訪問"上設置斷點嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!