从调试程序向 gdb 发送命令
是否可以从调试的程序中控制gdb?我希望有一个库可以帮助解决这个问题,最好是使用 gdb_sendcmd("bt")
这样的 API,但我可以接受通过套接字连接到本地 gdb 之类的东西。
主要用例是以编程方式添加数据断点来监视某个内存地址下一次被修改的时间。目标语言自然是C;任何适用于它的东西都可以在 C++ 和 Objective-C 中重用。
俄罗斯雇员的回答解决了我的直接问题,但我仍然想知道如何以编程方式运行 GDB 命令调试后的程序。如果我可以简单地将代码添加到项目中,而不是为 GDB 编写额外的启动命令(使用附加命令创建断点),则可能会加快调试速度。
因此,如果有一种方法可以以编程方式运行命令,我仍然很想听听;)
Is it possible to control gdb from the debugged program? I'm hoping for a library that can help with this, ideally with an API such as gdb_sendcmd("bt")
, but I can live with something like connecting to local gdb via a socket.
Primary use case is programmatically adding a data breakpoint to monitor when a certain memory address next gets modified. Target language is, naturally, C; anything applicable to it can be reused with C++ and Objective-C.
Answer from Employed Russian has solved my direct problem, but I'd still love to know how I can run GDB commands programmatically from the debugged program. It may speed up some debugging if I could simply add code to the project instead of writing extra startup commands for GDB that would create breakpoints with attached commands.
So if there is a way to run commands programmatically, I'd still love to hear it ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
是否可以从调试的程序中控制gdb?
不。如果程序可以做到这一点,并且(比如说)禁用所有断点,您将如何调试它?
主要用例是以编程方式添加数据断点来监视某个内存地址下一次被修改的时间。
这通常出现在这样的上下文中:在第 N 次调用 foo()
时,(本地或全局)变量 bar
意外被破坏,并且您想要找到罪魁祸首。
如果这确实是您的问题,请在您知道 bar
的值仍然有效的行上设置一个断点(就在它初始化之后)。将该断点上的忽略计数(使用 ignore
命令)设置为 N-1
,然后在命中断点时,在 bar
上设置观察点>。
您甚至可以将命令附加到断点:
commands 1 # assuming this was the first breakpoint
watch bar
continue
end
这样观察点就会自动附加。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
GDB导出的Python接口可以让你做很多事情。也许这样的东西会满足您的要求:(
只需在文件中写入,然后从您的
.gdbinit
文件中获取它)并且在应用程序方面:
我认为代码很简单,但正如我所言https://stackoverflow.com/a/8884512/341106中提到,并非所有内容都允许
stop
回调,GDB处于中间状态,但很多事情都会按预期进行。编辑:不用说,如果您的应用程序未使用调试符号编译,这将不起作用!
The Python interface exported by GDB allows you to do many things. Maybe something like this would fit your requirements:
(just write than in a file, and source it from your
.gdbinit
file)and on the application side:
I think the code is straight forward, but as I mentioned in https://stackoverflow.com/a/8884512/341106, not everything is allowed in the
stop
callback, GDB is in an intermediate state, but many things will work as expected.EDIT: needless to say, this won't work if you app is not compiled with debug symbols!