//

//  UncaughtExceptionHandler.m

//  UncaughtExceptions

//

//  Created by Matt Gallagher on 2010/05/25.

//  Copyright 2010 Matt Gallagher. All rights reserved.

//

//  Permission is given to use this source code file, free of charge, in any

//  project, commercial or otherwise, entirely at your risk, with the condition

//  that any redistribution (in part or whole) of source code must retain

//  this copyright and permission notice. Attribution in compiled projects is

//  appreciated but not required.

//

#import "YunUncaughtExceptionHandler.h"

#include <libkern/OSAtomic.h>

#include <execinfo.h>

NSString * const YunUncaughtExceptionHandlerSignalExceptionName = @"YunUncaughtExceptionHandlerSignalExceptionName";

NSString * const YunUncaughtExceptionHandlerSignalKey = @"YunUncaughtExceptionHandlerSignalKey";

NSString * const YunUncaughtExceptionHandlerAddressesKey = @"YunUncaughtExceptionHandlerAddressesKey";

volatile int32_t YunUncaughtExceptionCount = 0;

const int32_t YunUncaughtExceptionMaximum = 10;

const NSInteger YunUncaughtExceptionHandlerSkipAddressCount = 4;

const NSInteger YunUncaughtExceptionHandlerReportAddressCount = 5;

@implementation UncaughtExceptionHandler

+ (NSArray *)backtrace

{

void* callstack[128];

int frames = backtrace(callstack, 128);

char **strs = backtrace_symbols(callstack, frames);

int i;

NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];

for (

i = YunUncaughtExceptionHandlerSkipAddressCount;

i < YunUncaughtExceptionHandlerSkipAddressCount +

YunUncaughtExceptionHandlerReportAddressCount;

i++)

{

[backtrace addObject:[NSString stringWithUTF8String:strs[i]]];

}

free(strs);

return backtrace;

}

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex

{

if (anIndex == 0)

{

dismissed = YES;

}

}

- (void)validateAndSaveCriticalApplicationData

{

}

//错误日志开始发送到服务器

- (void)handleException:(NSException *)exception

{

[self validateAndSaveCriticalApplicationData];

// UIAlertView *alert =

// [[[UIAlertView alloc]

// initWithTitle:NSLocalizedString(@"Unhandled exception", nil)

// message:[NSString stringWithFormat:NSLocalizedString(

// @"You can try to continue but the application may be unstable.\n\n"

// @"Debug details follow:\n%@\n%@", nil),

// [exception reason],

// [[exception userInfo] objectForKey:YunUncaughtExceptionHandlerAddressesKey]]

// delegate:self

// cancelButtonTitle:NSLocalizedString(@"Quit", nil)

// otherButtonTitles:NSLocalizedString(@"Continue", nil), nil]

// autorelease];

// [alert show];

NSString *errorStr = [NSString stringWithFormat:NSLocalizedString(

@"error info details follow:%@%@", nil),

[exception reason],

[[exception userInfo] objectForKey:YunUncaughtExceptionHandlerAddressesKey]]

;

NSLog(@"errorStrerrorStr:%@",errorStr);

// [[NSUserDefaults standardUserDefaults]setObject:errorStr forKey:my_ExceptionLog];

if(errorStr)

{

[self sendErrorMsg];

}

//NSLog(@"error:%@",errorStr);

CFRunLoopRef runLoop = CFRunLoopGetCurrent();

CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);

while (!dismissed)

{

for (NSString *mode in (__bridge  NSArray *)allModes)

{

CFRunLoopRunInMode((__bridge CFStringRef)mode, 0.001, false);

}

}

CFRelease(allModes);

NSSetUncaughtExceptionHandler(NULL);

signal(SIGABRT, SIG_DFL);

signal(SIGILL, SIG_DFL);

signal(SIGSEGV, SIG_DFL);

signal(SIGFPE, SIG_DFL);

signal(SIGBUS, SIG_DFL);

signal(SIGPIPE, SIG_DFL);

