zoukankan      html  css  js  c++  java
  • iOS 调试 crash breakpoint EXC_BAD_ACCESS SIGABRT(转)

    在调试程序的时候,总是碰到crash的bug,而且一追踪就是一些汇编的代码,让人特别疑惑。

    一般情况下可以通过增加两天断点来解决此问题,方法介绍如下:

    基本上有错误分为以下几种类型:

    signal(SIGABRT, MySignalHandler);

    signal(SIGILL, MySignalHandler);

    signal(SIGSEGV, MySignalHandler);

    signal(SIGFPE, MySignalHandler);

    signal(SIGBUS, MySignalHandler);

    signal(SIGPIPE, MySignalHandler);

    SIGABRT和EXC_BAD_ACCESS较为特殊,算是比较好跟进。

    SIGABRT是系统报错,在memery warning之后,系统会把程序强制退出,报的就是这个错误。

    EXC_BAD_ACCESS 大多数时候是内存提前释放而引起的问题,或者访问的方法不存在引起的。

     1. Xcode内置GDB,可以使用GDB调试,调试命令:

    1.1 po 命令:为 print object 的缩写,显示对象的文本描述

    (lldb) po [$eax class]:输出异常对象的地址

    (lldb) po [$eax name]:输出这个异常的名字

    (lldb) po [$eax reason]:这个将会输出错误消息:

    (lldb)  “po $eax”:对这个对象调用“description”方法和打印出来

    “$eax”是cup的一个寄存器。在一个异常的情况下,这个寄存器将会包含一个异常对象的指针。注意:$eax只会在模拟器里面工作,假如你在设备上调试,你将需要使用”$r0″寄存器

    1.2 print 命令:有点类似于格式化输出,可以输出对象的不同信息

    比如:print (char*)[[dic description] cString]、(lldb) print (int)[label retainCount]

    1.3 info 命令:我们可以查看内存地址所在信息

    1.4 info line *内存地址:可以获取内存地址所在的代码行相关信息

    1.5 show 命令:显示 GDB 相关的信息。如:show version 显示GDB版本信息

    1.6 bt: 显示当前进程的函数调用栈的情况;"up num":查看调用的详细信息;down:返回栈列表;l:显示详细代码信息;p:输出数值。

    2. 添加全局断点(Add Exception BreakPoint):

    2.1 添加步骤:

    1. In the bottom-left corner of the breakpoints navigator, click the Add button. 
    2. Choose Add Exception Breakpoint. 
    3. Choose the type of exception from the Exception pop-up menu. 
    4. Choose the phase of the exception handling process at which you want program execution to stop. 
    5. Click Done.

    2.2 使用场景:

    程序因为SIGABRT而crash,想要定位到导致crash的行。

    然后右键单击该断点选择move breakpoint to,选择User,即可以在所有项目中起作用

    3. 添加符号断点(Add Symbolic BreakPoint):

    3.1 断点执行的时机:Symbolic breakpoints stop program execution when a specific function or method starts executing

    3.2 添加步骤:

    1. Steps In the bottom-left corner of the breakpoint navigator, click the Add button.

    2. Choose Add Symbolic Breakpoint. 
    3. Enter the symbol name in the Symbol field. 
    4. Click Done.

    3.3 使用场景:

    当想让系统在某个指定条件处中断时,设置相应的断点。

    比如:

    objc_exception_throw:在系统抛出异常处设置断点。

    -[NSException raise]:

    还可添加输入malloc_error_break的symbolic断点,以跟踪调试释放了2次的对象

    4. 设置NSZombieEnabledMallocStackLoggingNSAutoreleaseFreedObjectCheckEnabledNSDebugEnabled

    4.1 设置方法:

    1. Product->Edit Scheme...->Run...->EnvironmentVariables. 
    2. add NSZombieEnabled,set the value with YES
    3. add MallocStackLogging, set the value with YES.

    4. add NSAutoreleaseFreedObjectCheckEnabled, set the value with YES.

    5. add NSDebugEnabled, set the value with YES.

    4.2 使用场景:

    主要为了解决EXC_BAD_ACCESS问题,MallocStackLogging用来启用malloc记录(使用方式 malloc_history ${App_PID} ${Object_instance_addr})。

    4.3 需要注意的问题

    NSZombieEnabled只能在调试的时候使用,千万不要忘记在产品发布的时候去掉,因为NSZombieEnabled不会真正去释放dealloc对象的内存。

    5. 重写respondsToSelector方法

    5.1 实现方式


    #ifdef _FOR_DEBUG_  
    -(BOOL) respondsToSelector:(SEL)aSelector {  
    printf("SELECTOR: %s ", [NSStringFromSelector(aSelector) UTF8String]);  
    return [super respondsToSelector:aSelector];  
    }  
    #endif  
    5.2 使用方法:
    需要在每个object的.m或者.mm文件中加入上面代码(应该可以使用类属实现),并且在other c flags中加入-D  _FOR_DEBUG_(记住请只在Debug  Configuration下加入此标记)。这样当你程序崩溃时,Xcode的console上就会准确地记录了最后运行的object的方法。

    参考文章:

    1. Xcode GDB 调试:http://blog.csdn.net/ch_soft/article/details/7005998

    2. XCode的一些调试技巧:http://blog.csdn.net/kesalin/article/details/7222153

    3. About the Breakpoint  Navigator:http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles/about_breakpoint_navigator.html#//apple_ref/doc/uid/TP40010433-CH6-SW1

    4. 当程序崩溃的时候怎么办 part-1:http://article.ityran.com/archives/1006

    5. 当程序崩溃的时候怎么办 Part-2:http://article.ityran.com/archives/1143

    6. Memory Usage Performance Guidelines:https://developer.apple.com/library/mac/#documentation /performance/Conceptual/ManagingMemory/ManagingMemory.html#//apple_ref/doc/uid/10000160-SW1

  • 相关阅读:
    Valid Palindrome
    Linux经常使用命令(十七)
    JavaSE入门学习8:Java基础语法之方法
    Eclipse导入Maven项目出现错误:Unsupported IClasspathEntry kind=4
    HTTP ERROR 503
    演示账号激活的过程:注冊——>向指定邮箱发送邮件——>用户登录邮箱,激活账号
    Oracle update+with的使用场景
    代理模式(静态代理+动态代理)——JAVA
    BZOJ1611: [Usaco2008 Feb]Meteor Shower流星雨
    BZOJ1724: [Usaco2006 Nov]Fence Repair 切割木板
  • 原文地址:https://www.cnblogs.com/ljios/p/5088947.html
Copyright © 2011-2022 走看看