有什么办法告诉gdb等待进程启动并附加到它? [英] Is there any way to tell gdb to wait for a process to start and attach to it?

查看:244
本文介绍了有什么办法告诉gdb等待进程启动并附加到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被另一个进程调用的进程,另一个进程又调用了此类程序,等等.这是一个较长的工具链中的子进程.

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:

  1. 在命令行中启动原始父进程.
  2. 轮询ps -C <name process I want to catch>并获取PID.
  3. 启动gdb,附加到该进程的PID.
  1. start the original parent process in the commandline.
  2. poll ps -C <name process I want to catch> and get the PID.
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