有什么办法告诉gdb等待进程启动并附加到它? [英] Is there any way to tell gdb to wait for a process to start and attach to it?
问题描述
我有一个被另一个进程调用的进程,另一个进程又调用了此类程序,等等.这是一个较长的工具链中的子进程.
I have a process that is called by another process which is called by another process and so on ad nauseum. It's a child process in a long tool chain.
此过程崩溃了.
我想在gdb中捕获此过程以了解其崩溃的原因.但是,我唯一想到的方法是:
I would like to catch this process in gdb to understand why it's crashing. However, the only way I can think of is:
- 在命令行中启动原始父进程.
- 轮询
ps -C <name process I want to catch>
并获取PID. - 启动gdb,附加到该进程的PID.
- start the original parent process in the commandline.
- poll
ps -C <name process I want to catch>
and get the PID. - launch gdb, attached to that process's PID.
这很麻烦,但通常会完成任务.问题在于当前故障运行得非常快,而当我捕获PID并启动gdb时,它已经通过了故障点.
This is cumbersome but usually does the job. The problem is that the current failure runs very quickly, and by the time I capture the PID and launch gdb, it's already passed the failure point.
我想启动gdb而不是:
I would like to launch gdb and instead of:
(gdb) attach <pid>
我想这样做:
(gdb) attach <process name when it launches>
有什么办法吗?
我正在Linux上使用gdb 7.1
I am using gdb 7.1 on linux
推荐答案
这是我的名为gdbwait的脚本:
Here is my script called gdbwait:
#!/bin/sh
progstr=$1
progpid=`pgrep -o $progstr`
while [ "$progpid" = "" ]; do
progpid=`pgrep -o $progstr`
done
gdb -ex continue -p $progpid
用法:
gdbwait my_program
当然可以写得更好,但是Bourne shell脚本语法对我来说很痛苦,因此,如果可以,那么我就不用管它了. :) 如果新进程启动且死机太快,请在您自己的程序中添加1秒的延迟以进行调试...
Sure it can be written nicer but Bourne shell script syntax is painful for me so if it works then I leave it alone. :) If the new process launches and dies too quick, add 1 second delay in your own program for debugging ...
这篇关于有什么办法告诉gdb等待进程启动并附加到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!