从 C 程序到 gdb 的命令

发布于 2024-12-04 07:29:34 字数 231 浏览 0 评论 0原文

我是 UNIX 程序的新手。我遇到过一种情况,我必须从我的 C 程序向 gdb 发出命令。我有一个 C 程序,它通过派生一个新的子进程来调用另一个 C 程序。我需要调试这个子 C 程序,因此,我使用系统命令在这个 C 程序上调用 gdb 进程。但我得到了一个我不想要的 gdb 提示符。我想从我的父 C 程序向 gdb 发出命令。有没有办法从 C 程序向 gdb 发出命令?

请回复。

多谢。

埃萨什

I am newbie to UNIX programs. I have encountered a situation wherein I have to issue commands to gdb from my C program. I have a C program which invokes another C program by forking a new child process. I need to debug this child C program and hence, I used system command to call gdb process on this C program. But I get a gdb prompt which I do not want. I want to issue commands to the gdb from my parent C program. Is there a way to issue commands to gdb from a C program ?

Please reply.

Thanks a lot.

Esash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

咋地 2024-12-11 07:29:34

如果需要调试子进程,则在 fork+exec 时不一定需要使用 GDB 调用子进程。只要知道子进程的PID,就可以使用GDB中的“attach”命令附加到正在运行的子进程。基本上,您可以像这样启动 GDB:

 $ gdb
 (gdb) attach pid-of-child

在上面,将 pid-of-child 替换为子进程的 PID,这样您就可以从交互式 GDB 中调试子进程,而无需父进程根本需要处理GDB。

If you need to debug the child process, you don't necessarily need to invoke the child with GDB when you fork+exec. As long as you have the PID of the child process, you can use the "attach" command in GDB to attach to the running child process. Basically, you would start GDB like:

 $ gdb
 (gdb) attach pid-of-child

In the above, replace pid-of-child with the PID of the child process, and there you go, you can debug the child process from interactive GDB, without the parent process needing to deal with GDB at all.

一瞬间的火花 2024-12-11 07:29:34

有多种方法可以通过编程方式“驱动”GDB。

如果你只想发出一个命令,例如找出孩子崩溃的原因,你可以这样做:

gdb --batch -ex where /path/to/child <pid-of-child>

如果有更多命令,那么你愿意放在命令行上,你可以将它们写入一个临时文件,然后要求 gdb 执行它们:

gdb --batch -x /path/to/commandfile /path/to/child <pid-of-child>

以上都不允许您执行编程(if ... then do-something-in-gdb else do-something-else-in-gdb)控制。

为此,您可能需要使用 GDB 的机器接口 (MI),或者使用嵌入式 Python 解释器

There are several ways to "drive" GDB programmatically.

If you just want to issue one command, e.g. to find out why the child crashed, you can do something like this:

gdb --batch -ex where /path/to/child <pid-of-child>

If there are more commands then you are willing to put on command line, you could write them to a temporary file, and ask gdb to execute them:

gdb --batch -x /path/to/commandfile /path/to/child <pid-of-child>

Neither of the above allows you to perform programmatic (if ... then do-something-in-gdb else do-something-else-in-gdb) control.

For that you may want to either exercise GDB's machine interface (MI), or use the embedded Python interpreter.

缱绻入梦 2024-12-11 07:29:34

还有以下 on fork gdb 选项。这将立即附加到子进程。

设置 follow-fork-mode 模式

,因此,

设置 follow-fork-mode child

Theres also the follow on fork gdb option. This will attach to the child process immediately.

set follow-fork-mode mode

so,

set follow-fork-mode child

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