ARM Linux BackTrace


/*
特此声明:感谢前人种树。

如果是x86平台gcc编译,可以直接编译。
如果是ARM平台arm-linux-gcc交叉编译,需要带如下编译参数
-rdynamic 
-funwind-tables 
-ffunction-sections
否则可能backtrace返回值为0,得不到需要的信息。
*/

#include <stdio.h>  
#include <stdlib.h>  
#include <stddef.h>  
#include <execinfo.h>  
#include <signal.h>  

void dump(int signo)  
{  
    void *buffer[30] = {0};  
    size_t size;  
    char **strings = NULL;  
    size_t i = 0;  

    size = backtrace(buffer, 30);  
    fprintf(stdout, "Obtained %zd stack frames.nm\n", size);  
    strings = backtrace_symbols(buffer, size);  
    if (strings == NULL)  
    {  
        perror("backtrace_symbols.");  
        exit(EXIT_FAILURE);  
    }  

    for (i = 0; i < size; i++)  
    {  
        fprintf(stdout, "%s\n", strings[i]);  
    }  
    free(strings);  
    strings = NULL;  
    exit(0);  
}  

void func_c()  
{  
    *((volatile char *)0x0) = 0x9999;  
}  

void func_b()  
{  
    func_c();  
}  

void func_a()  
{  
    func_b();  
}  

int main(int argc, const char *argv[])  
{  
    if (signal(SIGSEGV, dump) == SIG_ERR)  
        perror("can't catch SIGSEGV");  

    func_a();  
    return 0;  




评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值