gdb-iOS调试

来源:互联网 发布:macd背离源码 编辑:程序博客网 时间:2024/06/05 09:19

  编程难免有bug,一些隐蔽的bug往往让人比较烦恼,初学者更是如此,俺经过亲身经历,整理了一些debug的小技巧,希望对初学者有所帮助,高手拍砖也欢迎哈

1.最常用的肯定是NSLog了,可以查看各种变量在各个状态的值,而且对于程序流程的整理也很有帮助

2.单步调试,一般鼠标悬浮在变量上就可以快速查看变量的状态,俺一般看这个变量的地址,就是0x开头的,主要是看它是否为nil,nil的地址是0x0,字符串可以看具体值,而字典和数组却只显示count,即有多少个元素或键值对,如果要查看整个数组或字典的话,此时是不行的(呵呵,方法在后面)

俺主要想说的其实是后面这几点,呵呵

3.debuger在单步调试的时候会出现,三部分,俺初学的时候只用下面的单步调试,直观快捷



3-1.后来发现:有个比较快捷的方法查看数组和字典,右上角的窗口里,有Arguments和locals(俺常用),在Arguments的self或loacls里有你想查看的数组的话,选中它,右键菜单上有printdescription toconsole,可以很方便的在console里查看数组和字典的值,当然像文件夹路径这种长字符串更不在话下了

3-2.左上角的窗口以前看上去好复杂,虽然现在看还是复杂,但俺看出了点门道,特别是在interrupt的时候,报错状态下,左上角的第一行显示????,其实左上角是倒序显示的,就是说最后执行的语句是在第一句,而已经执行的是显示在下面,有的时候,???下面有一些是显示"方法的调用",即[objectmessage],这很关键,可以找到距离报错的地点最近的地方,经过俺的实践,常常就是在那解决bug的

4.也是在报错状态,console一般会显示错误信息,常见的unregniseselector这些看多了也就熟悉了,有的时候会显示Call stack atfirstthrow这种也是倒着看,先执行的在下面显示,定位到离错误最近的地方解决bug,如:

 

Java代码  收藏代码
  1. 2010-10-16 20:14:13.633 Universiade 2011[5293:207*** Terminating app due to uncaught exception 'NSUnknownKeyException'reason: '[<MoreSecondViewController_ser 0x6d49bf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mytableView.'  
  2. *** Call stack at first throw 
  3.  
  4.     0   CoreFoundation                      0x0264db99 __exceptionPreprocess 185  
  5.     1   libobjc.A.dylib                     0x0279d40e objc_exception_throw 47  
  6.     2   CoreFoundation                      0x0264dad1 -[NSException raise] 17  
  7.     3   Foundation                          0x0008a0f3 _NSSetUsingKeyValueSetter 135  
  8.     4   Foundation                          0x0008a061 -[NSObject(NSKeyValueCoding) setValue:forKey:] 285  
  9.     5   UIKit                               0x0050870a -[UIRuntimeOutletConnectioconnect] 112  
  10.     6   CoreFoundation                      0x025c3d0f -[NSArray makeObjectsPerformSelector:] 239  
  11.     7   UIKit                               0x00507121 -[UINib instantiateWithOwner:options:] 1041  
  12.     8   UIKit                               0x00508eb5 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] 168  
  13.     9   UIKit                               0x003be95f -[UIViewController _loadViewFromNibNamed:bundle:] 70  
  14.     10  UIKit                               0x003bc675 -[UIViewController loadView] 120  
  15.     11  UIKit                               0x003bc54f -[UIViewController view] 56  
  16.     12  UIKit                               0x003ba9f4 -[UIViewController contentScrollView] 42  
  17.     13  UIKit                               0x003ca7e2 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] 48  
  18.     14  UIKit                               0x003c8ea3 -[UINavigationController _layoutViewController:] 43  
  19.     15  UIKit                               0x003ca067 -[UINavigationController _startTransition:fromViewController:toViewController:] 326  
  20.     16  UIKit                               0x003c4ccd -[UINavigationController _startDeferredTransitionIfNeeded] 266  
  21.     17  UIKit                               0x003cbd8b -[UINavigationController pushViewController:transition:forceImmediate:] 876  
  22.     18  UIKit                               0x003c4b67 -[UINavigationController pushViewController:animated:] 62  
  23.     19  Universiade 2011                    0x0002a82e -[MoreTableView tableView:didSelectRowAtIndexPath:] 1050  
  24.     20  UIKit                               0x00385a48 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] 1140  
  25.     21  UIKit                               0x0037c32e -[UITableView _userSelectRowAtIndexPath:] 219  
  26.     22  Foundation                          0x0009121a __NSFireDelayedPerform 441  
  27.     23  CoreFoundation                      0x0262ef73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ 19  
  28.     24  CoreFoundation                      0x026305b4 __CFRunLoopDoTimer 1364  
  29.     25  CoreFoundation                      0x0258cdd9 __CFRunLoopRun 1817  
  30.     26  CoreFoundation                      0x0258c350 CFRunLoopRunSpecific 208  
  31.     27  CoreFoundation                      0x0258c271 CFRunLoopRunInMode 97  
  32.     28  GraphicsServices                    0x02f2c00c GSEventRunModal 217  
  33.     29  GraphicsServices                    0x02f2c0d1 GSEventRun 115  
  34.     30  UIKit                               0x00320af2 UIApplicationMain 1160  
  35.     31  Universiade 2011                    0x00002a5c main 102  
  36.     32  Universiade 2011                    0x000029ed start 53  
  37.  
  38. terminate called after throwing an instance of 'NSException'  
  39. Program received signal:  “SIGABRT”.  
 

5.比较恼火的BAD_ACCESS,网上有个做法是设置ZomebieEnabled,确实不错,有的时候确不显示(不知道是不是人品问题..)

不显示的时候用俺刚才说的debuger左上角的定位至离错误最近的地方那个方法,一般都OK了

  吾生有涯,debug无涯,未完待续

原创粉丝点击