一册踩、常用的命令
1.print、po輸出打印
打印變量的值可以使用print(簡寫p)命令效拭,該命令如果打印的是基礎數(shù)據(jù)類型暂吉,則會列出基礎數(shù)據(jù)類型的類型和值。如果是對象缎患,還會打印出對象指針地址慕的,如下所示:
(lldb) p str
(__NSCFConstantString *) $1 = 0x000000010c14e068 @"我是一個普通的字符串"
(lldb) p intDemo
(NSInteger) $2 = 2
(lldb)
print命令輸出的信息中帶有 $0 、 $1的字樣挤渔。我們可以將它看作是lldb內(nèi)部的變量肮街,我們可以在后面的查詢中直接使用這些值。比如現(xiàn)在我要重新取回 $1 的值(或者調(diào)用$1的方法都是可以的判导,注意在使用lldb內(nèi)部的變量$來記錄變量的時候嫉父,沒有語法提示,所以你需要自己能準確的打印出方法的名字眼刃,或者在Xcode編輯器中去打好方法绕辖,然后再拷貝到lldb處)
(lldb) p $1
(__NSCFConstantString *) $1 = 0x000000010c14e068 @"我是一個普通的字符串"
每打印一個與對象相關(guān)的命令,$后面的值都會加1
print的打印信息較多擂红,如果只想查看對象的值仪际,則可以使用po(printobject)命令,如下所示:
(lldb) po str
我是一個普通的字符串
(lldb) po intDemo
2
對于基礎數(shù)據(jù)類型,還可以指定格式打印弟头,如用16進制的格式打臃宰ァ:
(lldb) p/x intDemo
(NSInteger) $14 = 0x0000000000000002
其他格式(如:八進制,二進制等)不做過多說明赴恨,感興趣可以自己研究疹娶。
上面的例子是比較簡單的,在實際開發(fā)中的應用是非常廣泛的伦连,如雨饺,打印self.view所有的子view,查看某個子view的描述等等惑淳。
2.call
call調(diào)用方法的意思额港,和p,po也有此功能,call可以使用在沒有返回值歧焦,不需要顯示輸出的情況下移斩,如設置view顏色
(lldb) call [self.view setBackgroundColor:[UIColor redColor]]
這樣就不用為了看改一下顏色的效果再進行build一次了,尤其是build一次還要經(jīng)過漫長的等待绢馍。
3.expression表達式(expr)
expr常用于調(diào)試時修改變量的值向瓷,如進入商品詳情頁之后,我想使用一個測試商品進行測試舰涌,這時猖任,不需要修改代碼再重新編譯就可以看到效果,來試驗一下瓷耙。我在viewDidLoad中打一個斷點朱躺,進入商品詳情頁,到斷點位置后搁痛,輸入expr命令來修改 productId·
和skuId
,然后繼續(xù)運行长搀,OK了,省去了寫測試代碼和編譯的時間落追,事半功倍有沒有盈滴!
(lldb) expr self.productId = @"A0000001076"
(__NSCFString *) $0 = 0x00007ffc78a8f300 @"A0000001076"
(lldb) expr self.currentSkuId = @"sku10210013"
(__NSCFString *) $1 = 0x00007ffc790610c0 @"sku10210013"
它的作用不僅僅是修改變量的值,你加其他功能的代碼也是可以的轿钠,只要是和上下文相關(guān)的一句代碼就OK,如:初始化導航欄之后病苗,我想修改一下它的顏色疗垛,看一下效果,如下:
(lldb) expr self.superNavBarView.backgroundColor = [UIColor redColor]
(UICachedDeviceRGBColor *) $3 = 0x00007ffc783d0290
注意:表達式之后不需要加分號硫朦,加了分號之后會報錯
4.image
(1)image lookup --address尋址贷腕,定位異常代碼位置
舉例說明,下面這段代碼,執(zhí)行的時候必然會出現(xiàn)異常泽裳,因為越界了
NSString *str = @"245656";
[str substringFromIndex:10];
控制臺輸出了以下崩潰信息:
2016-05-27 14:58:54.173 GomeEShop[61425:4544758] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFConstantString substringFromIndex:]: Index 10 out of bounds; string length 6'
*** First throw call stack:
(
0 CoreFoundation 0x000000010e99ad85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000011360ddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010e99acbd +[NSException raise:format:] + 205
3 Foundation 0x000000010f8698bd -[NSString substringFromIndex:] + 126
4 GomeEShop 0x00000001097c396f -[GMFPTGoodsDetailVC initNecessaryViews] + 3903
5 GomeEShop 0x00000001097c1c51 -[GMFPTGoodsDetailVC viewDidLoad] + 369
6 UIKit 0x00000001110ae984 -[UIViewController loadViewIfRequired] + 1198
7 UIKit 0x00000001110b493b -[UIViewController __viewWillAppear:] + 120
8 UIKit 0x00000001110e4750 -[UINavigationController _startCustomTransition:] + 1203
9 UIKit 0x00000001110f4b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
10 UIKit 0x00000001110f5d0b -[UINavigationController __viewWillLayoutSubviews] + 57
11 UIKit 0x00000001112a4503 -[UILayoutContainerView layoutSubviews] + 248
12 UIKit 0x0000000110fce980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
13 QuartzCore 0x0000000110c85c00 -[CALayer layoutSublayers] + 146
14 QuartzCore 0x0000000110c7a08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
15 QuartzCore 0x0000000110c79f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
16 QuartzCore 0x0000000110c6e3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
17 QuartzCore 0x0000000110c9c086 _ZN2CA11Transaction6commitEv + 486
18 UIKit 0x0000000110f4019b _afterCACommitHandler + 174
19 CoreFoundation 0x000000010e8bfc37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
20 CoreFoundation 0x000000010e8bfba7 __CFRunLoopDoObservers + 391
21 CoreFoundation 0x000000010e8b57fb __CFRunLoopRun + 1147
22 CoreFoundation 0x000000010e8b50f8 CFRunLoopRunSpecific + 488
23 GraphicsServices 0x000000011481bad2 GSEventRunModal + 161
24 UIKit 0x0000000110f13f09 UIApplicationMain + 171
25 GomeEShop 0x00000001091c0ebf main + 111
26 libdyld.dylib 0x0000000113ba992d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
通過異常信息瞒斩,我們看到崩潰很可能發(fā)生在-[GMFPTGoodsDetailVCinitNecessaryViews]
方法中,也就是地址0x00000001097c396f
,我們輸入以下命令來進行精確定位
(lldb) image lookup --address 0x00000001097c396f
執(zhí)行后輸出以下信息
Address: GomeEShop[0x00000001006af96f] (GomeEShop.__TEXT.__text + 7000239)
Summary: GomeEShop`-[GMFPTGoodsDetailVC initNecessaryViews] + 3903 at GMFPTGoodsDetailVC.m:589
我們看到出錯的位置在GMFPTGoodsDetailVC.m
文件中的initNecessaryViews
方法中涮总,第589行
(2)image lookup --type查看類型
我們還可以使用image lookup
命令來查看具體的類型胸囱,如下所示:
(lldb) image lookup --type UIImage
執(zhí)行后輸出了UIImage所包含的屬性
Best match found in /Users/zhangbeibei/Library/Developer/Xcode/DerivedData/GomeEShop-dhpyydtykrwzvpawiualtxfthsqy/Build/Products/Debug-iphonesimulator/GomeEShop.app/GomeEShop:
id = {0x00cabeb2}, name = "UIImage", byte-size = 8, decl = UIImage.h:53, compiler_type = "@interface UIImage : NSObject
@property ( readonly,getter = size,setter = <null selector>,nonatomic ) CGSize size;
@property ( readonly,getter = CGImage,setter = <null selector>,nonatomic ) CGImageRef CGImage;
@property ( readonly,getter = CIImage,setter = <null selector>,nonatomic ) CIImage * CIImage;
@property ( readonly,getter = imageOrientation,setter = <null selector>,nonatomic ) UIImageOrientation imageOrientation;
@property ( readonly,getter = scale,setter = <null selector>,nonatomic ) CGFloat scale;
@property ( readonly,getter = images,setter = <null selector>,nonatomic ) NSArray * images;
@property ( readonly,getter = duration,setter = <null selector>,nonatomic ) NSTimeInterval duration;
@property ( readonly,getter = capInsets,setter = <null selector>,nonatomic ) UIEdgeInsets capInsets;
@property ( readonly,getter = resizingMode,setter = <null selector>,nonatomic ) UIImageResizingMode resizingMode;
@property ( readonly,getter = alignmentRectInsets,setter = <null selector>,nonatomic ) UIEdgeInsets alignmentRectInsets;
@property ( readonly,getter = renderingMode,setter = <null selector>,nonatomic ) UIImageRenderingMode renderingMode;
@property ( readonly,getter = traitCollection,setter = <null selector>,copy,nonatomic ) UITraitCollection * traitCollection;
@property ( readonly,getter = imageAsset,setter = <null selector>,nonatomic ) UIImageAsset * imageAsset;
@property ( readonly,getter = flipsForRightToLeftLayoutDirection,setter = <null selector>,nonatomic ) BOOL flipsForRightToLeftLayoutDirection;
@end"
5.bt
我們可以用bt來打印調(diào)用堆棧,加all可打印多有thread的堆棧瀑梗,如下圖
(lldb) bt
* thread #1: tid = 0x4558f6, 0x0000000113eedf06 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000113eedf06 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x0000000113eb54ec libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x0000000113c4fcec libsystem_c.dylib`abort + 129
frame #3: 0x0000000113a4e051 libc++abi.dylib`abort_message + 257
frame #4: 0x0000000113a73ac9 libc++abi.dylib`default_terminate_handler() + 267
frame #5: 0x000000011360e046 libobjc.A.dylib`_objc_terminate() + 103
frame #6: 0x0000000113a7126e libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x0000000113a70ef9 libc++abi.dylib`__cxa_rethrow + 99
frame #8: 0x000000011360df5e libobjc.A.dylib`objc_exception_rethrow + 40
frame #9: 0x000000010e8b51b4 CoreFoundation`CFRunLoopRunSpecific + 676
frame #10: 0x000000011481bad2 GraphicsServices`GSEventRunModal + 161
frame #11: 0x0000000110f13f09 UIKit`UIApplicationMain + 171
* frame #12: 0x00000001091c0ebf GomeEShop`main(argc=1, argv=0x00007fff56aea5f8) + 111 at main.m:16
frame #13: 0x0000000113ba992d libdyld.dylib`start + 1
frame #14: 0x0000000113ba992d libdyld.dylib`start + 1
二烹笔、斷點編輯
1.設置觸發(fā)條件
我們在進行斷點調(diào)試時,可以為斷點設置觸發(fā)條件抛丽,以便針對特定的數(shù)據(jù)進行分析谤职,觀察程序運行正常,如在首頁的403行設置一個斷點亿鲜,觸發(fā)條件是isCache
為NO
這樣當刷新數(shù)據(jù)且數(shù)據(jù)是從緩存中讀取的時候允蜈,該斷點生效
2.設置觸發(fā)事件
在編輯斷點時從Action選項列表中可以看到,斷點的觸發(fā)事件有AppleScript蒿柳、Capture GPU Frame陷寝、Debugger Command、Log Message其馏、Shell Command凤跑、Sound這幾種。
Debugger Command
它和上述說的在控制臺輸入的命令是一樣的叛复,可以起到相同的效果仔引,但是它的方便之處在于,如果你勾選了Automatically continue after evaluating actions
褐奥,在斷點處程序不會中斷咖耘,會正常運行。不足之處是撬码,輸入命令時沒有聯(lián)想提示儿倒,不太方便。
如下圖呜笑,在加載首頁數(shù)據(jù)時的斷點處夫否,添加了命令expr self.superNavBarView.backgroundColor = [UIColor blueColor]
,再勾選上Automatically continue after evaluating actions
叫胁,執(zhí)行到此處的時候不會停止凰慈,而且導航欄的顏色變成了藍色。
使用Debugger Command
驼鹅,再加po
命令可以隨心所欲的輸出你需要的信息微谓,而你就不用為了調(diào)試一個東西而加一句NSLog然后再重新編譯一次看結(jié)果了森篷,代碼中也少了很多NSLog
這樣的語句,是不是簡潔多了豺型。
注意:經(jīng)試驗在這里輸出格式化字符串時仲智,不能使用[NSString stringWithFormat:@"%@",str]
,否則會報下面的錯誤姻氨,要使用上圖中的[[NSString alloc] initWithFormat:@"%@",str]
才可以钓辆,是不是有點怪
error: too many arguments to method call, expected 1, have 2
error: 1 errors parsing expression
Log Message
看字面意思就知道這個是輸出log,在輸入框中直接輸入你想輸出的內(nèi)容接哼绑,比Debugger Command+po方便岩馍,但是它很直白,不能輸出變量等信息抖韩,只能輸出靜態(tài)字符串蛀恩,如下圖,輸出的內(nèi)容就是輸入的內(nèi)容茂浮,不會被格式化
輸出內(nèi)容就是[[NSString alloc] initWithFormat:@"%@",str]
Sound
設置聲音双谆,當代碼執(zhí)行到斷點處時,會有提示音席揽,對我們來說不是經(jīng)常用
其他幾種就不做探究了顽馋,因為我也不是很懂,呵呵幌羞。寸谜。。
三属桦、命令的簡稱和別名
1.簡稱
很多時候熊痴,LLDB完整的命令是很長的。比如前面所說的image lookup --address
這個組合命令聂宾。為了方便日常的使用果善,提高效率,LLDB命令也提供通過簡稱的方式調(diào)用命令系谐。如 image lookup —address
這個 命令巾陕,可以簡寫為im loo -a
,expression
簡寫為expr
纪他,print
簡寫為p
等等鄙煤,自己去發(fā)現(xiàn)吧。
2.別名
除了使用命令的簡稱外止喷,對于不好記憶的命令馆类,我們還可以為它設置別名,現(xiàn)在我們?yōu)?code>imagelookup –address設置一個別名叫imgadd
輸入命令:
command alias imgadd image lookup --address %1
然后再使用imgadd
命令和imagelookup –address
就是一樣的了弹谁,不過我發(fā)現(xiàn)比較坑的是乾巧,重新編譯一次工程后,設置的別名就不好使了预愤,還是使用原名吧沟于,在Xcode控制臺輸入命令時都會有聯(lián)想提示,也比較方便
以上介紹的命令是比較常用的一些植康,然而lldb的命令遠不只有這些旷太,想知道還有哪些,輸入help销睁,查看一下
(lldb) help
Debugger commands:
apropos -- Find a list of debugger commands related to a particular
word/subject.
breakpoint -- A set of commands for operating on breakpoints. Also see
_regexp-break.
bugreport -- Set of commands for creating domain specific bugreports.
command -- A set of commands for managing or customizing the
debugger commands.
disassemble -- Disassemble bytes in the current function, or elsewhere
in the executable program as specified by the user.
expression -- Evaluate an expression (ObjC++ or Swift) in the current
program context, using user defined variables and
variables currently in scope.
frame -- A set of commands for operating on the current thread's
frames.
gdb-remote -- Connect to a remote GDB server. If no hostname is
provided, localhost is assumed.
gui -- Switch into the curses based GUI mode.
help -- Show a list of all debugger commands, or give details
about specific commands.
kdp-remote -- Connect to a remote KDP server. udp port 41139 is the
default port number.
language -- A set of commands for managing language-specific
functionality.'.
log -- A set of commands for operating on logs.
memory -- A set of commands for operating on memory.
platform -- A set of commands to manage and create platforms.
plugin -- A set of commands for managing or customizing plugin
commands.
process -- A set of commands for operating on a process.
quit -- Quit out of the LLDB debugger.
register -- A set of commands to access thread registers.
script -- Pass an expression to the script interpreter for
evaluation and return the results. Drop into the
interactive interpreter if no expression is given.
settings -- A set of commands for manipulating internal settable
debugger variables.
source -- A set of commands for accessing source file information
target -- A set of commands for operating on debugger targets.
thread -- A set of commands for operating on one or more threads
within a running process.
type -- A set of commands for operating on the type system
version -- Show version of LLDB debugger.
watchpoint -- A set of commands for operating on watchpoints.
Current command abbreviations (type 'help command alias' for more info):
add-dsym -- ('target symbols add') Add a debug symbol file to one of the
target's current modules by specifying a path to a debug symbols
file, or using the options to specify a module to download
symbols for.
attach -- ('_regexp-attach') Attach to a process id if in decimal,
otherwise treat the argument as a process name to attach to.
b -- ('_regexp-break') Set a breakpoint using a regular expression
to specify the location, where <linenum> is in decimal and
<address> is in hex.
bt -- ('_regexp-bt') Show a backtrace. An optional argument is
accepted; if that argument is a number, it specifies the number
of frames to display. If that argument is 'all', full
backtraces of all threads are displayed.
c -- ('process continue') Continue execution of all threads in the
current process.
call -- ('expression --') Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.
continue -- ('process continue') Continue execution of all threads in the
current process.
detach -- ('process detach') Detach from the current process being
debugged.
di -- ('disassemble') Disassemble bytes in the current function, or
elsewhere in the executable program as specified by the user.
dis -- ('disassemble') Disassemble bytes in the current function, or
elsewhere in the executable program as specified by the user.
display -- ('_regexp-display') Add an expression evaluation stop-hook.
down -- ('_regexp-down') Go down "n" frames in the stack (1 frame by
default).
env -- ('_regexp-env') Implements a shortcut to viewing and setting
environment variables.
exit -- ('quit') Quit out of the LLDB debugger.
f -- ('frame select') Select a frame by index from within the
current thread and make it the current frame.
file -- ('target create') Create a target using the argument as the
main executable.
finish -- ('thread step-out') Finish executing the function of the
currently selected frame and return to its call site in
specified thread (current thread, if none specified).
image -- ('target modules') A set of commands for accessing information
for one or more target modules.
j -- ('_regexp-jump') Sets the program counter to a new address.
jump -- ('_regexp-jump') Sets the program counter to a new address.
kill -- ('process kill') Terminate the current process being debugged.
l -- ('_regexp-list') Implements the GDB 'list' command in all of
its forms except FILE:FUNCTION and maps them to the appropriate
'source list' commands.
list -- ('_regexp-list') Implements the GDB 'list' command in all of
its forms except FILE:FUNCTION and maps them to the appropriate
'source list' commands.
n -- ('thread step-over') Source level single step in specified
thread (current thread, if none specified), stepping over calls.
next -- ('thread step-over') Source level single step in specified
thread (current thread, if none specified), stepping over calls.
nexti -- ('thread step-inst-over') Single step one instruction in
specified thread (current thread, if none specified), stepping
over calls.
ni -- ('thread step-inst-over') Single step one instruction in
specified thread (current thread, if none specified), stepping
over calls.
p -- ('expression --') Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.
po -- ('expression -O -- ') Evaluate an expression (ObjC++ or Swift)
in the current program context, using user defined variables and
variables currently in scope.
print -- ('expression --') Evaluate an expression (ObjC++ or Swift) in
the current program context, using user defined variables and
variables currently in scope.
q -- ('quit') Quit out of the LLDB debugger.
r -- ('process launch -X true --') Launch the executable in the
debugger.
rbreak -- ('breakpoint set -r %1') Sets a breakpoint or set of
breakpoints in the executable.
repl -- ('expression -r -- ') Evaluate an expression (ObjC++ or Swift)
in the current program context, using user defined variables and
variables currently in scope.
run -- ('process launch -X true --') Launch the executable in the
debugger.
s -- ('thread step-in') Source level single step in specified thread
(current thread, if none specified).
si -- ('thread step-inst') Single step one instruction in specified
thread (current thread, if none specified).
step -- ('thread step-in') Source level single step in specified thread
(current thread, if none specified).
stepi -- ('thread step-inst') Single step one instruction in specified
thread (current thread, if none specified).
t -- ('thread select') Select a thread as the currently active
thread.
tbreak -- ('_regexp-tbreak') Set a one shot breakpoint using a regular
expression to specify the location, where <linenum> is in
decimal and <address> is in hex.
undisplay -- ('_regexp-undisplay') Remove an expression evaluation
stop-hook.
up -- ('_regexp-up') Go up "n" frames in the stack (1 frame by
default).
x -- ('memory read') Read from the memory of the process being
debugged.
For more information on any command, type 'help <command-name>'.
正如上述最后一句所說供璧,想了解某個命令的更多信息,輸入help +命令名即可
命令是不是太多了冻记,想要熟練使用睡毒,就要多用多用再多用,熟練之后效率會有意想不到的提高!