使用 GDB 调测 SIGSEGV 信号

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

static void gdb_signal_handler(int sig, siginfo_t *info, void *ctx)
{
    char cmd[256];

    printf("Segmentation fault happened, starting GDB ... \n\n");

    snprintf(cmd, 256, "gdb --pid=%d -ex bt -q", getpid());
    system(cmd);

    printf("Finish debugging, now quit! \n");
    exit(-1);
}

void test_3(int *p)
{
    *p = 1;
}

void test_2(int *p)
{
    test_3(p);
}

void test_1(int *p)
{
    test_2(p);
}

int main(int argc, char *argv[])
{
    int *p = 0x12345678;

    struct sigaction action;
    sigemptyset(&action.sa_mask);   
    action.sa_sigaction = gdb_signal_handler;
    action.sa_flags = SA_SIGINFO;

    sigaction(SIGSEGV, &action, NULL);

    test_1(p);
    return 0;
}

#if 0

Segmentation fault happened, starting GDB ...

Attaching to process 79230
Reading symbols from /home/share/work/ac-code/command/std-lib/backtrace2...
(No debugging symbols found in /home/share/work/ac-code/command/std-lib/backtrace2)
Reading symbols from /lib/x86_64-linux-gnu/libpthread.so.0...
Reading symbols from /usr/lib/debug/.build-id/c6/d0d79d906d62bb768421fc6dada0d5e729f177.debug...
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...
Reading symbols from /usr/lib/debug/.build-id/87/b331c034a6458c64ce09c03939e947212e18ce.debug...
Reading symbols from /lib64/ld-linux-x86-64.so.2...
Reading symbols from /usr/lib/debug/.build-id/c9/a47e328eb2f1f41bcbcf31f9c11b9e17aa2a83.debug...
0x00007f69c4e75c3a in __GI___wait4 (pid=79231, stat_loc=stat_loc@entry=0x7ffc22ec1ce8, options=options@entry=0,
    usage=usage@entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
27      ../sysdeps/unix/sysv/linux/wait4.c: No such file or directory.
#0  0x00007f69c4e75c3a in __GI___wait4 (pid=79231, stat_loc=stat_loc@entry=0x7ffc22ec1ce8,
    options=options@entry=0, usage=usage@entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007f69c4e75bfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc@entry=0x7ffc22ec1ce8,
    options=options@entry=0) at waitpid.c:38
#2  0x00007f69c4de4f67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00005604c51e75f8 in ?? ()
#4  <signal handler called>
#5  0x00005604c51e761e in ?? ()
#6  0x00005604c51e7643 in ?? ()
#7  0x00005604c51e7662 in ?? ()
#8  0x00005604c51e76eb in ?? ()
#9  0x00007f69c4db7083 in __libc_start_main (main=0x5604c51e7665, argc=1, argv=0x7ffc22ec30a8,
    init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffc22ec3098)
    at ../csu/libc-start.c:308
#10 0x00005604c51e720e in ?? ()
(gdb)
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Killed
xiaozhi@ac:/home/share$ Starting program: /home/share/backtrace2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x000055555555561e in ?? ()
=> 0x000055555555561e:  c7 00 01 00 00 00       movl   $0x1,(%rax)


#endif

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值