从调试程序向 gdb 发送命令

发布于 12-29 08:35 字数 423 浏览 1 评论 0原文

是否可以从调试的程序中控制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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独2025-01-05 08:35:28

GDB导出的Python接口可以让你做很多事情。也许这样的东西会满足您的要求:(

import gdb

CMD_FCT  = "gdb_run" 
CMD_NAME = "str"

class GdbRunBreakpoint(gdb.Breakpoint):
    def __init__(self):
        gdb.Breakpoint.__init__(self, CMD_FCT, internal=1)
        self.silent = True

    def stop(self):
        cmd = gdb.parse_and_eval(CMD_NAME).string()
        gdb.execute(cmd)
        return False
GdbRunBreakpoint()

只需在文件中写入,然后从您的 .gdbinit 文件中获取它)

并且在应用程序方面:

void gdb_run(char *str) {}


int main () {
    gdb_run("where");
}

我认为代码很简单,但正如我所言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:

import gdb

CMD_FCT  = "gdb_run" 
CMD_NAME = "str"

class GdbRunBreakpoint(gdb.Breakpoint):
    def __init__(self):
        gdb.Breakpoint.__init__(self, CMD_FCT, internal=1)
        self.silent = True

    def stop(self):
        cmd = gdb.parse_and_eval(CMD_NAME).string()
        gdb.execute(cmd)
        return False
GdbRunBreakpoint()

(just write than in a file, and source it from your .gdbinit file)

and on the application side:

void gdb_run(char *str) {}


int main () {
    gdb_run("where");
}

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!

苄①跕圉湢2025-01-05 08:35:28

是否可以从调试的程序中控制gdb?

不。如果程序可以做到这一点,并且(比如说)禁用所有断点,您将如何调试它?

主要用例是以编程方式添加数据断点来监视某个内存地址下一次被修改的时间。

这通常出现在这样的上下文中:在第 N 次调用 foo() 时,(本地或全局)变量 bar 意外被破坏,并且您想要找到罪魁祸首。

如果这确实是您的问题,请在您知道 bar 的值仍然有效的行上设置一个断点(就在它初始化之后)。将该断点上的忽略计数(使用 ignore 命令)设置为 N-1,然后在命中断点时,在 bar 上设置观察点>。

您甚至可以将命令附加到断点:

commands 1   # assuming this was the first breakpoint
 watch bar
 continue
end

这样观察点就会自动附加。

Is it possible to control gdb from the debugged program?

No. If the program could do that, and (say) disabled all breakpoints, how would you debug it?

Primary use case is programmatically adding a data breakpoint to monitor when a certain memory address next gets modified.

This often comes up in a context like this: on Nth invocation of foo(), a (local or global) variable bar gets unexpectedly clobbered, and you want to find the culprit.

If that is indeed your problem, then set a breakpoint on the line where you know the value of bar is still good (just after it has been initialized). Set ignore count (using ignore command) on that breakpoint to N-1, and then, when the breakpoint is hit, set the watchpoint on bar.

You can even attach commands to the breakpoint:

commands 1   # assuming this was the first breakpoint
 watch bar
 continue
end

so the watchpoint is attached automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文