是否可以通过编程方式设置 gdb 观察点?
我想在我的 C++ 程序中临时设置一个观察点(硬件写入中断)以查找内存损坏。
我已经看到了通过 gdb 手动执行此操作的所有方法,但我想通过代码中的某种方法实际设置观察点,这样我就不必闯入 gdb,找出地址,设置观察点,然后继续。
像这样的东西:
#define SET_WATCHPOINT(addr) asm ("set break on hardware write %addr")
I want to set a watchpoint (break on hardware write) temporarily in my C++ program to find memory corruption.
I've seen all the ways to do it manually through gdb, but I would like to actually set the watchpoint via some method in my code so I don't have to break into gdb, find out the address, set the watchpoint and then continue.
Something like:
#define SET_WATCHPOINT(addr) asm ("set break on hardware write %addr")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从子进程设置硬件观察点。
Set hardware watchpoint from child process.
基于 user512106 的精彩回答,我编写了一个可能对某人有用的小“库”:
它位于 github 上 https:/ /github.com/whh8b/hwbp_lib。我希望我可以直接评论他的答案,但我还没有足够的代表。
根据社区的反馈,我将在此处复制/粘贴相关代码:
我希望这对某人有帮助!
将要
Based on user512106's great answer, I coded up a little "library" that someone might find useful:
It's on github at https://github.com/whh8b/hwbp_lib. I wish I could have commented directly on his answer, but I don't have enough rep yet.
Based on feedback from the community, I am going to copy/paste the relevant code here:
I hope that this helps someone!
Will
在GDB中,有两种类型的观察点:硬件和软件。
编辑:
我仍在尝试了解什么是硬件观察点。您还可以查看 GDB 低端文件(例如,
amd64-linux-nat.c
),但它(当然)涉及 2进程: 1/ 您想要观看的进程 2/ 轻量级调试器,通过 ptrace 连接到第一个进程,并使用:设置和处理观察点。
In GDB, there are two types of watchpoints, hardware and software.
EDIT:
I'm still trying to understand what are hardware watchpoint.You can also take a look at GDB low-end files (eg,
amd64-linux-nat.c
) but it (certainly) involves 2 processes: 1/ the one you want to watch 2/a lightweight debugger who attaches to the first one with ptrace, and uses:to set and handle the watchpoint.
程序本身可以向 GDB 提供命令。不过,您需要一个特殊的 shell 脚本来运行 GDB。
将此代码复制到名为 untee 的文件中,然后执行 chmod 755 untee
现在是 C 代码:
将其复制到名为 test.c 的文件中,使用命令 gcc test.c -O0 -g -o test 进行编译,然后执行 ./untee /tmp/dbgpipe | gdb -ex "run" ./test
这适用于我的 64 位 Ubuntu,使用 GDB 7.3(较旧的 GDB 版本可能拒绝从非终端读取命令)
The program itself can supply commands to the GDB. You'll need a special shell script to run GDB though.
Copy this code into the file named untee, and execute chmod 755 untee
And now the C code:
Copy that to the file named test.c, compile with command gcc test.c -O0 -g -o test then execute ./untee /tmp/dbgpipe | gdb -ex "run" ./test
This works on my 64-bit Ubuntu, with GDB 7.3 (older GDB versions might refuse to read commands from non-terminal)
如果您碰巧使用 Xcode,则可以通过在另一个断点上使用操作来设置您的观察点,从而达到所需的效果(自动设置观察点):
watchpoint set variable
(或者,如果您使用 GDB1,则命令如下:watch
>),1:更新版本的 Xcode 不再支持 GDB,但我相信它是仍然可以手动设置。
If you happen to be using Xcode, you can achieve the required effect (automatic setting of watchpoints) by using an action on another breakpoint to set your watchpoint:
watchpoint set variable <variablename>
(or if you're using GDB1, a command like:watch <variablename>
),1: GDB is no longer supported in more recent versions of Xcode, but I believe it is still possible to set it up manually.