llbd
调试器可用命令总览参考链接地址:lldb可用命令
下面列出几个常用的做示范:
Help 他会列举出所有的命令。
如果你忘记了一个命令是做什么的,或者是想知道更多的话,你可以通过 help <command>来了解更多细节,
例如:help print 或者help thread
print 就是打印值(lldb实际上会做前缀匹配,所以你也可以使用prin,pri,或者p。但是不能使用pr,因为lldb不能消除和process的歧义)
打印变量,可以给print指定不同的打印格式。他们都是以print/<fmt>或者简化的p/<fmt>格式书写。
例如:(默认的格式)p 16(输出16,十进制)。 (十六进制)p/x 16( 输出0x10)。
(二进制 t代表two)p/t 16(0b00000000000000000000000000010000)
P/t(char) 16(输出0b00010000)
你也可以使用p/c打印字符。或者p/s打印以空终止的字符串(以‘\0’结尾的字符串)。
po 打印对象信息。
例如:po anObj po [array count] po btn po array
expr 可以在调试的时候动态的执行表达式。并将结果打印出来。常用于在调试过程中修改变量的值。(lldb会做前缀匹配,可以使用e代替)
例如:expr 5+2 e aString = @"aNewValue"
e [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isAleradySingle"]
bt 打印调用堆栈
加all可打印所有thread的堆栈。
查询地址(image)
image 命令可用于寻址,有多个组合命令。比较实用的用法是用于寻找栈地址对应的代码位置。 下面我写了一段代码
1 NSArray *arr=[[NSArray alloc] initWithObjects:@"1",@"2", nil nil];
2 NSLog(@"%@",arr[2]);
这段代码有明显的错误,程序运行这段代码后会抛出下面的异常:
1 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
2 *** First throw call stack:(
3 0 CoreFoundation 0x0000000101951495 __exceptionPreprocess + 165
4 1 libobjc.A.dylib 0x00000001016b099e objc_exception_throw + 43
5 2 CoreFoundation 0x0000000101909e3f -[__NSArrayI objectAtIndex:] + 175
6 3 ControlStyleDemo 0x0000000100004af8 -[RootViewController viewDidLoad] + 312
7 4 UIKit 0x000000010035359e -[UIViewController loadViewIfRequired] + 562
8 5 UIKit 0x0000000100353777 -[UIViewController view] + 29
9 6 UIKit 0x000000010029396b -[UIWindow addRootViewControllerViewIfPossible] + 58
10 7 UIKit 0x0000000100293c70 -[UIWindow _setHidden:forced:] + 282
11 8 UIKit 0x000000010029cffa -[UIWindow makeKeyAndVisible] + 51
12 9 ControlStyleDemo 0x00000001000045e0 -[AppDelegate application:didFinishLaunchingWithOptions:] + 672
13 10 UIKit 0x00000001002583d9 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 264
14 11 UIKit 0x0000000100258be1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1605
15 12 UIKit 0x000000010025ca0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
16 13 UIKit 0x000000010026dd4c -[UIApplication handleEvent:withNewEvent:] + 3189
17 14 UIKit 0x000000010026e216 -[UIApplication sendEvent:] + 79
18 15 UIKit 0x000000010025e086 _UIApplicationHandleEvent + 578
19 16 GraphicsServices 0x0000000103aca71a _PurpleEventCallback + 762
20 17 GraphicsServices 0x0000000103aca1e1 PurpleEventCallback + 35
21 18 CoreFoundation 0x00000001018d3679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
22 19 CoreFoundation 0x00000001018d344e __CFRunLoopDoSource1 + 478
23 20 CoreFoundation 0x00000001018fc903 __CFRunLoopRun + 1939
24 21 CoreFoundation 0x00000001018fbd83 CFRunLoopRunSpecific + 467
25 22 UIKit 0x000000010025c2e1 -[UIApplication _run] + 609
26 23 UIKit 0x000000010025de33 UIApplicationMain + 1010
27 24 ControlStyleDemo 0x0000000100006b73 main + 115
28 25 libdyld.dylib 0x0000000101fe95fd start + 1
29 26 ??? 0x0000000000000001 0x0 + 1
30 )
31 libc++abi.dylib: terminating with uncaught exception of type NSException
现在,我们怀疑出错的地址是0x0000000100004af8(可以根据执行文件名判断,或者最小的栈地址)。为了进一步精确定位,我们可以输入以下的命令:
image lookup -- address 0x0000000100004af8
命令执行后返回:
1 Address: ControlStyleDemo[0x0000000100004af8] (ControlStyleDemo.__TEXT.__text + 13288)
2 Summary: ControlStyleDemo`-[RootViewController viewDidLoad] + 312 at RootViewController.m:53
我们可以看到,出错的位置是RootViewController.m的第53行。