IOS crash分析

此处不讨论具体的如何根据.dsym文件解析crash log的方式。

什么是崩溃:

不希望出现的中断,APP收到了系统发出的unhandle signal,来源主要由系统内核,处理器,或者应用程序本身。

一、一般的崩溃

1.违反苹果的政策:启动、恢复、暂停或退出超时;

用户强制退出:

低内存退出:MemoryWarning;

2.程序中有bug

二、崩溃解析说明

1.MemoryWarning的崩溃比较特别,没有任何trace,标志性信息即某一条trace后面有“jettisoned”。

解决方法:

可用Allocations、Leaks Instruments 或VM Tracker Instrument来帮助检查。另外,解析内存警告的log时,里面每一个page代表4k byte;

2.Exception Codes

0x8badf00d:代表 watchdog timeout,主线程阻塞超时;

0xbad22222:resume太频繁?

0xdead10cc:死锁,程序在后台时占用了系统资源

0xdeadfa11:用户强制退出程序

3.Exception Type

EXC_CRASH (SIGABRT):一般是APP做了系统不支持的操作,例如给一个对象发送其没有实现的消息;

SIGABRT is a BSD signal sent by an application to itself when an NSException or obj_exception_throw is not caught.

EXC_BAD_ACCESS (SIGSEGV):一般是APP进入了一种错误状态,大部分是内存问题;

EXC_BAD_ACCESS is a Mach exception sent by the kernel to your application when you try to access memory that is not mapped for your application. If not handled at the Mach level, it will be translated into a SIGBUS or SIGSEGV BSD signal.

另外需要注意的是:找到崩溃的线程,整个很重要,不是只有主线程才会崩溃

三、异常处理方式

1.NSUncaughtExceptionHandler

NSSetUncaughtExceptionHandler(&HandleException);

  

2.拦截系统的signal,自己进行处理;

signal(SIGABRT, SignalHandler);

  有两个信号无法拦截,SIGKILL (kill -9and SIGSTOP(终端中Ctrl+C)

四、当异常出现时如何将当前应用程序继续运行:

回到主线程,将之前线程的runlooper永久的block,用我们新的run looper继续运行。有一个缺点:之前的runlooper所占有的栈信息将会一直保持(即泄露)

原文地址:https://www.cnblogs.com/dokaygang128/p/4044746.html