1. 程式人生 > >(轉)Xcode中LLDB的基本命令的使用(Swift)

(轉)Xcode中LLDB的基本命令的使用(Swift)

 

上面劃線部分:d對應十進位制,h對應十六進位制,o對應八進位制,再試試其他幾個:

(lldb) p/o person.age

(UInt32) $R2 = 030

(lldb) p/d person.age

(UInt32) $R3 = 24

3:列印Raw value,-R ( --raw-output ) Don't use formatting options.

(lldb) e -R -- person

(LLDBDemo.Person) $R7 = 0x00007fd20a706be0 {

ObjectiveC.NSObject = {}

name = {

_core = {

_baseAddress = {

_rawValue = 0x000000011321e4e0

}

_countAndFlags = {

_value = 3

}

_owner = None {

Some = {

instance_type = 0x0000000000000000

}

}

}

}

age = {

_value = 24

}

}

4:顯示變數型別, -T ( --show-types ) Show variable types when dumping values.

(lldb) e -T -- person

(LLDBDemo.Person) $R9 = 0x00007fd20a706be0 {

(NSObject) ObjectiveC.NSObject = {}

(String) name = "hua"

(UInt32) age = 24

}

5:顯示變數位置資訊, -L ( --location ) Show variable location information.

(lldb) e -L -- person

scalar(0x00007fd20a706be0): (LLDBDemo.Person) $R10 = 0x00007fd20a706be0 {

scalar(0x00007fd20a706be0): ObjectiveC.NSObject = {}

0x00007fd20a706bf0: name = "hua"

0x00007fd20a706c08: age = 24

}

6:當然也可以組合多個部分一起檢視,這裡就是組合之前的R、T、L

(lldb) e -RTL -- person

scalar(0x00007fd20a706be0): (LLDBDemo.Person) $R11 = 0x00007fd20a706be0 {

scalar(0x00007fd20a706be0): (ObjectiveC.NSObject) ObjectiveC.NSObject = {}

0x00007fd20a706bf0: (Swift.String) name = {

0x00007fd20a706bf0: (Swift._StringCore) _core = {

0x00007fd20a706bf0: (Swift.COpaquePointer) _baseAddress = {

0x00007fd20a706bf0: (Builtin.RawPointer) _rawValue = 0x000000011321e4e0

}

0x00007fd20a706bf8: (Swift.UInt) _countAndFlags = {

0x00007fd20a706bf8: (Builtin.Int64) _value = 3

}

0x00007fd20a706c00: (Swift.Optional) _owner = None {

0x00007fd20a706c00: (AnyObject) Some = {

0x00007fd20a706c00: (Builtin.RawPointer) instance_type = 0x0000000000000000

}

}

}

}

0x00007fd20a706c08: (Swift.UInt32) age = {

0x00007fd20a706c08: (Builtin.Int32) _value = 24

}

}

7:多行表示式模式

(lldb) e

Enter expressions, then terminate with an empty line to evaluate:

1 struct Compass{var dirction = "N";var angle = 16.5}

2 var c = Compass()

3 print(c)

4

