backtrace on mips

本文深入解析了GCC编译器中的-fexceptions、-funwind-tables、-fasynchronous-unwind-tables选项,并通过backt.c示例展示了如何使用这些选项进行异常处理。同时,详细介绍了backt.c的实现过程,包括段错误处理函数和异常堆栈跟踪。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

for C:

(1)正解:

mipsel-linux-gcc backt.c -g -rdynamic -fexceptions

原因:

-fexceptions

Enable exception handling. Generates extra code needed to propagate exceptions. For some targets, this implies GCC generates frame unwind information for all functions, which can produce significant data size overhead, although it does not affect execution.If you do not specify this option,GCC enables it by default for languages like C++ that normally require exception handling, and disables it for languages like C that do not normally require it. However, you may need to enable this option when compiling C code that needs to interoperate properly with exception handlers written in C++. You may also wish to disable this option if you are compiling older C++ programs that don't use exception handling. 

(2)

mipsel-linux-gcc backt.c -g -rdynamic -funwind-tables

(3)

mipsel-linux-gcc backt.c -g -rdynamic -fasynchronous-unwind-tables

for C++:

mipsel-linux-g++ backt.c -g -rdynamic

 

以下是backt.c的实现:

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
 
#include <signal.h>
#include <unistd.h>
 
void print_bt()
{
    void *array[10];
    size_t size;
    char **strings;
    size_t i;
 
    size = backtrace (array, 10);
    strings = backtrace_symbols (array, size);
 
    printf ("*****Obtained %zd stack frames.*****\n", size);
 
    for (i = 0; i < size; i++)
    printf ("%s\n", strings[i]);
 
    free (strings);
}
 
//段错误处理函数
void dump(int signo)
{
    printf("[*****Program received signal SIGSEGV, Segmentation fault.id=%d*****]\n", signo);
    print_bt();
 
    exit(0);
}
void fun3()
{
    print_bt();
}
void fun2()
{
    fun3();
}
void fun1()
{
    fun2();
}
 
int main(int argc, char *argv[])
{
    signal(SIGSEGV, &dump);
    signal(SIGABRT, &dump);
    fun1();
 
    return 0;
}


 

参考文献:

http://sourceware.org/ml/libc-ports/2006-04/msg00009.html

http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值