IOS Crash ,就兩種狀況:一種是異常,另外一種是中斷[信號量]。服務器
#include <libkern/OSAtomic.h>ci
#include <execinfo.h>rem
// 系統信號截獲處理方法string
void signalHandler(int signal);it
// 異常截獲處理方法io
void exceptionHandler(NSException *exception);table
const int32_t _uncaughtExceptionMaximum = 10;exception
void signalHandler(int signal)方法
{ im
volatile int32_t _uncaughtExceptionCount = 0;
int32_t exceptionCount = OSAtomicIncrement32(&_uncaughtExceptionCount);
if (exceptionCount > _uncaughtExceptionMaximum) // 若是太多不用處理
{
return;
}
// 獲取信息
NSMutableDictionary *userInfo =
[NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey];
NSArray *callStack = [ExceptionHandler backtrace];
[userInfo setObject:callStack forKey:SingalExceptionHandlerAddressesKey];
// 如今就能夠上報信息到服務器
}
void exceptionHandler(NSException *exception)
{
volatile int32_t _uncaughtExceptionCount = 0;
int32_t exceptionCount = OSAtomicIncrement32(&_uncaughtExceptionCount);
if (exceptionCount > _uncaughtExceptionMaximum) // 若是太多不用處理
{
return;
}
NSArray *callStack = [ExceptionHandler backtrace];
NSMutableDictionary *userInfo =[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo setObject:callStack forKey:ExceptionHandlerAddressesKey];
// 如今就能夠上報信息到服務器
}
@implementation ExceptionHandler
//獲取調用堆棧
+ (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;
}
// 註冊崩潰攔截
-(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