关闭

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

标签: c语言linuxc++
1575人阅读 评论(0) 收藏 举报
分类:

概述

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

使用的命令: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]
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11127次
    • 积分:322
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:0篇
    • 译文:1篇
    • 评论:9条
    文章分类
    最新评论