IOS Crash information capture tool

Source: Internet
Author: User

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
   

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.