美文网首页
LLDB(Low Lever Debug)

LLDB(Low Lever Debug)

作者: NextStepPeng | 来源:发表于2018-05-10 17:46 被阅读0次

断点

  • 设置断点(C语言)
    $breakpoint set -n XXX
    set 是子命令
    -n 是选项 是--name 的缩写!

*设置断点(OC)
$breakpoint set -n "[ViewController pauseGame:]"
设置多个
$breakpoint set -n "[ViewController pauseGame:]" -n "[ViewController pauseGame1:]"
在某个文件里面设置
$breakpoint set --file ViewController.m --selector touchesBegan:withEvent:
遍历整个项目的方法
$breakpoint set -r Game:

  • 查看断点列表
    $breakpoint list

  • 删除
    $breakpoint delete 组号

  • 禁用/启用
    $breakpoint disable 禁用
    $breakpoint enable 启用

  • 遍历整个项目中满足Game:这个字符的所有方法
    $breakpoint set -r Game:

  • 查看更多断点
    $breakpoint help 或者 help

流程控制

  • 继续执行
    $continue c
  • 单步运行,将子函数当做整体一步执行
    $n next
  • 单步运行,遇到子函数会进去
    $s

查看堆栈信息

$bt

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x000000010003a3a8 LLDB`-[ViewController pengTest4:](self=0x0000000100205180, _cmd="pengTest4:", str=@"peng") at ViewController.m:105
    frame #1: 0x000000010003a35c LLDB`-[ViewController pengTest3:](self=0x0000000100205180, _cmd="pengTest3:", str=@"peng") at ViewController.m:101
  * frame #2: 0x000000010003a2d8 LLDB`-[ViewController pengTest2:](self=0x0000000100205180, _cmd="pengTest2:", str=@"peng") at ViewController.m:96
    frame #3: 0x000000010003a254 LLDB`-[ViewController pengTest1:](self=0x0000000100205180, _cmd="pengTest1:", str=@"peng") at ViewController.m:91
    frame #4: 0x000000010003a0bc LLDB`-[ViewController touchesBegan:withEvent:](self=0x0000000100205180, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x00000001700f0f80) at ViewController.m:51
    frame #5: 0x000000019643401c UIKit`forwardTouchMethod + 336
    frame #6: 0x0000000196433eb8 UIKit`-[UIResponder touchesBegan:withEvent:] + 60
    frame #7: 0x00000001962d817c UIKit`-[UIWindow _sendTouchesForEvent:] + 1948
    frame #8: 0x00000001962d3728 UIKit`-[UIWindow sendEvent:] + 3192
    frame #9: 0x00000001962a433c UIKit`-[UIApplication sendEvent:] + 340
    frame #10: 0x0000000196a9e014 UIKit`__dispatchPreprocessedEventFromEventQueue + 2400
    frame #11: 0x0000000196a98770 UIKit`__handleEventQueue + 4268
    frame #12: 0x0000000196a98b9c UIKit`__handleHIDEventFetcherDrain + 148
    frame #13: 0x000000019012142c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
    frame #14: 0x0000000190120d9c CoreFoundation`__CFRunLoopDoSources0 + 540
    frame #15: 0x000000019011e9a8 CoreFoundation`__CFRunLoopRun + 744
    frame #16: 0x000000019004eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #17: 0x0000000191ab8074 GraphicsServices`GSEventRunModal + 100
    frame #18: 0x0000000196309058 UIKit`UIApplicationMain + 208
    frame #19: 0x000000010003a4f0 LLDB`main(argc=1, argv=0x000000016fdcbae0) at main.m:14
    frame #20: 0x000000018f05d59c libdyld.dylib`start + 4

  • 跳入堆栈对应函数
    $frame select 3
