c/c++ 打印调用栈

        打印调用栈可以在程序出现死机的时候(如出现 SIGABRT、SIGSEGV等一些信号错误)是很有用的信息,有可能就不需要 core file 来协助排查问题了。通过 man backtrace 可以得到一个例子的源码:

#define SIZE 100
static void backTracePro(void) 
{ 
    int j, nptrs; 
 
    void *buffer[SIZE]; 
    char **strings; 
    nptrs = backtrace(buffer, SIZE);
    printf("****************************************\n");//这行我自己加的
    printf("backtrace() returned %d addresses\n", nptrs); 

    /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) 
     would produce similar output to the following: */ 
    strings = backtrace_symbols(buffer, nptrs); 
    if (strings == NULL) 
    { 
        perror("backtrace_symbols"); 
        exit(EXIT_FAILURE); 
    } 

    for (j = 0; j < nptrs; j++)
    {
        printf("%s\n", strings[j]); 
    }
    
    free(strings); 
}

然后可以把这个函数放在信号回调函数里,所以先需要设置一下信号处理函数,函数:int sigaction (int __sig, const struct sigaction *__restrict __act, struct sigaction *__restrict __oact),

static bool initSignalCallBack() 
{
    struct sigaction sigact;

    memset(&sigact, 0, sizeof(sigact));
    sigact.sa_handler = signalHandler;

    sigemptyset(&(sigact.sa_mask));

    int ret = sigaction(SIGABRT, &sigact, NULL);
	if(0 == ret)
	{
		ret |= sigaction(SIGSEGV, &sigact, NULL);
	}
	else
	{
		strerror(errno);
		return false;
	}
	
	if(ret)
	{
		strerror(errno);
	}
	
	return ret == 0;
}

所以可以把 backTracePro() 放到信号回调函数 signalHandler() 里,看看执行结果:

接下来用 c++filt 解析一下符号就可以看到调用到哪个函数了。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值