Xcode调试技巧

来源:互联网 发布:杭创软件 编辑:程序博客网 时间:2024/06/11 04:51

Xcode从默认使用LLDB后,有很多实用的调试技巧,这里简单介绍一下。

输出被调用的方法/函数

Log the called method/func
1
NSLog(@"%s", __PRETTY_FUNCTION__);

监测任意的异常

  • Add ALL EXCEPTION breakpoint
  • objc_exception_throw(Add Symbolic Exception)

为任意方法添加断点

以UIView的setFrame方法为例:启动App,挂上Debugger,在任意时刻暂停App,在lldb中输入

Set breakpoint
1
(lldb) breakpoint set -n "-[UIView setFrame:]"

Sound Breakpoint

添加断点,设置action为sound,然后勾选Automatically continue after evaluating。可以简单地用以确认某些方法是否被调用了。

输出所有线程的backtrace

命中断点后,在lldb中输入:

Print Backtrace
1
(lldb) bt all

输出View结构

任意时刻暂停App,在lldb中输入:

Print View Hierachy
1
(lldb) po [[[[UIApplication sharedApplication] delegate] window] recursiveDescription]

输出NSData

Debugger中,选中NSData->View Memory,会看到一些无法阅读的数据; 在lldb中,输入:

Print NSData object to get address
1
(lldb) po yourDataObject

会得到yourDataObject的地址,用该地址替换掉Memory Viewer中的地址,会自动使用合适的encoding。

Instruments Flags

这在调试多线程应用时格外有用,因为很难保证固定的执行顺序。使用这个特性,需要添加DTPerfomanceSession.framework到Xcode project中。

下面的代码片段,将在使用Instruments时,添加一个flag:

Use DTSendSignalFlag to set flags in Instruemnts
123
#import <DTPerformanceSession/DTSignalFlag.h> // ...DTSendSignalFlag("com.invasivecode.mytracepoints.app.point", DT_POINT_SIGNAL, TRUE);

你也可以在Instruments中显示开始和结束标志:

Use DTSentSignalFlag to set Start and End Flags
123456789
#import <DTPerformanceSession/DTSignalFlag.h> // ...// Put this line at the begginingDTSendSignalFlag("com.invasivecode.mytracepoints.app.start", DT_START_SIGNAL, TRUE);// ... more code here// put this at the endDTSendSignalFlag("com.invasivecode.mytracepoints.app.end", DT_END_SIGNAL, TRUE);

Reference:

  • Presentation by Mike Hay at CocoaHeads
  • Flags: very useful when debugging with instruments
原创粉丝点击