欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lldb调试命令一

程序员文章站 2024-03-23 21:28:10
...

1.什么是LLDB

         LLDB是英文Low Lever Debug的缩写,是XCode内置的为我们开发者提供的调试工具;方便开发者对开发问题进行快速有效的解决;

2.LLDB 断点设置

新建一个Object-C工程,写了下方代码,给一个 OC 方法下断点,运行工程,就可以进入LLDB调试环境

lldb调试命令一
1、使用LLDB中“breakpoint set -n 函数名 ”下断点


1.在lldb 中输入给demo1方法下断点

 breakpoint set -n demo1  
 
输出:
​​​​Breakpoint 2: where = LLDB调试`-[ViewController demo1] at ViewController.m:27, address = 0x000000010d8e1be0

表示断点下入成功

其中:

Breakpoint 2: 表示这是全局的第二个断点
where = LLDB调试`-[ViewController demo1] at ViewController.m:27,:标示断点位置和方法名称
address = 0x000000010d8e1be0:断点所表示的内存地址


2.给函数下断点
breakpoint set -n "[ViewController douYinClick:]"
Breakpoint 3: where = LLDB调试`-[ViewController douYinClick:] at ViewController.m:34, address = 0x000000010d8e1c10

2、breakpoint set -l 行数 -f 文件名


输入:
breakpoint set -l 23 -f ViewController.m
输出:
Breakpoint 4: where = LLDB调试`-[ViewController touchesBegan:withEvent:] + 77 at ViewController.m:23:5, address = 0x000000010d8e1b8d

lldb调试命令一

3.使用breakpoint set -a 16位的物理地址

这里我们首先找到16为物理地址,xcode工具栏Debug  ->Debug WorkFlow -> Alawys Show Disassembly进入栈调试找到物理地址0x10d8e1b97;

输入:
breakpoint set -a 0x10d8e1b97
输出:
Breakpoint 5: where = LLDB调试`-[ViewController touchesBegan:withEvent:] + 87 at ViewController.m:24:6, address = 0x000000010d8e1b97

lldb调试命令一

4、使用函数全部名称设置断点

breakpoint set -F 函数全名

输入:
breakpoint set -F kuaiShouClick:

5.使用函数局部名称进行设置断点

输入:
 breakpoint set -r touchBegan
输出:
Breakpoint 9: 5 locations.

这个说明是第9组下了断点,共下了5处断点,但是我们项目只有一个处实现了,说明给底层函数中也可以使用这种方式下断点,我们可以使用 breakpoint list进行验证

输入:
breakpoint list
输出:
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 5

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x000000010d8e1b40, resolved, hit count = 5 

2: name = 'demo1', locations = 1, resolved = 1, hit count = 4
  2.1: where = LLDB调试`-[ViewController demo1] at ViewController.m:27, address = 0x000000010d8e1be0, resolved, hit count = 4 

3: names = {'[ViewController douYinClick:]', '[ViewController douYinClick:]'}, locations = 1, resolved = 1, hit count = 0
  3.1: where = LLDB调试`-[ViewController douYinClick:] at ViewController.m:34, address = 0x000000010d8e1c10, resolved, hit count = 0 

4: file = 'ViewController.m', line = 23, exact_match = 0, locations = 1, resolved = 1, hit count = 3
  4.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] + 77 at ViewController.m:23:5, address = 0x000000010d8e1b8d, resolved, hit count = 3 

5: address = LLDB调试[0x0000000100001b97], locations = 1, resolved = 1, hit count = 3
  5.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] + 87 at ViewController.m:24:6, address = 0x000000010d8e1b97, resolved, hit count = 3 

6: name = 'kuaiShouClick', locations = 0 (pending)

7: name = 'kuaiShouClick', locations = 0 (pending)

8: name = 'kuaiShouClick:', locations = 1, resolved = 1, hit count = 0
  8.1: where = LLDB调试`-[ViewController kuaiShouClick:] at ViewController.m:37, address = 0x000000010d8e1c60, resolved, hit count = 0 

