平時(shí)開(kāi)發(fā)的過(guò)程中使用Xcode
都是圖形化操作習(xí)慣了忌傻,要是脫離了xcode你還能調(diào)試代碼嗎屁置,恩,Xcode
已經(jīng)把我們慣壞了朗若,不管怎樣恼五,作為一個(gè)開(kāi)發(fā)對(duì)于了解圖形操作背后的東西應(yīng)該要了解。
申明:本文的[]
都表示命令簡(jiǎn)寫(xiě)
一捡偏、增加方法斷點(diǎn)
- breakpoint set --name[-n] "
方法名稱
" - breakpoint set --address[-a]
方法的內(nèi)存地址
上面4個(gè)都是等價(jià)的唤冈,通過(guò)breakpoint list
查看已經(jīng)添加的斷點(diǎn)
-
模糊設(shè)置斷點(diǎn) breakpoint set -r
方法名稱
,r 是正則的意思(lldb) breakpoint set -r foo Breakpoint 2: 136 locations.
發(fā)現(xiàn)有136個(gè)地方包含了
foo
字符串的方法,通過(guò)breakpoint list
查看可以知道在哪里有這個(gè)方法,輸出太多這邊就截取一部分银伟。(lldb) breakpoint list Current breakpoints: 1: file = '/Users/fangshufeng/Desktop/thirdPart/image/Image/Image/ViewController.m', line = 24, exact_match = 0, locations = 1, resolved = 1, hit count = 2 1.1: where = Image`-[ViewController touchesBegan:withEvent:] + 77 at ViewController.m:24, address = 0x000000010a4f768d, resolved, hit count = 2 2: regex = 'foo', locations = 136, resolved = 136, hit count = 2 2.1: where = Image`-[ViewController foo] + 23 at ViewController.m:30, address = 0x000000010a4f76f7, resolved, hit count = 2 2.2: where = libbsm.0.dylib`au_print_xml_footer, address = 0x000000010ce5d665, resolved, hit count = 0 2.3: where = libsystem_kernel.dylib`proc_reset_footprint_interval, address = 0x000000010d5254ed, resolved, hit count = 0 [...]
-
開(kāi)關(guān)斷點(diǎn)
- breakpoint disable
斷點(diǎn)編號(hào)
(讓某個(gè)斷點(diǎn)不可用但是不是刪除) - breakpoint enable
斷點(diǎn)編號(hào)
(讓斷點(diǎn)由不可用到可用) - breakpoint delete
斷點(diǎn)編號(hào)
(刪除斷點(diǎn)不可恢復(fù),需重新添加绘搞,不加編號(hào)則讓你選擇是否全部刪除)
- breakpoint disable
二彤避、線程斷點(diǎn)
源碼調(diào)試
這個(gè)平時(shí)用的最多了,看下圖
- thread continue [continue] [c ] : 程序繼續(xù)
- thread step-over [next] [n] : 單步運(yùn)?夯辖,把子函數(shù)當(dāng)做整體?一步執(zhí)行
- thread step-in [step] [s] : 單步運(yùn)?琉预,遇到子函數(shù)會(huì)進(jìn)入?函數(shù)
- thread step-out [finish] : 直接執(zhí)行完函數(shù),返回函數(shù)調(diào)用處
太常用不貼代碼了
- thread backtrace [bt]:查看調(diào)用椵锕樱回溯
如圖有時(shí)想查看代碼的調(diào)用堆棧圆米,但是xcode無(wú)法查看全,左邊的地方只能看到部分啄栓,當(dāng)然你可以點(diǎn)擊這個(gè)地方娄帖,去查看。
也可以通過(guò)命令thread backtrace
或者bt
frame
為棧幀的意思昙楚,想了解函數(shù)棧幀的點(diǎn)擊此處
- frame variable :查看函數(shù)棧幀內(nèi)的局部變量的值
(lldb) frame variable
(ViewController *) self = 0x00007fe390a04960
(SEL) _cmd = "touchesBegan:withEvent:"
(__NSSetM *) touches = 0x0000600003d30640 1 element
(UITouchesEvent *) event = 0x0000600000f66250
(int) a = 10
我們知道oc
的方法內(nèi)置了self
和_cmd
參數(shù),所以上面有self
和_cmd
匯編代碼調(diào)試
開(kāi)啟匯編模式
和源碼的區(qū)別是
- ni : 單步運(yùn)?近速,把子函數(shù)當(dāng)做整體?一步執(zhí)行
- si: 單步運(yùn)?,遇到子函數(shù)會(huì)進(jìn)入?函數(shù)
三堪旧、內(nèi)存斷點(diǎn)
有時(shí)候想看某個(gè)內(nèi)存發(fā)生改變的時(shí)候觸發(fā)斷點(diǎn)
- watchpoint set variable 變量
- watchpoint set variable 地址
我們給vc增加一個(gè)屬性bar
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"fff");
[self foo];
}
- (void)foo {
NSLog(@"--foo--");
NSLog(@"--foo2--");
NSLog(@"--foo3--");
for ( int i = 0; i < 3; i++) {
self.bar += 10;
}
}
通過(guò)該命令削葱,每當(dāng)內(nèi)存地址內(nèi)容被修改都會(huì)斷到
還可以配合watchpoint command
使用
- watchpoint command add 斷點(diǎn)編號(hào):當(dāng)內(nèi)存斷點(diǎn)觸發(fā)的時(shí)候執(zhí)行一些操作
每次改變都會(huì)輸出值。
watchpoint command add 1 // 1 表示 watchpoint 斷點(diǎn)編號(hào)
Enter your debugger command(s). Type 'DONE' to end.
> p self.bar;
> DONE // DONE 表示命令輸入結(jié)束
相關(guān)指令如下淳梦,見(jiàn)名知意了不需要作介紹了
- watchpoint list
- watchpoint disable 斷點(diǎn)編號(hào)
- watchpoint enable 斷點(diǎn)編號(hào)
- watchpoint delete 斷點(diǎn)編號(hào)
- watchpoint command list 斷點(diǎn)編號(hào)
- watchpoint command delete 斷點(diǎn)編號(hào)
四析砸、不常用但很實(shí)用的指令
場(chǎng)景1:
要想代碼執(zhí)行到31行的斷點(diǎn)的時(shí)候直接返回,不想執(zhí)行后面的代碼爆袍,就可以通過(guò)thread return
指令首繁,當(dāng)代碼執(zhí)行到31行斷點(diǎn)時(shí),thread return
可以實(shí)現(xiàn)螃宙。
場(chǎng)景2:有時(shí)崩潰了直接到main
函數(shù)了蛮瞄,無(wú)法知道崩潰的地方
都知道下面的代碼會(huì)崩潰
此時(shí)崩潰在下面的main函數(shù)處
終端輸出信息
并不知道函數(shù)崩潰在哪一行可以使用image lookup
指令
(lldb) image lookup -a 0x00000001022365db
Address: Image[0x00000001000015db] (Image.__TEXT.__text + 203)
Summary: Image`-[ViewController touchesBegan:withEvent:] + 139 at ViewController.m:30
發(fā)現(xiàn)崩潰在ViewController
的touchesBegan:withEvent:
在30行處。
image lookup
周邊
- image lookup -t 類(lèi):查看類(lèi)的屬性
(lldb) image lookup -t UIView
Best match found in /Users/fangshufeng/Library/Developer/Xcode/DerivedData/Image-cncplmezlusnyackyzpyppykazhf/Build/Products/Debug-iphonesimulator/Image.app/Image:
id = {0x00001063}, name = "UIView", byte-size = 8, decl = UIView.h:143, compiler_type = "@interface UIView : UIResponder
@property ( readonly,getter = layerClass,setter = <null selector>,nonatomic,class ) Class layerClass;
@property ( getter = isUserInteractionEnabled,setter = setUserInteractionEnabled:,assign,readwrite,nonatomic ) BOOL userInteractionEnabled;
@property ( getter = tag,setter = setTag:,assign,readwrite,nonatomic ) NSInteger tag;
@property ( readonly,getter = layer,setter = <null selector>,nonatomic ) CALayer * layer;
@property ( readonly,getter = canBecomeFocused,setter = <null selector>,nonatomic ) BOOL canBecomeFocused;
@property ( readonly,getter = isFocused,setter = <null selector>,nonatomic ) BOOL focused;
@property ( getter = semanticContentAttribute,setter = setSemanticContentAttribute:,assign,readwrite,nonatomic ) UISemanticContentAttribute semanticContentAttribute;
@property ( readonly,getter = effectiveUserInterfaceLayoutDirection,setter = <null selector>,nonatomic ) UIUserInterfaceLayoutDirection effectiveUserInterfaceLayoutDirection;
@end"
- image lookup -n 方法名稱:查看方法的位置
(lldb) image lookup -n foo
1 match found in /Users/fangshufeng/Library/Developer/Xcode/DerivedData/Image-cncplmezlusnyackyzpyppykazhf/Build/Products/Debug-iphonesimulator/Image.app/Image:
Address: Image[0x0000000100001660] (Image.__TEXT.__text + 224)
Summary: Image`-[ViewController foo] at ViewController.m:33
- image lookup -o -f :可以查看內(nèi)存的偏移量谆扎,這個(gè)再逆向中很常用
還有一些常見(jiàn)的指令比如p
挂捅、po
、expression
就不說(shuō)了堂湖。