iOS-LLDB 常用命令梳理(一)

iOS-LLDB 常用命令梳理(一)

一,p命令在不同场景下的使用

1.pexpression命令 的简写:常规用法打印当前输出者的类型与对应的内存地址。$0 :xcode缓存中的局部变量

(lldb) p self
(ViewController *) $0 = 0x0000000122a06720

2.p命令 :特殊用法可以进行 进制转化

(lldb) p 10 转十进制
(int) $2 = 10
(lldb) p/x 10 转十六进制
(int) $3 = 0x0000000a
(lldb) p/o 10 转八进制
(int) $4 = 012
(lldb) p/t 10 转二进制
(int) $5 = 0b00000000000000000000000000001010
(lldb)   
等。。。。

3.p命令 :修改变量值

- (IBAction)button1Thing:(id)sender {
    [self.dataSource addObject:[GoodsTool new]];
    [self text1:@"button1"];
}
-(void)text1:(NSString *)type{
    NSLog(@"%@",type);

    [self text2:type];
    self.titleM=type;
}
-(void)text2:(NSString *)type{
    [self text3:type];
    self.titleM=type;

}
-(void)text3:(NSString *)type{
    NSLog(@"%@",type);
    self.titleM=type;
}

4.p命令 :打印数组中对象属性时需要做强置类型转换。

未转化前
(lldb) p self.dataSource.lastObject
(GoodsTool *) $0 = 0x0000000282a22af0
(lldb) p $0.name
error: <user expression 1>:1:4: property 'name' not found on object of type 'id _Nullable'
$0.name
   ^
转换后
(lldb) p (GoodsTool *)self.dataSource.lastObject
(GoodsTool *) $1 = 0x0000000282a22af0
(lldb) p $1.name
(__NSCFConstantString *) $2 = 0x000000010459c008 @"shouji"

注意:通过p命令动态修改值时,当修改的为基本数据类型如int 将不起效。区别在于对象为指针,但是int是直接取得值。有本质上的区别。
5.po命令即 expression -O 的缩写:调用当前输出者的 Description 方法。

(lldb) po self
<ViewController: 0x122a06720>

二,断点添加命令及扩展

1.breakpoint set -n "-[ViewController text3:]"命令:添加断点。也可以同时添加多个:breakpoint set -n "-[ViewController text1:]" -n "-[ViewController text2:]"

(lldb) breakpoint set -n "-[ViewController text3:]"
Breakpoint 17: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818

2.br list"命令(br 是breakpoint 缩写):对方法动态加断电。

(lldb) br list
Current breakpoints:
17: name = '-[ViewController text3:]', locations = 1, resolved = 1, hit count = 0
  17.1: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818, resolved, hit count = 0 

3.br list"命令(br 是breakpoint 缩写):对方法动态加断电。

(lldb) br list
Current breakpoints:
17: name = '-[ViewController text3:]', locations = 1, resolved = 1, hit count = 0
  17.1: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818, resolved, hit count = 0 

4.br enable 17.1"br disable 17.1命令:disable使某个断点失效,enable开启某个断电17.1对应断点id。

(lldb) br enable 17.1
1 breakpoints enabled.
(lldb) br list
Current breakpoints:
17: name = '-[ViewController text3:]', locations = 1, resolved = 1, hit count = 0
  17.1: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818, resolved, hit count = 0 
(lldb) br disable 17.1
1 breakpoints disabled.
(lldb) br list
Current breakpoints:
17: name = '-[ViewController text3:]', locations = 1
  17.1: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818, unresolved, hit count = 0  Options: disabled 
(lldb) 
当使用 disabled 对应的断电后将会有一个Options: disabled

5.br delete 17.1"命令相当于br disable 17.1命令

