iOS调试 LLDB

 
LLDB是个开源的内置于XCode的具有REPL(read-eval-print-loop)特征的Debugger,其可以安装C++或者Python插件。
 
常用调试命令:
 
1、print命令
 
print命令的简化方式有prin pri p,唯独pr不能用来作为检查,因为会和process混淆,幸运的是p被lldb实现为特指print。
实际上你会发现,lldb对于命令的简称,是头部匹配方式,只要不混淆,你可以随意简称某个命令。
例如: (lldb) p i
       (int) $3 = 0
   返回的$3是命令结果的引用名,使用$3可以进行print $3 + 7这样打印出7,当然,$3可以被用于任何其他表达式或者接收参数的命令。
 
 
2、expression命令
     expression命令可以用来修改变量的值,当然大部分情况下,使用xcode提供的可视化编辑器更方便。
     (lldb) e i = 100
     (int) $4 = 100
 
       实际上print相当于expression --,而--的意思是命令的参数终止,跟在--后面的都是命令的输入数据
       要打印一个对象,则需要使用e -O -- anObj,而e -O -- 的缩写正是我们常用的po命令:
 
 
 3、流程控制命令
 
继续:process continue, continue, c
下一步:thread step-over, next, n
进入:thread step-in, step, s
跳出:thread step-out, finish, f
 
 
 
4、断点命令
 
条件断点、条件执行、记录日志、自动继续、重复断点跳过。
 
 
 
5、在debugger中执行任意代码
(lldb) e char *$str = (char *)malloc(128)
(lldb) e (void)strcpy($str, "wxrld of warcraft")
(lldb) e $str[1] = 'o'
(char) $5 = 'o'
(lldb) p $str
(char *) $str = 0x00007fa22a70f1a0 "world of warcraft"
    
 在debugger中可以修改view的颜色、尺寸、甚至创建controller来push。
    
 expr (void)[0x7fde6c484640 setBackgroundColor:[UIColor redColor]] //根据地址0x7fde6c484640改变一个控件的背景色,找到那个控件,多用于约束有问题时,打印一堆地址而不知道是那个控件有约束问题。。。
 
 
 
6、watchpoint
 
watchpoint可以在某个变量被写入/读取时暂停程序运行:实际上可以使用watchpoint来监视任意一段内存的读写。
 
(lldb) watchpoint set variable string_weak_assign
Watchpoint created: Watchpoint 1: addr = 0x103a66428 size = 8 state = enabled type = w
    declare @ '/Users/wangweiliang/Downloads/AssociatedObjects-master/AssociatedObjects/ViewController.m:12'
    watchpoint spec = 'string_weak_assign'
    new value: 0x00007fa22c804890
(lldb) 
 
string_weak_assign值发生变化,就会命中
Watchpoint 1 hit:
old value: 0x00007fa22c804890
new value: 0x0000000000000000   // 很显然被释放了
 
 
(lldb) watchpoint set v -w read _abc4
v是variable的简写,同样的,set可以简写为s,watch可以简写为wa,而-w后面的参数是不可以简写的必须为read、write或者read_write。
 
7、image
 
    NSArray *array = @[@1, @2];
    NSLog(@"item 3: %@", array[2]);
 
抛出异常
 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
        0   CoreFoundation                      0x00000001097f6e65 __exceptionPreprocess + 165
              1   libobjc.A.dylib                     0x000000010926fdeb objc_exception_throw + 48
        2   CoreFoundation                      0x00000001096e5534 -[__NSArrayI objectAtIndex:] + 164
3   AssociatedObjects                   0x0000000108d6a5e7 -[ViewController viewDidLoad] + 855
根据地址找到崩溃代码
(lldb) image lookup --address 0x0000000108d6a5e7   
      Address: AssociatedObjects[0x00000001000015e7] (AssociatedObjects.__TEXT.__text + 855)
      Summary: AssociatedObjects`-[ViewController viewDidLoad] + 855 at ViewController.m:36  // 第36行
原文地址:https://www.cnblogs.com/10-19-92/p/5277455.html