从 C 程序到 gdb 的命令
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果需要调试子进程,则在 fork+exec 时不一定需要使用 GDB 调用子进程。只要知道子进程的PID,就可以使用GDB中的“attach”命令附加到正在运行的子进程。基本上,您可以像这样启动 GDB:
在上面,将 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:
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.
有多种方法可以通过编程方式“驱动”GDB。
如果你只想发出一个命令,例如找出孩子崩溃的原因,你可以这样做:
如果有更多命令,那么你愿意放在命令行上,你可以将它们写入一个临时文件,然后要求 gdb 执行它们:
以上都不允许您执行编程(
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:
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:
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.
还有以下 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