捕获崩溃信号 获取堆栈信息

2020-03-02  本文已影响0人  teddy8ear

void registerSignalHandler(void) {

    signal(SIGSEGV, handleSignalException);

    signal(SIGFPE, handleSignalException);

    signal(SIGBUS, handleSignalException);

    signal(SIGPIPE, handleSignalException);

    signal(SIGHUP, handleSignalException);

    signal(SIGINT, handleSignalException);

    signal(SIGQUIT, handleSignalException);

    signal(SIGABRT, handleSignalException);

    signal(SIGILL, handleSignalException);

}

void handleSignalException(int signal) {

    NSMutableString *crashString = [[NSMutableString alloc]init];

    void* callstack[128];

    int i, frames = backtrace(callstack, 128);

    char** traceChar = backtrace_symbols(callstack, frames);

    for (i = 0; i <frames; ++i) {

        [crashString appendFormat:@"%s\n", traceChar[i]];

    }

    NSLog(crashString);

}

上一篇下一篇

猜你喜欢

热点阅读