iOS crash 异常捕获

//

//  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);

 

转载于:https://www.cnblogs.com/qingjoin/p/5970215.html

资源下载链接为: https://pan.quark.cn/s/502b0f9d0e26 在进行STM32F103C8T6与HC - 06蓝牙模块、PC端以及ROS(机器人操作系统)的串口通信测试时,我们编写了以下程序。 硬件连接 将STM32F103C8T6的USART1的TX(PA9)引脚与HC - 06的RX引脚相连,同时将USART1的RX(PA10)引脚与HC - 06的TX引脚相连,以实现两者之间的串口通信。 另外,通过串口转USB模块(如CH340等)将STM32F103C8T6与PC端连接起来,方便在PC端进行通信数据的发送和接收。 程序功能 初始化USART1,设置波特率为9600,用于与HC - 06通信。同时,初始化USART2(连接串口转USB模块),波特率同样设置为9600,用于与PC端通信。 在主循环中,STM32F103C8T6不断检测USART1和USART2是否有数据接收。当从USART1(HC - 06)接收到数据时,将数据暂存到一个缓冲区中,然后通过USART2发送给PC端。反之,当从USART2(PC端)接收到数据时,也暂存到缓冲区,再通过USART1发送给HC - 06。这样就实现了STM32F103C8T6作为中间节点,将HC - 06与PC端的数据进行转发。 硬件连接 HC - 06蓝牙模块通过串口与STM32F103C8T6连接,如上所述。 程序功能(蓝牙通信部分) HC - 06在默认状态下会自动进入配对模式,等待与手机或其他蓝牙设备配对。当配对成功后,它会将从蓝牙设备接收到的数据通过串口发送给STM32F103C8T6。同时,它也会将STM32F103C8T6发送过来的数据转发给已配对的蓝牙设备。在本测试程序中,主要关注其与STM32F103C8T6之间的串口通信功能,确保数据能够正确地在两者之间传输。 硬件连接 通过串口
内容概要:本文详细介绍了一个基于两个单片机串行通信的电子密码锁项目。项目背景指出随着信息技术的发展,电子密码锁因其高可靠性、低成本等优势成为主流选择。项目采用主控和辅助两个单片机分别负责不同功能模块,并通过串行通信(如UART协议)实现数据交互。主控单片机处理密码输入验证、用户界面显示等,辅助单片机负责锁控制。系统还涉及多级安全防护、低功耗设计、友好的用户界面等特性。项目挑战包括确保通信稳定、提升密码验证安全性、优化电源管理和用户交互设计等。项目创新点在于双单片机协同工作、串行通信协议优化、多级安全防护以及低功耗设计。; 适合人群:对嵌入式系统开发有一定了解,特别是对单片机编程、串行通信协议、密码锁设计感兴趣的工程师或学生。; 使用场景及目标:①适用于家庭安防、商业办公、银行金融、智能酒店、医疗行业等需要高安全性的场所;②帮助开发者掌握双单片机协同工作的原理,提高系统的稳定性和安全性;③通过实际项目加深对串行通信协议的理解,掌握密码锁系统的软硬件设计方法。; 阅读建议:建议读者结合实际硬件设备进行实践操作,重点理解串行通信协议的设计与实现,同时关注密码验证的安全性设计和电源管理优化。此外,可以通过提供的代码示例加深对各功能模块的理解,并尝试修改和优化代码以适应不同的应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值