【LLDB】高级运用与深入理解

僤鯓⒐⒋嵵緔 提交于 2020-02-29 21:52:08

###LLDB获取帮助

调用help命令来列出LLDB所有的顶层命令

###LLDB高级运用

(lldb) e int $a = 2
(lldb) p $a * 19
38
(lldb) e NSArray *$array = @[ @"Saturday", @"Sunday", @"Monday" ]
(lldb) p [$array count]
3
(lldb) po [[$array objectAtIndex:0] uppercaseString]
SATURDAY
(lldb) p [[$array objectAtIndex:$a] characterAtIndex:0]
error: no known method '-characterAtIndex:'; cast the message send to the method's return type
error: 1 errors parsing expression
//lldb无法判定某一步的计算结果是什么数据类型
(lldb) p (char)[[$array objectAtIndex:$a] characterAtIndex:0]
'M'
(lldb) p/d (char)[[$array objectAtIndex:$a] characterAtIndex:0]
77

(lldb) thread step-in // The same as "step" or "s" in GDB.
(lldb) thread step-over // The same as "next" or "n" in GDB.
(lldb) thread step-out // The same as "finish" or "f" in GDB.
(lldb) thread step-inst // The same as "stepi" / "si" in GDB.
(lldb) thread step-over-inst // The same as "nexti" / "ni" in GDB.
(lldb) thread list
(lldb) thread backtrace
(lldb) thread backtrace all //查看调用栈状态
(lldb) thread return //命令需要一个参数来指明函数强制返回时的返回值。

//检查帧参数和本地变量的最简便的方式是使用frame variable命令
(lldb) frame variable
self = (SKTGraphicView *) 0x0000000100208b40
_cmd = (struct objc_selector *) 0x000000010001bae1
sender = (id) 0x00000001001264e0
selection = (NSArray *) 0x00000001001264e0
i = (NSUInteger) 0x00000001001264e0
c = (NSUInteger) 0x00000001001253b0

(lldb) frame variable self
(SKTGraphicView *) self = 0x0000000100208b40

(lldb) frame variable *self
(SKTGraphicView *) self = 0x0000000100208b40
(NSView) NSView = {
(NSResponder) NSResponder = {
...

(lldb) frame select 9
frame #9: 0x0000000100015ae3

//查看工程中使用的库
lldb) image list
[  0] 432A6EBF-B9D2-3850-BCB2-821B9E62B1E0 0x0000000100000000 /Users/**/Library/Developer/Xcode/
(lldb) image lookup --address 0x0000000100000de0//具体哪一行
(lldb) image lookup --type NSURL //如果想查看具体类型

高级运用

###自定义执行

执行任意代码
(lldb) e char *$str = (char *)malloc(128)
(lldb) e (void)strcpy($str, "wxrld of warcraft")
(lldb) e $str[1] = 'o'
(char) $0 = 'o'
(lldb) p $str
(char *) $str = 0x00007fd04a900040 "world of warcraft"
(lldb) e (void)free($str)
在debugger中可以修改view的颜色、尺寸、甚至创建controller来push。

###打印cgframe

(lldb) po self.view.frame
error: unsupported expression with unknown type
error: unsupported expression with unknown type
error: 2 errors parsing expression

(lldb) p (CGRect)[self.view frame]
(CGRect) $0 = origin=(x=0, y=0) size=(width=320, height=480)

###观察watchpoint

watchpoint可以在某个变量被写入/读取时暂停程序运行

(lldb) watchpoint set expression -- (int*)&_abc4
Watchpoint created: Watchpoint 7: addr = 0x15e36d3c size = 4 state = enabled type = w
    new value: 0x00000000
(lldb) watchpoint set v -w read _abc4
Watchpoint created: Watchpoint 8: addr = 0x15e36d3c size = 4 state = enabled type = r
    watchpoint spec = '_abc4'
    new value: 0
(lldb) watchpoint set v -w read_write _abc3
Watchpoint created: Watchpoint 9: addr = 0x15e36d38 size = 4 state = enabled type = rw
    watchpoint spec = '_abc3'
    new value: 0

###刷新UI

(lldb) po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]<uiwindow: 0x7f82b1fa8140; frame = (0 0; 320 568); gesturerecognizers = ; layer = >
   | <uiview: 0x7f82b1d01fd0; frame = (0 0; 320 568); autoresize = w+h; layer = ></uiview: 0x7f82b1d01fd0; frame = (0 0; 320 568); autoresize = w+h; layer = ></uiwindow: 0x7f82b1fa8140; frame = (0 0; 320 568); gesturerecognizers = ; layer = >

(lldb) e id $myView = (id)0x7f82b1d01fd0

(lldb) e (void)[$myView setBackgroundColor:[UIColor blueColor]]
(lldb) e (void)[CATransaction flush]//必须加

(lldb) e id $nvc = [[[UIApplication sharedApplication] keyWindow] rootViewController]
(lldb) e id $vc = [UIViewController new]
(lldb) e (void)[[$vc view] setBackgroundColor:[UIColor yellowColor]]
(lldb) e (void)[$vc setTitle:@"Yay!"]
(lldb) e (void)[$nvc pushViewContoller:$vc animated:YES]
(lldb) e (void)[CATransaction flush]

###查找按钮的 target

(lldb) po [$myButton allTargets]
{(
    )}
(lldb) po [$myButton actionsForTarget:(id)0x7fb58bd2e240 forControlEvent:0](
_handleTap:
)

###高级frame调试

// Object-C
po [[[UIWindow keyWindow] rootViewController] _printHierarchy]
// Swift
expr -l objc++ -O -- [[UIWindow keyWindow] recursiveDescription]

fr v -R self // obj print

expr -l objc++ -O -- [0x7f8d81c3c8b0 _printHierarchy]

expr -l objc++ -O -- [[[UIWindow keyWindow] rootViewController] _printHierarchy]

###LLDB 和 Python LLDB 有内建的,完整的 Python 支持。在LLDB中输入 script,会打开一个 Python REPL。你也可以输入一行 python 语句作为 script 命令 的参数,这可以运行 python 语句而不进入REPL:

(lldb) script import os
(lldb) script os.system("open http://www.objc.io/")

这样就允许你创造各种酷的命令。把下面的语句放到文件 ~/myCommands.py 中:

def caflushCommand(debugger, command, result, internal_dict): debugger.HandleCommand("e (void)[CATransaction flush]")

command script import ~/myCommands.py

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!