linux/unix 段错误捕获【续】
睿丰德科技 专注RFID识别技术和条码识别技术与管理软件的集成项目。质量追溯系统、MES系统、金蝶与条码系统对接、用友与条码系统对接
本文为“在C/C++中捕获段错误,打印出错的具体位置”的续篇,进一步解决涉及动态链接库的情况。
背景知识:
·linux/unix下动态链接库的基本原理
·/proc/pid/maps文件的基本格式
·动态链接库:在进程执行过程中动态加载,进程间可以共享代码,可用在发布升级包等场合
概述:
用户自己编写的代码均编译进了可执行文件里的时候,“在C/C++中捕获段错误,打印出错的具体位置”里给出了在发生段错误(或其他错误,读者可以修改附件里面的头文件,增加捕获的错误类型)的情况下,输出代码执行路径的方法。本文在此基础上,分析了当用户编写的部分代码不在可执行文件中时,如何获取代码执行路径。
为简洁起见,后文用“原方法”指代前一文章内的分析方法。
正文:
先给出本文示例代码(
segvCatch_ext.rar )
命令行下执行的命令行次序如下:
[root@redhat tcpBreak]# g++ -fPIC -shared -g -o libtest.so lib.cpp
[root@redhat tcpBreak]# g++ -g test.cpp ./libtest.so [root@redhat tcpBreak]# ./a.out [root@redhat tcpBreak]# addr2line...(省略) 一、出错代码在动态链接库内时,原方法的输出 有些情况下,我们会采用动态链接库,如果出错代码行恰巧在动态链接库内,原方法仍可得到出错时的地址。例如:

[root@redhat tcpBreak]# g++ -g test.cpp ./libtest.so [root@redhat tcpBreak]# ./a.out [root@redhat tcpBreak]# addr2line...(省略) 一、出错代码在动态链接库内时,原方法的输出 有些情况下,我们会采用动态链接库,如果出错代码行恰巧在动态链接库内,原方法仍可得到出错时的地址。例如:
- signal[8] catched when running code at 8048ab3
- signal[8] catched when running code at 4001771b
- signal[8] catched when running code at 400176fd
- [root@redhat tcpBreak]# addr2line 8048ab3 4001771b 400176fd -s -C -f -e a.out
- main
- test.cpp:15
- ??
- ??:0
- ??
- ??:0
- [root@redhat tcpBreak]# addr2line 4001771b 400176fd -s -C -f -e libtest.so
- ??
- ??:0
- ??
- ??:0
- -------------------------- 进程挂掉时的MAPS文件 --------------------------
- 08048000-08049000 r-xp 00000000 00:09 17256 /mnt/hgfs/share/net/tcpBreak/a.out
- 08049000-0804a000 rw-p 00001000 00:09 17256 /mnt/hgfs/share/net/tcpBreak/a.out
- 0804a000-0804b000 rwxp 00000000 00:00 0
- 40000000-40015000 r-xp 00000000 08:02 271023 /lib/ld-2.3.2.so
- 40015000-40016000 rw-p 00014000 08:02 271023 /lib/ld-2.3.2.so
- 40016000-40017000 rw-p 00000000 00:00 0
- 40017000-40018000 r-xp 00000000 00:09 17255 /mnt/hgfs/share/net/tcpBreak/libtest.so
- 40018000-40019000 rw-p 00000000 00:09 17255 /mnt/hgfs/share/net/tcpBreak/libtest.so
- 40019000-4001b000 rw-p 00000000 00:00 0
- 40026000-400cf000 r-xp 00000000 08:02 350892 /usr/lib/libstdc++.so.5.0.3
- 400cf000-400d4000 rw-p 000a9000 08:02 350892 /usr/lib/libstdc++.so.5.0.3
- 400d4000-400d9000 rw-p 00000000 00:00 0
- 400d9000-400fa000 r-xp 00000000 08:02 286922 /lib/tls/libm-2.3.2.so
- 400fa000-400fb000 rw-p 00020000 08:02 286922 /lib/tls/libm-2.3.2.so
- 400fb000-40102000 r-xp 00000000 08:02 271272 /lib/libgcc_s-3.2.2-20030225.so.1
- 40102000-40103000 rw-p 00007000 08:02 271272 /lib/libgcc_s-3.2.2-20030225.so.1
- 40103000-40104000 rw-p 00000000 00:00 0
- 42000000-4212e000 r-xp 00000000 08:02 286920 /lib/tls/libc-2.3.2.so
- 4212e000-42131000 rw-p 0012e000 08:02 286920 /lib/tls/libc-2.3.2.so
- 42131000-42133000 rw-p 00000000 00:00 0
- bfffd000-c0000000 rwxp ffffe000 00:00 0
- -------------------------------------------------------------------------
- --------------------------- 进程挂掉时的栈帧 --------------------------
- signal[8] catched when running code at 8048ab3
- signal[8] catched when running code at 4001771b
- signal[8] catched when running code at 400176fd
- -------------------------------------------------------------------------
- [root@redhat tcpBreak]# addr2line 71b 6fd -s -C -f -e libtest.so
- a()
- lib.cpp:14
- b()
- lib.cpp:10
- [root@redhat tcpBreak]# addr2line 8048ab3 -s -C -f -e a.out
- main
- test.cpp:15
- [root@redhat tcpBreak]# addr2line 71b 6fd -s -C -f -e libtest.so
- a()
- lib.cpp:14
- b()
- lib.cpp:10