9: regex = 'touchBegan', locations = 5, resolved = 5, hit count = 0
  9.1: where = UIKitCore`-[_UIFocusFastScrollingRecognizer focusEnginePanGesture:touchBeganAtDigitizerLocation:], address = 0x00007fff47371559, resolved, hit count = 0 
  9.2: where = UIKitCore`-[UIKeyboardMenuView showAsHUDFromLocation:withInputView:touchBegan:], address = 0x00007fff4750b652, resolved, hit count = 0 
  9.3: where = UIKitCore`-[_UITextDocumentInterface _setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff47518923, resolved, hit count = 0 
  9.4: where = UIKitCore`-[_UIInputViewControllerOutput setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff475196ce, resolved, hit count = 0 
  9.5: where = MapKit`-[MKActionRowItemView _touchBegan], address = 0x00007fff2768f2d6, resolved, hit count = 0 

这里我们可以看到第九组共有5出断点,都是底层使用的方法,并且都包含touchBegan 字段方法名称

3.LLDB断点配置

    其实断点配置还有很多,这里只是简单的列举几个:

  • -i <count> (--ignore -count <count>)
输入:
breakpoint set -F wangZheClick: -i 3
  • -o <boolean>只断住一次
输入:
 breakpoint set -F wangZheClick: -o yes
  • -N 给断点添加别名

输入:
breakpoint set -F douYinClick: -N douyinBreakPoint
输出:
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 2

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x0000000105105b40, resolved, hit count = 2 

3: name = 'douYinClick:', locations = 1, resolved = 1, hit count = 0
  Names:
    douyinBreakPoint

  3.1: where = LLDB调试`-[ViewController douYinClick:] at ViewController.m:34, address = 0x0000000105105c10, resolved, hit count = 0 
  • -N 给断点添加别名
输入:
breakpoint set -n demo1 -c "i==10"

4.断点的禁用与启用和删除

  • 查看断点列表:breakpoint list 
输入:
breakpoint list 
输出:
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 6

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x00000001094e2b30, resolved, hit count = 6 

2: name = 'demo1', locations = 1, resolved = 1, hit count = 5
Condition: i==10

  2.1: where = LLDB调试`-[ViewController demo1] at ViewController.m:29, address = 0x00000001094e2be0, resolved, hit count = 5 
  • 禁用第二组断点
输入:
breakpoint disable 2
输出:
1 breakpoints disabled.
查看下断点列表,说明第二组断点还在里面,只是没有启用
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 8

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x00000001094e2b30, resolved, hit count = 8 

2: name = 'demo1', locations = 1 Options: disabled 
Condition: i==10

  2.1: where = LLDB调试`-[ViewController demo1] at ViewController.m:29, address = 0x00000001094e2be0, unresolved, hit count = 5 
  • 启用第二组断点
输入:
breakpoint enable 2

注意:我在这里操作的时候都是以组为单位进行禁用断点和启用断点,因为一般情况下我们下断点一组都是只有一个断点;
如果是如下情况,我们使用:breakpoint set -r touchBegan,下断点后,共5处断点

breakpoint list
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 8

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x00000001094e2b30, resolved, hit count = 8 

2: name = 'demo1', locations = 1, resolved = 1, hit count = 5
Condition: i==10

  2.1: where = LLDB调试`-[ViewController demo1] at ViewController.m:29, address = 0x00000001094e2be0, resolved, hit count = 5 

3: regex = 'touchBegan', locations = 5, resolved = 5, hit count = 0
  3.1: where = UIKitCore`-[_UIFocusFastScrollingRecognizer focusEnginePanGesture:touchBeganAtDigitizerLocation:], address = 0x00007fff47371559, resolved, hit count = 0 
  3.2: where = UIKitCore`-[UIKeyboardMenuView showAsHUDFromLocation:withInputView:touchBegan:], address = 0x00007fff4750b652, resolved, hit count = 0 
  3.3: where = UIKitCore`-[_UITextDocumentInterface _setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff47518923, resolved, hit count = 0 
  3.4: where = UIKitCore`-[_UIInputViewControllerOutput setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff475196ce, resolved, hit count = 0 
  3.5: where = MapKit`-[MKActionRowItemView _touchBegan], address = 0x00007fff2768f2d6, resolved, hit count = 0 

我们只想给其中某一个方法下,可以找到它的编号:例如给[UIKeyboardMenuView showAsHUDFromLocation:withInputView:touchBegan:]这个方法下断点;

禁用输入:
breakpoint disable 3.3
启用输入:
breakpoint enable 3.3
  • 删除断点

        1.删除所有断点

输入:
breakpoint delete
输出:
About to delete all breakpoints, do you want to do that?: [Y/n] 
询问是否删除 Y
All breakpoints removed. (2 breakpoints)

查看breakpoint list
输出:No breakpoints currently set.



        2.删除组断点

输入:
breakpoint delete 2
输出:
1 breakpoints deleted; 0 breakpoint locations disabled.

查看breakpoint list

Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 8

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x00000001094e2b30, resolved, hit count = 8 

3: regex = 'touchBegan', locations = 5, resolved = 5, hit count = 0
  3.1: where = UIKitCore`-[_UIFocusFastScrollingRecognizer focusEnginePanGesture:touchBeganAtDigitizerLocation:], address = 0x00007fff47371559, resolved, hit count = 0 
  3.2: where = UIKitCore`-[UIKeyboardMenuView showAsHUDFromLocation:withInputView:touchBegan:], address = 0x00007fff4750b652, resolved, hit count = 0 
  3.3: where = UIKitCore`-[_UITextDocumentInterface _setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff47518923, resolved, hit count = 0 
  3.4: where = UIKitCore`-[_UIInputViewControllerOutput setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff475196ce, resolved, hit count = 0 
  3.5: where = MapKit`-[MKActionRowItemView _touchBegan], address = 0x00007fff2768f2d6, resolved, hit count = 0 

       2.删除组内断点

输入:
breakpoint delete 3.4
输出:
0 breakpoints deleted; 1 breakpoint locations disabled.

查看Breakpoint list 
Current breakpoints:
1: file = '/Users/a1/Desktop/GitHub/iOSLearn/LLDB/LLDB调试/LLDB调试/ViewController.m', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 8

  1.1: where = LLDB调试`-[ViewController touchesBegan:withEvent:] at ViewController.m:21, address = 0x00000001094e2b30, resolved, hit count = 8 

3: regex = 'touchBegan', locations = 5, resolved = 4, hit count = 0
  3.1: where = UIKitCore`-[_UIFocusFastScrollingRecognizer focusEnginePanGesture:touchBeganAtDigitizerLocation:], address = 0x00007fff47371559, resolved, hit count = 0 
  3.2: where = UIKitCore`-[UIKeyboardMenuView showAsHUDFromLocation:withInputView:touchBegan:], address = 0x00007fff4750b652, resolved, hit count = 0 
  3.3: where = UIKitCore`-[_UITextDocumentInterface _setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff47518923, resolved, hit count = 0 
  3.4: where = UIKitCore`-[_UIInputViewControllerOutput setInputModeList:touchBegan:fromLocation:updatePoint:], address = 0x00007fff475196ce, unresolved, hit count = 0  Options: disabled 
  3.5: where = MapKit`-[MKActionRowItemView _touchBegan], address = 0x00007fff2768f2d6, resolved, hit count = 0 

删除组内断点的时候,失败了,查询后发下组内断点还是存在的,说明删除断点的时候只能按照组进行删除,

5.查看 LLDB 的其他指令

  直接在LLDB 上 输入 :help 就可以查看其他的指令了,这里太多了,就不贴过来了;

    可以搜索部分比如:help breakpoint,就是查看 breakpoint下的所有指令。

输入:
help breakpoint
输出:
   Commands for operating on breakpoints (see 'help b' for shorthand.)

Syntax: breakpoint <subcommand> [<command-options>]

The following subcommands are supported:

      clear   -- Delete or disable breakpoints matching the specified source
                 file and line.
      command -- Commands for adding, removing and listing LLDB commands
                 executed when a breakpoint is hit.
      delete  -- Delete the specified breakpoint(s).  If no breakpoints are
                 specified, delete them all.
      disable -- Disable the specified breakpoint(s) without deleting them.  If
                 none are specified, disable all breakpoints.
      enable  -- Enable the specified disabled breakpoint(s). If no breakpoints
                 are specified, enable all of them.
      list    -- List some or all breakpoints at configurable levels of detail.
      modify  -- Modify the options on a breakpoint or set of breakpoints in
                 the executable.  If no breakpoint is specified, acts on the
                 last created breakpoint.  With the exception of -e, -d and -i,
                 passing an empty argument clears the modification.
      name    -- Commands to manage name tags for breakpoints
      read    -- Read and set the breakpoints previously saved to a file with
                 "breakpoint write".  
      set     -- Sets a breakpoint or set of breakpoints in the executable.
      write   -- Write the breakpoints listed to a file that can be read in
                 with "breakpoint read".  If given no arguments, writes all
                 breakpoints.

For more help on any particular subcommand, type 'help <command> <subcommand>'.

 6.断点的简写

  b -f ViewController.m -r Clicked: 等同于:breakpoint set --file ViewController.m -r Clicked:

  • b :breakpoint set
  • -f--file
  • -n--name

 但是 breakpoint list 、breakpoint disable 等只能简写成 break libreak dis ,因为简写的 b 后面默认会带上 set 的。

这一篇文章主要描述一下LLDB中我们经常使用的断点调试方式,下一篇文章讲下LLDB的进阶用法。