frame #3: 0x000000010003a254 LLDB`-[ViewController pengTest1:](self=0x0000000100205180, _cmd="pengTest1:", str=@"peng") at ViewController.m:91
   88   
   89   - (void)pengTest1:(NSString *)str{
   90       NSLog(@"%s",__func__);
-> 91       �[4m[�[0mself pengTest2:str];
   92   }
   93   
   94   - (void)pengTest2:(NSString *)str{
  • 进入堆栈的上一个函数
    $ up

  • 进入堆栈的下一个函数
    $ down

  • 返回堆栈调试
    $ thread return

LLDB动态调试

  • 添加数据(下面伪代码)

$p [self.models addObject:[[Person alloc] init]];

  • 添加多行代码 (换行时:同事按住control + command键)

$p Person *p5 = [[Person alloc] init];p5.name = "nextstep";p5.age = 13;[self.models addObject:p5];

内存断点

  • 设置内存断点
    $watchpoint set expression 0x000000010030ac98
$frame variable
     
     (ViewController *) self = 0x0000000100509a20
     (SEL) _cmd = "viewDidLoad"
     (Person *) p1 = 0x000000010030aaf0
     (Person *) p2 = 0x0000000000000010
     (Person *) p3 = 0x000000016fd060f8
     //查看需要设置内存地址
     $p &p1->_name
     (NSString **) $0 = 0x000000010030ac98
     
     //设置
     $watchpoint set expression 0x000000010030ac98
  • 删除内存断点
    $watchpoint delete

  • 查看内存断点
    $watchpoint list

  • 查看内存断点帮助
    $watchpoint help
    $watchpoint set help

cmmand指令(被动触发)

有时候当我们断点的时候,想让系统帮忙打印下对应信息

#设置断点
(lldb) b -[ViewController pengTest3:]
#输出
     Breakpoint 1: where = LLDB`-[ViewController pengTest3:] + 44 at ViewController.m:135, address = 0x0000000100012314

#查看断点清单
     (lldb) breakpoint list
     Current breakpoints:
     1: name = '-[ViewController pengTest3:]', locations = 1, resolved = 1, hit count = 1
     1.1: where = LLDB`-[ViewController pengTest3:] + 44 at ViewController.m:135, address = 0x0000000100012314, resolved, hit count = 1
     #设置断点指令(后续每次进入该断点 会运行对应指令)
     (lldb) breakpoint command add 1
     Enter your debugger command(s).  Type 'DONE' to end.
     > po self
     > p self.view
     > DONE
    #运行结果
 po self
<ViewController: 0x100405920>


 p self.view
(UIView *) $3 = 0x000000010020e7e0
    
  

*查看断点的指令
$breakpoint command list 1
*删除断点指令
$breakpoint command delete 1

p & po 区别

  • po - :
    Displays any returned value with formatting controlled by the type's author.
    类似 NSlog 格式化打印,自定义的打印方式
  • p - :
    Displays any returned value with LLDB's default formatting.
    expression 的缩写, 会输出对应的内存地址值

stop-hook(被动触发)

让你在每次stop的时候去执行一些命令,只对breadpoint,watchpoint
$target stop-hook add -o "frame variable"
断点一触发就会打印对应信息

Process 821 resuming
(ViewController *) self = 0x0000000100405920
(SEL) _cmd = "touchesBegan:withEvent:"
(__NSSetM *) touches = 0x0000000170246510 1 element
(UITouchesEvent *) event = 0x00000001740ef780
(UIView *) p1 = 0x000000010020e7e0

查看

$target stop-hook list

删除

$target stop-hook delete

直接取消 对应编号hook

$undisplay 2

常用命令

  • 查看使用的动态库 (列出当前可执行的和依赖的共享库
    图像)
    $ image list
  • 查看使用的动态库(在可执行的和依赖的范围内查找信息
    共享库映像)
    $image lookup -a "0x00123"
  • 查看类的详细信息
    $image lookup -t "Person"
Best match found in /Users/pengxicheng/Library/Developer/Xcode/DerivedData/TestLLDB-ducscbzedftwabgaptewzlmqgexq/Build/Products/Debug-iphoneos/TestLLDB.app/TestLLDB:
id = {0x30000002b}, name = "Person", byte-size = 24, decl = Person.h:11, compiler_type = "@interface Person : NSObject{
    int _age;
    NSString * _name;
}
@property ( getter = name,setter = setName:,readwrite,copy,nonatomic ) NSString * name;
@property ( getter = age,setter = setAge:,assign,readwrite,nonatomic ) int age;

  • p
  • b -[xxx xxx] //下断点
  • x 同 memory read 0x0000000104c0c3e0 //内存地址读写
  • register read //寄存器内容读取
  • po

参考1:
http://lldb.llvm.org/index.html
参考2:
http://www.dreamingwish.com/article/lldb-usage-a.html
参考3:
https://developer.apple.com/library/content/documentation/IDEs/Conceptual/gdb_to_lldb_transition_guide/document/lldb-basics.html#//apple_ref/doc/uid/TP40012917-CH2-SW1

相关文章

网友评论

      本文标题:LLDB(Low Lever Debug)

      本文链接:https://www.haomeiwen.com/subject/vffzrftx.html