(lldb) br list
Current breakpoints:
17: name = '-[ViewController text3:]', locations = 1, resolved = 1, hit count = 0
  17.1: where = Goods`-[ViewController text3:] + 52 at ViewController.m:48:17, address = 0x0000000104ed1818, resolved, hit count = 0 

6.br delete 17"命令:删除id 为17 这组的所有断点,不支持删除某个组中的单个断点

(lldb) br delete 17
1 breakpoints deleted; 0 breakpoint locations disabled.
(lldb) br list
Current breakpoints:
18: names = {'-[ViewController text1:]', '-[ViewController text2:]'}, locations = 2, resolved = 2, hit count = 0
  18.1: where = Goods`-[ViewController text1:] + 52 at ViewController.m:37:17, address = 0x0000000104ed1700, resolved, hit count = 0 
  18.2: where = Goods`-[ViewController text2:] + 52 at ViewController.m:43:6, address = 0x0000000104ed1798, resolved, hit count = 0 

(lldb) 

7.br delete "命令:删除所有断点

(lldb) br delete
About to delete all breakpoints, do you want to do that?: [Y/n] y
All breakpoints removed. (1 breakpoint)
(lldb) br list
No breakpoints currently set.
(lldb) 

三,断点操作

1.c命令和 continue:跳过当前的断点。

(lldb) c
Process 41940 resuming
(lldb) c
Process 41940 resuming
(lldb) c
Process 41940 resuming
2021-06-15 14:53:37.489940+0800 Goods[41940:8141509] 3333333

2.nnext命令 :源代码级单步调试,可以进入子函数内部。

3.ninexti命令 :指令级单步调试,进入汇编流程。

->  0x1043197a8 <+68>:  adrp   x2, 3
2021-06-15 15:11:47.411297+0800 Goods[42145:8155622] button1
(lldb) expression type=@"2222"
(NSTaggedPointerString *) $0 = 0xa32c96bca5103483 @"2222"
(lldb) n
(lldb) n
(lldb) n
2021-06-15 15:12:30.224760+0800 Goods[42145:8155622] 2222
(lldb) 

四,堆栈信息打印

1.bt命令:打印当前的堆栈信息 。bt 5:打印前5条。

(lldb) bt 5
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000102605508 Goods`-[ViewController button1Thing:](self=0x0000000122a06720, _cmd="button1Thing:", sender=0x00000001228099e0) at ViewController.m:25:6
    frame #1: 0x000000019e32d190 UIKitCore`-[UIApplication sendAction:to:from:forEvent:] + 100
    frame #2: 0x000000019dc63fc4 UIKitCore`-[UIControl sendAction:to:forEvent:] + 208
    frame #3: 0x000000019dc64318 UIKitCore`-[UIControl _sendActionsForEvents:withEvent:] + 356
    frame #4: 0x000000019dc62b7c UIKitCore`-[UIControl touchesEnded:withEvent:] + 536
(lldb) 

2.up命令:向上跟进指令。

(lldb) up
frame #1: 0x00000001001a57f4 Goods`-[ViewController text2:](self=0x00000001007069a0, _cmd="text2:", type=@"button1") at ViewController.m:49:5
   46  	    self.titleM=type;
   47  	}
   48  	-(void)text2:(NSString *)type{
-> 49  	    [self text3:type];
    	    ^
   50  	    self.titleM=type;
   51  	
   52  	}

3.up命令:向上跟进指令。(若果没有源码,这里显示的将是对应方法的汇编代码

(lldb) up
frame #1: 0x00000001001a57f4 Goods`-[ViewController text2:](self=0x00000001007069a0, _cmd="text2:", type=@"button1") at ViewController.m:49:5
   46  	    self.titleM=type;
   47  	}
   48  	-(void)text2:(NSString *)type{
-> 49  	    [self text3:type];
    	    ^
   50  	    self.titleM=type;
   51  	
   52  	}

4.down命令:向下跟进指令。

