LLDB指令

平時(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)存地址
15428751580151.jpg

上面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)則讓你選擇是否全部刪除)
15428758913025.jpg

二彤避、線程斷點(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)用處

太常用不貼代碼了

15428763316698.jpg
  • thread backtrace [bt]:查看調(diào)用椵锕樱回溯
15428766389548.jpg

如圖有時(shí)想查看代碼的調(diào)用堆棧圆米,但是xcode無(wú)法查看全,左邊的地方只能看到部分啄栓,當(dāng)然你可以點(diǎn)擊這個(gè)地方娄帖,去查看。

15428767683217.jpg

也可以通過(guò)命令thread backtrace或者bt

15428770034517.jpg

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)啟匯編模式

15428781223072.jpg

和源碼的區(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ì)斷到

15428798696567.jpg

還可以配合watchpoint command使用

  • watchpoint command add 斷點(diǎn)編號(hào):當(dāng)內(nèi)存斷點(diǎn)觸發(fā)的時(shí)候執(zhí)行一些操作

每次改變都會(huì)輸出值。


15428800472134.jpg
 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:

15428794369809.jpg

要想代碼執(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ì)崩潰

15428804652206.jpg

此時(shí)崩潰在下面的main函數(shù)處

15428805038949.jpg

終端輸出信息

15428805244685.jpg

并不知道函數(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)崩潰在ViewControllertouchesBegan: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挂捅、poexpression就不說(shuō)了堂湖。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末闲先,一起剝皮案震驚了整個(gè)濱河市状土,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌伺糠,老刑警劉巖蒙谓,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異训桶,居然都是意外死亡累驮,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)舵揭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)谤专,“玉大人,你說(shuō)我怎么就攤上這事午绳≈檬蹋” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵拦焚,是天一觀的道長(zhǎng)蜡坊。 經(jīng)常有香客問(wèn)我,道長(zhǎng)赎败,這世上最難降的妖魔是什么秕衙? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮螟够,結(jié)果婚禮上灾梦,老公的妹妹穿的比我還像新娘。我一直安慰自己妓笙,他們只是感情好若河,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著寞宫,像睡著了一般萧福。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上辈赋,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天鲫忍,我揣著相機(jī)與錄音,去河邊找鬼钥屈。 笑死悟民,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的篷就。 我是一名探鬼主播射亏,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了智润?” 一聲冷哼從身側(cè)響起及舍,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎窟绷,沒(méi)想到半個(gè)月后锯玛,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡兼蜈,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年攘残,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片为狸。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡肯腕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出钥平,到底是詐尸還是另有隱情,我是刑警寧澤姊途,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布涉瘾,位于F島的核電站,受9級(jí)特大地震影響捷兰,放射性物質(zhì)發(fā)生泄漏立叛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一贡茅、第九天 我趴在偏房一處隱蔽的房頂上張望秘蛇。 院中可真熱鬧,春花似錦顶考、人聲如沸赁还。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)艘策。三九已至,卻和暖如春渊季,著一層夾襖步出監(jiān)牢的瞬間朋蔫,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工却汉, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留驯妄,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓合砂,卻偏偏與公主長(zhǎng)得像青扔,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 常用指令 continue / c 執(zhí)行到下一個(gè)斷點(diǎn) next / n 單步運(yùn)行 把子函數(shù)當(dāng)作整體 st...
    小狼_zy閱讀 387評(píng)論 0 1
  • LLDB的Xcode默認(rèn)的調(diào)試器赎懦,它與LLVM編譯器一起雀鹃,帶給我們更豐富的流程控制和數(shù)據(jù)檢測(cè)的調(diào)試功能。平時(shí)用Xc...
    小笨狼閱讀 20,474評(píng)論 31 187
  • ??LLDB的Xcode默認(rèn)的調(diào)試器励两,它與LLVM編譯器一起黎茎,帶給我們更豐富的流程控制和數(shù)據(jù)檢測(cè)的調(diào)試功能。平時(shí)用...
    Thinkdifferents閱讀 1,559評(píng)論 1 4
  • 一当悔、概述 LLDB全稱 [ Low Level Debugger ], 默認(rèn)內(nèi)置于Xcode中的動(dòng)態(tài)調(diào)試工具傅瞻。標(biāo)準(zhǔn)...
    Superman168閱讀 15,234評(píng)論 2 19
  • Xcode Debugging 你的代碼,或者任何人的代碼中總會(huì)有bug存在盲憎,你可以把調(diào)試看做是更好地理解代碼的一...
    吃蘑菇De大灰狼閱讀 2,448評(píng)論 0 2