(Compass #1)(dirction: "N", angle: 16.5)

(lldb) e

Enter expressions, then terminate with an empty line to evaluate:

1 func add(a:Int,b:Int)->Int {return a+b }

2 let c = add(3,b:4)

3 print(c)

4

7

8:匯入模組框架

如果我們在除錯的過程中,需要獲取某個框架的類,但是當前檔案中並沒有匯入,我們可以在console中除錯過程中直接輸入import和框架名稱,好比如:我們需要MapKit中的類,直接:

(lldb) p import MapKit

9:LLDB變數

細心的朋友可能會發現輸出的資訊中帶有$1、$2的字樣。實際上,我們每次查詢的結果會儲存在一些持續變數中($[0-9]+),這樣你可以在後面的查詢中直接使用這些值。其實前面在使用layer的例子中已經提到了,在看個例子:

(lldb) e var $a = Person(name:"Jenny",age:24)

(lldb) p $a

(LLDBDemo.Person) $R12 = 0x00007fd20a512ba0 {

ObjectiveC.NSObject = {}

name = "Jenny"

age = 24

}


接下來看一下我們經常使用的斷點(breakpoint),斷點採用這個命令breakpoint,縮寫br,這裡先列出文件,看一下為我們提供了哪些功能:

(lldb) help breakpoint

The following subcommands are supported:

clear -- Clears a breakpoint or set of breakpoints in the executable.

command -- A set of commands for adding, removing and examining bits of

code to be executed when the breakpoint is hit (breakpoint

'commands').

delete -- Delete the specified breakpoint(s). If no breakpoints are

specified, delete them all.

disable -- Disable the specified breakpoint(s) without removing 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 -- A set of commands to manage name tags for breakpoints

set -- Sets a breakpoint or set of breakpoints in the executable.

For more help on any particular subcommand, type 'help

  
'.

基本的使用:

1:使用list顯示所有斷點資訊

\

當前工程只有一個斷點,所以只打印了一個斷點資訊,並且可以知道斷點的相關資訊,在當前檔案的29行,而且可以通過左邊斷點顯示區域來對比結果。

2:禁用斷點即disable命令

根據上面內容,現在我們來玩一玩,現在在原有的程式碼上再新增2個斷點,如下圖:

\

1)首先使前面兩個斷點失效

(lldb) br disable 1.*

1 breakpoints disabled.

(lldb) br disable 2.*

1 breakpoints disabled.

2)點選執行程式,會跳到第三個斷點,中間斷點不在暫停,並且斷點資訊也會不一樣,多了斷點失效部分的內容,如下圖:

\

3:我們也可以使用 enable恢復斷點

(lldb) br enable 1.*

1 breakpoints enabled.

(lldb) br enable 2.*

1 breakpoints enabled.


4:刪除斷點,刪除第一個斷點

(lldb) br delete 1

1 breakpoints deleted; 0 breakpoint locations disabled.

(lldb) br list

Current breakpoints:

2: file = '/Users/longshihua/Desktop/LLDBDemo/LLDBDemo/ViewController.swift', line = 28, exact_match = 0, locations = 1, resolved = 1, hit count = 1

2.1: where = LLDBDemo`LLDBDemo.ViewController.testPerson () -> () + 12 at ViewController.swift:28, address = 0x000000010b4f3d5c, resolved, hit count = 1

3: file = '/Users/longshihua/Desktop/LLDBDemo/LLDBDemo/ViewController.swift', line = 30, exact_match = 0, locations = 1, resolved = 1, hit count = 1

3.1: where = LLDBDemo`LLDBDemo.ViewController.testPerson () -> () + 204 at ViewController.swift:30, address = 0x000000010b4f3e1c, resolved, hit count = 1

5:新增斷點

(lldb) br set -f ViewController.swift -l26

Breakpoint 4:where = LLDBDemo`LLDBDemo.ViewController.testPerson () -> () +12 at ViewController.swift:28, address =0x000000010b4f3d5c

命令中的-f ViewController.swift和-l26

-f ( --file ) 具體的檔案

-l ( --line ) 檔案的位置,即具體哪一行

6:為函式新增斷點,這裡直接為我們的testPerson函式新增斷點,-F為函式名

(lldb) br set -F testPerson

Breakpoint 3:2 locations.

7:條件斷點

先新增一個函式,並加一個斷點,如下圖:當某種條件產生的時候觸發的斷點,現在這裡實現停在1000次迴圈的第800次

\

 

當i為800時觸發斷點

(lldb) br modify -c i==800

(lldb) po i

800

指令說明:

-c ( --condition )

The breakpoint stops onlyifthis condition expression evaluates to

true.

記住一點,多利用help,查詢命令和幫助資訊,所以這裡我們可以通過help br set來檢視更多資訊。

 

瞭解一下執行緒相關內容thread

1:threadreturn

Debug的時候,也許會因為各種原因,我們不想讓程式碼執行某個方法,或者要直接返回一個想要的值。這時候就該thread return上場了。thread return可以接受一個表示式,呼叫命令之後直接從當前的堆疊中返回表示式的值。我們只需在方法的開始位置加一個斷點,當程式中斷的時候,輸入命令即可。

2:thread step-over單步執行,執行下一行程式碼

thread step-in 進入函式體,單步執行

thread step-out 退出當前函式體

3:thread backtrace顯示堆疊資訊

(lldb) thread backtrace

* thread #1: tid = 0x35a80, 0x0000000105b62d5c LLDBDemo`ViewController.testPerson(self=0x00007fab09d2bf00) -> () + 12 at ViewController.swift:28, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1

