SIGSEGV异常时打印函数调用链

C语言写的程序跑飞了,怎样打印出函数调用链呢?


linux_dev_framework软件包中的trace_exception_test.c就是一个实现演示样例。

该程序有益产生一个内存訪问异常,然后打出调用链。

程序源代码:

/* 
 * 本软件为免费、开源软件。
 * 本软件的版权(包含源代码及二进制公布版本号)归一切公众全部。

 * 您能够自由使用、传播本软件。  * 您也能够以不论什么形式、不论什么目的使用本软件(包含源代码及二进制公布版本号),而不受不论什么版权限制。  * =====================  * 作者: 孙明保  * 邮箱: sunmingbao@126.com  */ #include <stdio.h> #include "debug.h" #include "trace_exception.h" //以下的代码有益产生一个内存訪问异常 static int a; int __attribute__((noinline)) gen_SIGSEGV(int *bad_pointer) {     DBG_PRINT("==");     a = *bad_pointer;     return a; } int __attribute__((noinline)) SIGSEGV_test(int para) {     DBG_PRINT("==");     return gen_SIGSEGV(NULL)+para; } int __attribute__((noinline)) main(int argc, char *argv[]) {     DBG_PRINT("==");     trace_exception_init();     SIGSEGV_test(555);     return 0; }


执行效果:
[root@localhost ~]# cd linux-dev-framework
[root@localhost ~]# make
...
[root@localhost linux-dev-framework]# ./target/trace_exception_test.exe 


***********************
*******EXCEPTION*******
***********************
[program]:/root/linux_dev_framework/target/trace_exception_test.exe
received signal 11 (SIGSEGV).
The process will exit.

pc=0x080497bd access invalid mem addr=0x00000000
value of general regs:
GS      =0x00000033    FS      =0x00000000    ES      =0x0000007b    DS      =0x0000007b    
EDI     =0x00000000    ESI     =0x00000000    EBP     =0xbfd82328    ESP     =0xbfd82310    
EBX     =0x007dfff4    EDX     =0xbfd82264    ECX     =0xbfd822f8    EAX     =0x00000000    
TRAPNO  =0x0000000e    ERR     =0x00000004    EIP     =0x080497bd    CS      =0x00000073    
EFL     =0x00010292    UESP    =0xbfd82310    SS      =0x0000007b    

function call links:
0x080497bd - gen_SIGSEGV+45 (/root/linux_dev_framework/target/trace_exception_test.exe)
0x08049806 - SIGSEGV_test+54 (/root/linux_dev_framework/target/trace_exception_test.exe)
0x0804984e - main+62 (/root/linux_dev_framework/target/trace_exception_test.exe)

stack contents:
bfd82310        : e2 b9 04 08 ac b9 04 08 13 00 00 00 07 ba 04 08 : ****************
bfd82320        : 34 23 d8 bf f4 ff 7d 00 48 23 d8 bf 06 98 04 08 : 4#****}*H#******
bfd82330        : 00 00 00 00 ac b9 04 08 1b 00 00 00 fa b9 04 08 : ****************
bfd82340        : f4 ff 7d 00 00 00 00 00 68 23 d8 bf 4e 98 04 08 : **}*****h#**N***
bfd82350        : 2b 02 00 00 ac b9 04 08 22 00 00 00 f5 b9 04 08 : +*******"*******
bfd82360        : e0 b8 04 08 00 00 00 00 e8 23 d8 bf c6 fc 66 00 : *********#****f*
bfd82370        : 01 00 00 00 14 24 d8 bf 1c 24 d8 bf 00 d0 7c b7 : *****$***$****|*
bfd82380        : f4 6f 80 00 ff ff ff ff c4 5f 65 00 e0 8c 04 08 : *o*******_e*****
bfd82390        : 01 00 00 00 d0 23 d8 bf e5 57 64 00 b0 6a 65 00 : *****#***Wd**je*
bfd823a0        : 80 d3 7c b7 f4 ff 7d 00 00 00 00 00 00 00 00 00 : **|***}*********
bfd823b0        : e8 23 d8 bf 9f 3c c5 1f e0 cb 7a 62 00 00 00 00 : *#***<****zb****
bfd823c0        : 00 00 00 00 00 00 00 00 01 00 00 00 d0 96 04 08 : ****************
bfd823d0        : 00 00 00 00 f0 b9 64 00 eb fb 66 00 c4 5f 65 00 : ******d***f**_e*
bfd823e0        : 01 00 00 00 d0 96 04 08 00 00 00 00 f1 96 04 08 : ****************
bfd823f0        : 10 98 04 08 01 00 00 00 14 24 d8 bf e0 b8 04 08 : *********$******
bfd82400        : d0 b8 04 08 20 62 64 00 0c 24 d8 bf 00 00 00 00 : **** bd**$******
bfd82410        : 01 00 00 00 e5 27 d8 bf 00 00 00 00 07 28 d8 bf : *****'*******(**
bfd82420        : 20 28 d8 bf 3f 28 d8 bf 4f 28 d8 bf 5a 28 d8 bf :  (**?

(**O(**Z(** bfd82430 : 68 28 d8 bf 7e 28 d8 bf 8b 28 d8 bf a9 28 d8 bf : h(**~(***(***(** bfd82440 : b3 28 d8 bf b2 2d d8 bf cf 2d d8 bf e9 2d d8 bf : *(***-***-***-** bfd82450 : a4 2e d8 bf b5 2e d8 bf d3 2e d8 bf e4 2e d8 bf : *.***.***.***.** bfd82460 : ef 2e d8 bf 22 2f d8 bf 39 2f d8 bf 41 2f d8 bf : *.**"/**9/**A/** bfd82470 : 4c 2f d8 bf 59 2f d8 bf 73 2f d8 bf 7f 2f d8 bf : L/**Y/**s/***/** bfd82480 : a1 2f d8 bf b6 2f d8 bf 00 00 00 00 20 00 00 00 : */***/****** *** bfd82490 : 14 b4 35 00 21 00 00 00 00 b0 35 00 10 00 00 00 : **5*!*****5***** bfd824a0 : ff fb eb 0f 06 00 00 00 00 10 00 00 11 00 00 00 : **************** bfd824b0 : 64 00 00 00 03 00 00 00 34 80 04 08 04 00 00 00 : d*******4******* bfd824c0 : 20 00 00 00 05 00 00 00 08 00 00 00 07 00 00 00 : *************** bfd824d0 : 00 00 00 00 08 00 00 00 00 00 00 00 09 00 00 00 : **************** bfd824e0 : d0 96 04 08 0b 00 00 00 00 00 00 00 0c 00 00 00 : **************** bfd824f0 : 00 00 00 00 0d 00 00 00 00 00 00 00 0e 00 00 00 : **************** bfd82500 : 00 00 00 00 17 00 00 00 00 00 00 00 19 00 00 00 : ****************


本程序的完整源代码在linux-dev-framework源代码包中。


linux-dev-framework源代码包的介绍:
http://blog.csdn.net/crazycoder8848/article/details/22491525

原文地址:https://www.cnblogs.com/cynchanpin/p/7297568.html