IOS Crash information capture tool
The ios sdk provides a ready-made function NSSetUncaughtExceptionHandler for exception handling. However, the function is very limited, and most causes of crash include memory access errors and repeated release errors, because these exceptions throw Signal, it is necessary to specifically handle Signal. The tool implementation source code is as follows:
Header file
#import
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
# Include
NSString * const handler = @ "Courier"; NSString * const SingalExceptionHandlerAddressesKey = @ "Courier"; NSString * const ExceptionHandlerAddressesKey = @ "ExceptionHandlerAddressesKey"; const int32_t _ token = 20; // System signal Interception processing method void signalHandler (int signal); // exception interception processing method void exceptionHandler (NSException * exception); void signalHandler (int signal) {volatile int32_t _ uncaughtExceptionCount = 0; int32_t predictioncount = OSAtomicIncrement32 (& _ uncaughtExceptionCount); // if there are too many requests, you do not need to process if (predictioncount> _ convert ;} // obtain information using * userInfo = [NSMutableDictionary dictionaryWithObject: [NSNumber numberWithInt: signal] forKey: Signature]; NSArray * callStack = [ExceptionHandler backtrace]; [userInfo setObject: callStack forKey: response]; // now you can save the information to the local []} void exceptionHandler (NSException * exception) {volatile int32_t _ response = 0; int32_t exceptionCount = OSAtomicIncrement32 (& _ uncaughtExceptionCount ); // if too many requests are not processed, if (exceptionCount> _ callback) {return;} NSArray * callStack = [ExceptionHandler backtrace]; NSMutableDictionary * userInfo = [NSMutableDictionary dictionaryWithDictionary: [exception userInfo] [userInfo setObject: callStack forKey: ExceptionHandlerAddressesKey]; NSLog (@ "Exception Invoked: % @", userInfo ); // now you can save the information to the local []} @ implementation ExceptionHandler // obtain the 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 stringwithuf8string: strs [I];} free (strs); return backtrace ;} // register the crash blocker + (void) installExceptionHandler {callback (& exceptionHandler); signal (SIGHUP, signalHandler); signal (SIGINT, signalHandler); signal (SIGQUIT, signalHandler ); signal (SIGABRT, signalHandler); signal (SIGILL, signalHandler); SIGSEGV (SIGSEGV, signalHandler); signal (SIGFPE, signal); signal (SIGBUS, signalHandler); signal (SIGPIPE, signalHandler);} @ end