可以自动用gdb在SIGSEGV附加一个过程 [英] Can gdb automatically attach a process on a SIGSEGV

查看:240
本文介绍了可以自动用gdb在SIGSEGV附加一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,当执行接收到SIGSEGV一个错误的程序。

I have a faulty program that when execute receive a SIGSEGV.

我可以使用gdb的是这样的:

I can use gdb like this:

$ gdb ./prog 

不过,我想preFER了GDB从 PROG 赶上SIGSEGV,并自动将其固定。

But I would prefer that gdb catch the SIGSEGV from prog and attach it automatically.

$ ./prog
Segmentation Fault
(gdb) ...

有没有办法做到这一点?

Is there a way to do that?

感谢

推荐答案

嗯。您可以设置一个信号处理器与当前进程启动调试器。这样,你可以检查整个国家的活的。

Hmm. You can set up a signal handler to launch the debugger with the current process. That way you can inspect the whole state "live".

#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

const char *prog=0;
void fn(int signum)
{

    char buf[256]; 
    snprintf(buf,255,"ddd %s %d",prog,getpid());
    system(buf);
}
int main(int argc, char **argv)
{
    prog=argv[0];
    signal(SIGSEGV,&fn);
    int *p=0; 
    int k=*p;
}

更新:根据miedwar和Fanatic23的建议Chaged。当前Ubuntu的发行版配置为禁止非子进程的调试。见<一href=\"http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process\">http://askubuntu.com/questions/41629/after-upgrade-gdb-wont-attach-to-process为修复。

这篇关于可以自动用gdb在SIGSEGV附加一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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