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