if ([[exception name] isEqual:YunUncaughtExceptionHandlerSignalExceptionName])

{

kill(getpid(), [[[exception userInfo] objectForKey:YunUncaughtExceptionHandlerSignalKey] intValue]);

}

else

{

[exception raise];

}

}

-(void)sendErrorMsg

{

NSLog(@"exceppppppp");

// FetchQueue *fetchQueue = [[FetchQueue alloc]init];

//    [fetchQueue setGLDelegate:self.gldelegate];

//[fetchQueue SendHTTpToGLServer:my_EXCEPT sendType:@"POST"];

}

@end

void HandleException(NSException *exception)

{

int32_t exceptionCount = OSAtomicIncrement32(&YunUncaughtExceptionCount);

if (exceptionCount > YunUncaughtExceptionMaximum)

{

return;

}

NSArray *callStack = [UncaughtExceptionHandler backtrace];

NSMutableDictionary *userInfo =

[NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];

[userInfo

setObject:callStack

forKey:YunUncaughtExceptionHandlerAddressesKey];

[[[UncaughtExceptionHandler alloc] init]

performSelectorOnMainThread:@selector(handleException:)

withObject:

[NSException

exceptionWithName:[exception name]

reason:[exception reason]

userInfo:userInfo]

waitUntilDone:YES];

}

void SignalHandler(int signal)

{

int32_t exceptionCount = OSAtomicIncrement32(&YunUncaughtExceptionCount);

if (exceptionCount > YunUncaughtExceptionMaximum)

{

return;

}

NSMutableDictionary *userInfo =

[NSMutableDictionary

dictionaryWithObject:[NSNumber numberWithInt:signal]

forKey:YunUncaughtExceptionHandlerSignalKey];

NSArray *callStack = [UncaughtExceptionHandler backtrace];

[userInfo

setObject:callStack

forKey:YunUncaughtExceptionHandlerAddressesKey];

[[[UncaughtExceptionHandler alloc] init]

performSelectorOnMainThread:@selector(handleException:)

withObject:

[NSException

exceptionWithName:YunUncaughtExceptionHandlerSignalExceptionName

reason:

[NSString stringWithFormat:

NSLocalizedString(@"Signal %d was raised.", nil),

signal]

userInfo:

[NSDictionary

dictionaryWithObject:[NSNumber numberWithInt:signal]

forKey:YunUncaughtExceptionHandlerSignalKey]]

waitUntilDone:YES];

}

void YunInstallUncaughtExceptionHandler(void)

{

NSSetUncaughtExceptionHandler(&HandleException);

signal(SIGABRT, SignalHandler);

signal(SIGILL, SignalHandler);

signal(SIGSEGV, SignalHandler);

signal(SIGFPE, SignalHandler);

signal(SIGBUS, SignalHandler);

signal(SIGPIPE, SignalHandler);

}

.h文件

//

//  UncaughtExceptionHandler.h

//  UncaughtExceptions

//

//  Created by Matt Gallagher on 2010/05/25.

//  Copyright 2010 Matt Gallagher. All rights reserved.

//

//  Permission is given to use this source code file, free of charge, in any

//  project, commercial or otherwise, entirely at your risk, with the condition

//  that any redistribution (in part or whole) of source code must retain

//  this copyright and permission notice. Attribution in compiled projects is

//  appreciated but not required.

//

#import <UIKit/UIKit.h>

@interface UncaughtExceptionHandler : NSObject{

BOOL dismissed;

}

@end

void HandleException(NSException *exception);

void SignalHandler(int signal);

void YunInstallUncaughtExceptionHandler(void);

