IOS crash Flash-back information capture tool class

Source: Internet
Author: User

The IOS SDK provides an out-of-the-box function Nssetuncaughtexceptionhandler for exception handling, but with very limited functionality, most of the causes of crashes, such as memory access errors, repeated releases, and so on, are not captured because these exceptions throw signal , so it has to be specially signal treated. The implementation of the tool class source code is as follows:

Header file

#import <foundation/foundation.h>extern nsstring *const uncaughtexceptionhandlersignalkey;extern NSString * Const Singalexceptionhandleraddresseskey;extern nsstring *const Exceptionhandleraddresseskey; @interface exceptionhandler:nsobject+ (void) installexceptionhandler;+ (Nsarray *) backtrace; @end

Implementation file

#import "ExceptionHandler.h" #include <libkern/OSAtomic.h> #include <execinfo.h>nsstring *const Uncaughtexceptionhandlersignalkey = @ "Uncaughtexceptionhandlersignalkey"; NSString *const Singalexceptionhandleraddresseskey = @ "Singalexceptionhandleraddresseskey"; NSString *const Exceptionhandleraddresseskey = @ "Exceptionhandleraddresseskey"; Const int32_t _ Uncaughtexceptionmaximum = 20;//System signal interception processing method void Signalhandler (int signal);//exception intercept processing method void Exceptionhandler (    NSException *exception); void Signalhandler (int signal) {volatile int32_t _uncaughtexceptioncount = 0;    int32_t Exceptioncount = OSAtomicIncrement32 (&_uncaughtexceptioncount);    If too many do not have to deal with if (Exceptioncount > _uncaughtexceptionmaximum) {return; }//Get information nsmutabledictionary *userinfo = [Nsmutabledictionary dictionarywithobject:[nsnumber numberWithInt:sign    AL] Forkey:uncaughtexceptionhandlersignalkey];    Nsarray *callstack = [Exceptionhandler backtrace]; [UserInfo Setobject:callstack  Forkey:singalexceptionhandleraddresseskey];    Now you can save the information to local []}void Exceptionhandler (nsexception *exception) {volatile int32_t _uncaughtexceptioncount = 0;        int32_t Exceptioncount = OSAtomicIncrement32 (&_uncaughtexceptioncount);    If too many do not have to deal with if (Exceptioncount > _uncaughtexceptionmaximum) {return;    } nsarray *callstack = [Exceptionhandler backtrace];    Nsmutabledictionary *userinfo =[nsmutabledictionary dictionarywithdictionary:[exception UserInfo]];    [UserInfo Setobject:callstack Forkey:exceptionhandleraddresseskey];        NSLog (@ "Exception invoked:%@", userInfo);    You can now save the information to local []} @implementation exceptionhandler//get call stack + (Nsarray *) backtrace{void* callstack[128];    int frames = BackTrace (callstack, 128);        Char **strs = Backtrace_symbols (callstack,frames);    Nsmutablearray *backtrace = [Nsmutablearray arraywithcapacity:frames]; for (int i=0;i<frames;i++) {[BackTrace addobject:[nsstring StringwithuTf8string:strs[i]];    } free (STRs); return backtrace;}    Register crash intercept + (void) installexceptionhandler{Nssetuncaughtexceptionhandler (&exceptionhandler);    Signal (SIGHUP, Signalhandler);    Signal (SIGINT, Signalhandler);    Signal (Sigquit, Signalhandler);    Signal (SIGABRT, Signalhandler);    Signal (Sigill, Signalhandler);    Signal (SIGSEGV, Signalhandler);    Signal (SIGFPE, Signalhandler);    Signal (Sigbus, Signalhandler); Signal (Sigpipe, signalhandler);} @end


IOS crash Flash-back information capture tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.