Linux下的C++程序崩溃时打印崩溃信息

概述

在某些极端情况下,原本正常执行的程序发生了崩溃。这时候想通过调试是很难发现出错的地方的,所以在崩溃时打印出错点的调用堆栈是十分有必要的。

使用的命令:catchsegv program [args]
例如:我们的程序是test,则可在控制台输入:catchsegv ./test
如果使用gcc/g++编译test时添加了-g选项(即在可执行程序中加入调试信息),则可以输出崩溃的代码行数。

测试

有如下C++代码(test.cpp):

int main()
{
    int* nInt = 0;
    *nInt = 0; // crash
    return 0;
}

使用g++ test.cpp -o test -g编译此段C++代码,生成test可执行程序。在控制台执行catchsegv ./test,则有如下输出(注意Backtrace:下的):

Segmentation fault (core dumped)
*** Segmentation fault
Register dump:

 RAX: 0000000000000000   RBX: 0000000000000000   RCX: 0000000000000000
 RDX: 00007fff296866f8   RSI: 00007fff296866e8   RDI: 0000000000000001
 RBP: 00007fff29686600   R8 : 00007f0072412e80   R9 : 00007f007262d700
 R10: 00007fff29686490   R11: 00007f0072070e50   R12: 0000000000400400
 R13: 00007fff296866e0   R14: 0000000000000000   R15: 0000000000000000
 RSP: 00007fff29686600

 RIP: 00000000004004fd   EFLAGS: 00010246

 CS: 0033   FS: 0000   GS: 0000

 Trap: 0000000e   Error: 00000006   OldMask: 00000000   CR2: 00000000

 FPUCW: 0000037f   FPUSW: 00000000   TAG: 00000000
 RIP: 00000000   RDP: 00000000

 ST(0) 0000 0000000000000000   ST(1) 0000 0000000000000000
 ST(2) 0000 0000000000000000   ST(3) 0000 0000000000000000
 ST(4) 0000 0000000000000000   ST(5) 0000 0000000000000000
 ST(6) 0000 0000000000000000   ST(7) 0000 0000000000000000
 mxcsr: 1f80
 XMM0:  00000000000000000000000000000000 XMM1:  00000000000000000000000000000000
 XMM2:  00000000000000000000000000000000 XMM3:  00000000000000000000000000000000
 XMM4:  00000000000000000000000000000000 XMM5:  00000000000000000000000000000000
 XMM6:  00000000000000000000000000000000 XMM7:  00000000000000000000000000000000
 XMM8:  00000000000000000000000000000000 XMM9:  00000000000000000000000000000000
 XMM10: 00000000000000000000000000000000 XMM11: 00000000000000000000000000000000
 XMM12: 00000000000000000000000000000000 XMM13: 00000000000000000000000000000000
 XMM14: 00000000000000000000000000000000 XMM15: 00000000000000000000000000000000

Backtrace:
/home/jackripper/Desktop/Test.cpp:7(main)[0x4004fd]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7f0072070f45]
??:?(_start)[0x400429]

Memory map:

00400000-00401000 r-xp 00000000 08:01 4723165 /home/jackripper/Desktop/Test
00600000-00601000 r--p 00000000 08:01 4723165 /home/jackripper/Desktop/Test
00601000-00602000 rw-p 00001000 08:01 4723165 /home/jackripper/Desktop/Test
01ab6000-01adb000 rw-p 00000000 00:00 0 [heap]
7f0071e39000-7f0071e4f000 r-xp 00000000 08:01 1445862 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0071e4f000-7f007204e000 ---p 00016000 08:01 1445862 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f007204e000-7f007204f000 rw-p 00015000 08:01 1445862 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f007204f000-7f007220d000 r-xp 00000000 08:01 1443885 /lib/x86_64-linux-gnu/libc-2.19.so
7f007220d000-7f007240d000 ---p 001be000 08:01 1443885 /lib/x86_64-linux-gnu/libc-2.19.so
7f007240d000-7f0072411000 r--p 001be000 08:01 1443885 /lib/x86_64-linux-gnu/libc-2.19.so
7f0072411000-7f0072413000 rw-p 001c2000 08:01 1443885 /lib/x86_64-linux-gnu/libc-2.19.so
7f0072413000-7f0072418000 rw-p 00000000 00:00 0
7f0072418000-7f007241c000 r-xp 00000000 08:01 1443877 /lib/x86_64-linux-gnu/libSegFault.so
7f007241c000-7f007261b000 ---p 00004000 08:01 1443877 /lib/x86_64-linux-gnu/libSegFault.so
7f007261b000-7f007261c000 r--p 00003000 08:01 1443877 /lib/x86_64-linux-gnu/libSegFault.so
7f007261c000-7f007261d000 rw-p 00004000 08:01 1443877 /lib/x86_64-linux-gnu/libSegFault.so
7f007261d000-7f0072640000 r-xp 00000000 08:01 1443901 /lib/x86_64-linux-gnu/ld-2.19.so
7f0072818000-7f007281b000 rw-p 00000000 00:00 0
7f007283d000-7f007283f000 rw-p 00000000 00:00 0
7f007283f000-7f0072840000 r--p 00022000 08:01 1443901 /lib/x86_64-linux-gnu/ld-2.19.so
7f0072840000-7f0072841000 rw-p 00023000 08:01 1443901 /lib/x86_64-linux-gnu/ld-2.19.so
7f0072841000-7f0072842000 rw-p 00000000 00:00 0
7fff29668000-7fff29689000 rw-p 00000000 00:00 0 [stack]
7fff29797000-7fff29799000 r--p 00000000 00:00 0 [vvar]
7fff29799000-7fff2979b000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值