(lldb) down
frame #0: 0x00000001001a5854 Goods`-[ViewController text3:](self=0x00000001007069a0, _cmd="text3:", type=@"button1") at ViewController.m:54:17
   51  	
   52  	}
   53  	-(void)text3:(NSString *)type{
-> 54  	    NSLog(@"%@",type);
    	                ^
   55  	    self.titleM=type;
   56  	}
   57  	- (NSMutableArray *)dataSource{

5.frame variable命令:查看对应方法的参数。

(lldb) frame select 3
frame #3: 0x00000001001a5588 Goods`-[ViewController button1Thing:](self=0x00000001007069a0, _cmd="button1Thing:", sender=0x000000010060ccc0) at ViewController.m:31:5
   28  	}
   29  	- (IBAction)button1Thing:(id)sender {
   30  	    [self.dataSource addObject:[GoodsTool new]];
-> 31  	    [self text1:@"button1"];
    	    ^
   32  	}
   33  	- (IBAction)buttonThing2:(UIButton *)sender {
   34  	    [self.dataSource addObject:[GoodsTool new]];
(lldb) frame variable
(ViewController *) self = 0x00000001007069a0
(SEL) _cmd = "button1Thing:"
(UIButton *) sender = 0x000000010060ccc0

6.thread return命令:线程回滚命令。我们在第三个方法做的打印但是当我们 使用 thread return 命令后线程回滚,直接就rethrn 掉,不再走text2,和text3.

-(void)text1:(NSString *)type{

    [self text2:type];
    self.titleM=type;
}
-(void)text2:(NSString *)type{
    [self text3:type];
    self.titleM=type;

}
-(void)text3:(NSString *)type{
断点在这    NSLog(@"%@",type);
    self.titleM=type;
}
2021-06-15 21:34:56.226703+0800 Goods[42797:8263091] button1
(lldb) thread return
(lldb) thread return
(lldb) c
Process 42797 resuming

五,内存断点

1.watchpoint set variable good->_name 命令: good 为自定义对象,name 为属性。 观察对应的成员变量值的更改 当值发生改变时会自动断到,对应的内存中,显示更改前后的值。

->  0x1afeecfdc <+52>:  mov    x0, x20
(lldb) watchpoint set variable good->_name 
Watchpoint created: Watchpoint 1: addr = 0x280949f60 size = 8 state = enabled type = w
    declare @ '/Users/mac/Desktop/Goods 2/Goods/ViewController.m:21'
    watchpoint spec = 'good->_name'
    new value: 0x0000000104cc0008

Watchpoint 1 hit:
old value: 0x0000000104cc0008
new value: 0x0000000104cc00e8
(lldb) po 0x0000000104cc0008
shouji

(lldb) po 0x0000000104cc00e8
gaibian

(lldb) 

2.watchpoint set variable good->_name 命令: 和上面作用相同,方式不同。这是直接通过内存地址的值来监听对应的成员变量发生变化。

0x1afeecfdc <+52>:  mov    x0, x20
(lldb) p &good.name
error: <user expression 3>:1:1: address of property expression requested
&good.name
^~~~~~~~~~
(lldb) p &good->_name
(NSString **) $0 = 0x00000002818641c0
(lldb) watchpoint set expression 0x00000002818641c0
Watchpoint created: Watchpoint 1: addr = 0x2818641c0 size = 8 state = enabled type = w
    new value: 4374822920
(lldb) c
Process 42826 resuming
(lldb) c
Process 42826 resuming

Watchpoint 1 hit:
old value: 4374822920
new value: 4374823144
(lldb) po 4374822920
shouji

(lldb) po 4374823144
gaibian

3.watchpoint list 命令: 内存断点列表
4.watchpoint delete 命令: 删除所有内存断点

(lldb) watchpoint list
Number of supported hardware watchpoints: 4
Current watchpoints:
Watchpoint 1: addr = 0x2818641c0 size = 8 state = enabled type = w
    old value: 4374822920
    new value: 4374823144
(lldb) watchpoint delete
About to delete all watchpoints, do you want to do that?: [Y/n] y
All watchpoints removed. (1 watchpoints)
(lldb) watchpoint list
Number of supported hardware watchpoints: 4
No watchpoints currently set.
(lldb) 

六,给断点添加命令

1.breakpoint command add 4 命令(对应的也有delete ,list 命令): 这里的4 表示第四组断点。

(lldb) breakpoint command add 4
Enter your debugger command(s).  Type 'DONE' to end.
当输入命令回撤后将进入命令输入模式如下
> po self       打印自身
> po self.view  打印所有view视图
> DONE          以'DONE'结束

2021-06-15 22:19:03.252486+0800 Goods[42826:8280064] button1
 po self
<ViewController: 0x105409a70>
 po self.view
<UIView: 0x10540e490; frame = (0 0; 414 896); autoresize = W+H; layer = <CALayer: 0x281634440>>

2.target stop-hook add -o "frame variable" 命令(“frame variable” 可变的 “p self.view”,等: 类似于 hook 只要有断点就会打印对应的方法参数。
注意:

(lldb) target stop-hook add -o "frame variable"
Stop hook #1 added.
(lldb) target stop-hook list
Hook: 1
  State: enabled
  Commands: 
      frame variable
(lldb) c
Process 42832 resuming
2021-06-15 22:34:32.144961+0800 Goods[42832:8288061] button1
(ViewController *) self = 0x0000000105306aa0
(SEL) _cmd = "text3:"
(__NSCFConstantString *) type = 0x0000000104d800c8 @"button1"

(lldb) 

3.可以将对应的命令写到lldb 的配置文件中。
打开终端 :

~  ls -a
➜  ~  vim .lldbinit
添加:target stop-hook add -o "frame variable" 指令即可。
只要有断点就会打印对应参数
2021-06-15 22:49:29.475466+0800 Goods[42840:8293280] button1
(ViewController *) self = 0x0000000104a06f50
(SEL) _cmd = "text3:"
(__NSCFConstantString *) type = 0x000000010452c0c8 @"button1"

七,image的基本使用

1.image lookup -t GoodsTool 命令: 查看类的基本信息

- (IBAction)buttonThing2:(UIButton *)sender {
    GoodsTool *tool=self.dataSource.firstObject;
    tool.name=@"gaibian";

}
(lldb) image lookup -t GoodsTool
Best match found in /Users/mac/Library/Developer/Xcode/DerivedData/Goods-gtiwnlbbpbwommfrfdjewhdriymw/Build/Products/Debug-iphoneos/Goods.app/Goods:
id = {0x300000033}, name = "GoodsTool", byte-size = 40, decl = GoodsTool.h:12, compiler_type = "@interface GoodsTool : NSObject{
    char _c;
    int _a;
    NSString * _name;
    NSString * _num;
    double _b;
}
@property(nonatomic, readwrite, getter = name, setter = setName:) NSString *name;
@property(nonatomic, readwrite, getter = num, setter = setNum:) NSString *num;
@property(nonatomic, assign, readwrite, getter = a, setter = setA:) int a;
@property(nonatomic, assign, readwrite, getter = b, setter = setB:) double b;
@property(nonatomic, assign, readwrite, getter = c, setter = setC:) char c;
@end"

2.image list 命令: 查看加载的动态库

未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值