iOS crash 异常捕获的更多相关文章

  1. Android进阶——Crash异常捕获并发送到服务器

    在项目中,我们常常会遇到Crash的现象,也就是程序崩溃的时候,这个时候最常看到的就是这个界面 如果你的项目已经发布到市场上了,这样的崩溃对于开发人员是看不到的,所以我们得想方法将崩溃信息发送到服务器 ...

  2. IOS常见异常捕获

    前言:在开发APP时,我们通常都会需要捕获异常,防止应用程序突然的崩溃,防止给予用户不友好的体验.其实Objective-C的异常处理方法和JAVA的雷同,懂JAVA的朋友一看就懂.我为什么要写这篇博 ...

  3. 【IOS】异常捕获 拒绝闪退 让应用从容的崩溃 UncaughtExceptionHandler

    尽管大家都不愿意看到程序崩溃,但可能崩溃是每一个应用必须面对的现实.既然崩溃已经发生.无法阻挡了.那我们就让它崩也崩得淡定点吧. IOS SDK中提供了一个现成的函数 NSSetUncaughtExc ...

  4. iOS异常捕获

    文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...

  5. IOS Crash捕获

    IOS Crash ,就两种情况:一种是异常,另一种是中断[信号量]. #include <libkern/OSAtomic.h> #include <execinfo.h> ...

  6. 漫谈iOS Crash收集框架

    漫谈iOS Crash收集框架   Crash日志收集 为了能够第一时间发现程序问题,应用程序需要实现自己的崩溃日志收集服务,成熟的开源项目很多,如 KSCrash,plcrashreporter,C ...

  7. 第26月第22天 iOS瘦身之armv7 armv7s arm64选用 iOS crash

    1.iOS瘦身之armv7 armv7s arm64选用 机器对指令集的支持是向下兼容的,因此armv7的指令集是可以运行在iphone5S以上的,只是效率没那么高而已~ 但是由于苹果要求必须支持ar ...

  8. iOS crash 崩溃问题的追踪方法

    http://www.cnblogs.com/easonoutlook/archive/2012/12/27/2835884.html iOS crash 崩溃问题的追踪方法 在调试程序的时候,总是碰 ...

  9. iOS: Crash文件解析(一)

    iOS Crash文件的解析(一) 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断. ...

随机推荐

  1. deepdetect 用c++11写的机器学习caffe和XGBoost API 接口

    https://github.com/beniz/deepdetect DeepDetect (http://www.deepdetect.com/) is a machine learning AP ...

  2. PHP中的 extends与implements 区别 [转]

    extends 是继承某个类 继承之后可以使用父类的方法 也可以重写父类的方法 implements 是实现多个接口 接口的方法一般为空的 必须重写才能使用 extends是继承父类,只要那个类不是声 ...

  3. 基于选择重传ARQ传输协议的数据重传机制方案设计

    原文链接: http://blog.csdn.net/pinghegood/article/details/7841281  1.背景 最近在项目中,由于使用TD网络传输数据,数据掉包严重,软件组老大 ...

  4. 线状DP(石子归并)

    题意:有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动相邻的2堆石子合并,合并花费为新合成的一堆石子的数量.求将这N堆石子合并成一堆的总花费最小(或最大). dp[i][j]为从i到j的 ...

  5. noip2014-day2-t2

    题意:在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条件1 ...

  6. hadoop 集群部署ganglia 监控服务与nagios 报警服务

      1. 部署ganglia 服务   ganglia 涉及到的组件:     数据监测节点(gmond):这个部件装在需要监测的节点上,用于收集本节点的运行情况,并将这些统计信息传送到gmetad, ...

  7. 多线程JAVA篇(一)

    解析AsyncTask源码之前,首先讲述与之相关的Java线程知识: 知识点清单 1.Thread类 2.Runnable接口 3.Callable接口 4.synchronized关键字 5.vol ...

  8. Win7(32/64)VS2010配置编译GDAL环境(图文教程+亲测可用!)

    最近的一个VS2010的项目中用到了GDAL,关于GDAL这个库的说明与赞美,这里就不赘述了,下面是在VS2010中配置GDAL的详细过程. 系统说明 Win7(32位/64位),VS2010,GDA ...

  9. Pyhton开发堡垒机之paramiko模块

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: + ...

  10. Xshell_Using X11 forwarding

    FROM:http://www.netsarang.com/tutorial/xshell/1018/Using_X11_forwarding The X11 forwarding feature i ...