* frame #0: 0x0000000105b62d5c LLDBDemo`ViewController.testPerson(self=0x00007fab09d2bf00) -> () + 12 at ViewController.swift:28

............

............

Type型別查詢

(lldb) help type

The following subcommands are supported:

category -- A set of commands for operating on categories

filter -- A set of commands for operating on type filters

format -- A set of commands for editing variable value display options

lookup -- Lookup a type by name in the select target. This command

takes 'raw' input (no need to quote stuff).

summary -- A set of commands for editing variable summary display

options

synthetic -- A set of commands for operating on synthetic type

representations

For more help on any particular subcommand, type 'help

  
'.

丟擲一個異常,不知道是啥的時候怎麼辦?我們可以使用lookup命令,查詢我們的型別資訊:

(lldb) type lookup Person

@objc class Person : ObjectiveC.NSObject {

@objc var name: Swift.String

@objc var age: Swift.UInt32

@objc init(name: Swift.String, age: Swift.UInt32)

@objc override var description: Swift.String {

@objc override get {}

}

@objc func personException() throws

@objc deinit

@objc @objc init()

}

frame(幀)首先看一下與frame(幀)相關解釋:

作用:frame -- A set of commandsfor operating on the current thread's frames.

(lldb) help frame

The following subcommands are supported:

info -- List information about the currently selected frame in the

current thread.

select -- Select a frame by index from within the current thread and

make it the current frame.

variable -- Show frame variables. All argument and local variables that

are in scope will be shown when no arguments are given. If

any arguments are specified, they can be names of argument,

local, file static and file global variables. Children of

aggregate variables can be specified such as 'var->child.x'.

For more help on any particular subcommand, type 'help

  
'.

(lldb) frame variable person

(LLDBDemo.Person) person = 0x00007fab09e13790 {

ObjectiveC.NSObject = {}

name = "Jack"

age = 24

}

我們可以使用frame info檢視當前frame的資訊:

(lldb) frame info

frame #0:0x0000000105b62e1c LLDBDemo`ViewController.testPerson(self=0x00007fab09d2bf00) -> () +204 at ViewController.swift:30

target命令

對於target這個命令,我們用得最多的可能就是target modules lookup。由於LLDB給 target modules取了個別名image,所以這個命令我們又可以寫成image lookup.

1:當我們想檢視一個型別的時候,可以使用image lookup --type,簡寫為image lookup -t,比如我們可以看看自己的Person型別資訊:

(lldb) image lookup -t Person

2:當我們想查詢一個方法或者符號的資訊,比如所在檔案位置等。我們可以使用image lookup --name,簡寫為 image lookup -n,這裡我們就直接查詢我們的testPerson函式,我們可以看到如下資訊:

(lldb) image lookup -n testPerson

2 matches found in /Users/longshihua/Library/Developer/Xcode/DerivedData/LLDBDemo-fxvhyqfwhdszpwdegllmmmsjcohz/Build/Products/Debug-iphonesimulator/LLDBDemo.app/LLDBDemo:

Address: LLDBDemo[0x0000000100003d50] (LLDBDemo.__TEXT.__text + 9872)

Summary: LLDBDemo`LLDBDemo.ViewController.testPerson () -> () at ViewController.swift:26 Address: LLDBDemo[0x0000000100003e30] (LLDBDemo.__TEXT.__text + 10096)

Summary: LLDBDemo`@objc LLDBDemo.ViewController.testPerson () -> () at ViewController.swift

3:當我們有一個地址,想查詢這個地址具體對應的檔案位置,可以使用 image lookup --address,簡寫為image lookup -a.