如何使用SIGSEGV的信号处理程序调试程序

Bla*_*lad 8 c++ linux debugging gdb

我正在为一个应用程序编写一个插件,偶尔会抛出一个SIGSEGV.但是,应用程序捕获信号SIGSEGV.换句话说,插件是一个动态库.错误发生在我的插件和动态库中.但applcation处理sSIGSEGV并正常退出.因此,我很难调试并获得所有堆栈帧的回溯.任何的想法?

目前我使用gdb作为调试工具.

Emp*_*ian 10

GDB SIGSEGV在应用程序之前捕获.

您对Logan的回答所描述的内容毫无意义.

我怀疑真正发生的事情是应用程序创建了一个新进程,而只进入SIGSEGV了其他进程,而不是你附加GDB的进程.

如果我的猜测正确,以下命令可能会有用:

(gdb) catch fork
(gdb) catch vfork
(gdb) set follow-fork-mode child
Run Code Online (Sandbox Code Playgroud)

您可能还想编辑和扩展您的问题:

  • 你怎么知道那里一个SIGSEGV开始用?
  • 发布交互日志GDB也可能有用.


Log*_*ldo 5

即使程序捕获了 SIGSEGV,gdb 仍然应该首先获取它并给你一个调试程序的机会。你做过类似的事情吗

 handle SIGSEGV nostop
Run Code Online (Sandbox Code Playgroud)

在GDB?如果是这样,这可能就是它不停止的原因。

您确定确实发生了段错误吗?您可以使用另一个程序复制此行为,或者故意造成分段冲突吗?

例如:

$ cat sig.c
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

void handle(int n)
{
        puts("Bail");
        exit(1);
}

int main()
{
        signal(SIGSEGV, handle);
        int *pi = 0;
        *pi = 10;
        return 0;
}
$ gcc -g sig.c
$ ./a.out
Bail
$ gdb ./a.out
GNU gdb 6.6-debian
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
(gdb) run
Starting program: /home/elcapaldo/a.out

Program received signal SIGSEGV, Segmentation fault.
0x08048421 in main () at sig.c:15
15              *pi = 10;
(gdb) where
#0  0x08048421 in main () at sig.c:15
(gdb) c
Continuing.
Bail

Program exited with code 01.
(gdb) q
Run Code Online (Sandbox Code Playgroud)