天天看點

Xcode調試技巧之一 GDB輸出

轉載:http://www.cocoachina.com/macdev/tips/2012/0515/4250.html

XCode 内置GDB,我們可以在指令行中使用 GDB 指令來調試我們的程式。下面将介紹一些常用的指令以及調試技巧。 po 指令 :為 print object 的縮寫,顯示對象的文本描述(顯示從對象的 de

XCode 内置GDB,我們可以在指令行中使用 GDB 指令來調試我們的程式。下面将介紹一些常用的指令以及調試技巧。

po 指令:為 print object 的縮寫,顯示對象的文本描述(顯示從對象的 description 消息獲得的字元串資訊)。

比如:

Xcode調試技巧之一 GDB輸出

上圖中,我使用 po 指令顯示一個 NSDictionary 的内容。注意在左側我們可以看到 dict 的一些資訊:3 key/value pairs,顯示該 dict 包含的資料量,而展開的資訊顯示 isa 層次體系(即class 和 metaclass結構關系)。我們可以右擊左側的 dict,選中“Print Description of "dict"”,則可以在控制台輸出 dict 的詳細資訊:

[cpp] view plaincopyprint?

Printing description of dict:  

<CFBasicHash 0x1001149e0 [0x7fff7e27ff40]>{type = immutable dict, count = 3,  

entries =>  

    0 : <CFString 0x100002458 [0x7fff7e27ff40]>{contents = "first"} = <CFString 0x100002438 [0x7fff7e27ff40]>{contents = "one"}  

    1 : <CFString 0x100002498 [0x7fff7e27ff40]>{contents = "second"} = <CFString 0x100002478 [0x7fff7e27ff40]>{contents = "two"}  

    2 : <CFString 0x1000024d8 [0x7fff7e27ff40]>{contents = "third"} = <CFString 0x1000024b8 [0x7fff7e27ff40]>{contents = "three"}  

}  

(gdb)   

print 指令:有點類似于格式化輸出,可以輸出對象的不同資訊:

如:

[cpp] view plaincopyprint?

(gdb) print (char *)[[dict description] cStringUsingEncoding:4]  

$1 = 0x1001159c0 "{\n    first = one;\n    second = two;\n    third = three;\n}"  

(gdb) print (int)[dict retainCount]  

$2 = 1  

(gdb)   

注:4是 NSUTF8StringEncoding 的值。

info 指令:我們可以檢視記憶體位址所在資訊

比如 "info symbol 記憶體位址" 可以擷取記憶體位址所在的 symbol 相關資訊:

[cpp] view plaincopyprint?

(gdb) info symbol 0x00000001000017f7  

main + 343 in section LC_SEGMENT.__TEXT.__text of /Users/LuoZhaohui/Library/Developer/Xcode/DerivedData/RunTimeSystem-anzdlhiwvlbizpfureuvenvmatnp/Build/Products/Debug/RunTimeSystem  

比如 "info line *記憶體位址" 可以擷取記憶體位址所在的代碼行相關資訊:

[cpp] view plaincopyprint?

(gdb) info line *0x00000001000017f7  

Line 62 of "/Users/LuoZhaohui/Documents/Study/RunTimeSystem/RunTimeSystem/main.m" starts at address 0x1000017f7 <main+343> and ends at 0x10000180a <main+362>.  

show 指令:顯示 GDB 相關的資訊。如:show version 顯示GDB版本資訊

[cpp] view plaincopyprint?

(gdb) show version  

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug  8 20:32:45 UTC 2011)  

Copyright 2004 Free Software Foundation, Inc.  

GDB is free software, covered by the GNU General Public License, and you are  

welcome to change it and/or distribute copies of it under certain conditions.  

Type "show copying" to see the conditions.  

There is absolutely no warranty for GDB.  Type "show warranty" for details.  

This GDB was configured as "x86_64-apple-darwin".  

help 指令:如果忘記某條指令的文法了,可以使用 help 指令名 來擷取幫助資訊。如:help info 顯示 info 指令的用法。

[cpp] view plaincopyprint?

(gdb) help info  

Generic command for showing things about the program being debugged.  

List of info subcommands:  

info address -- Describe where symbol SYM is stored  

info all-registers -- List of all registers and their contents  

info args -- Argument variables of current stack frame  

info auxv -- Display the inferior's auxiliary vector  

info breakpoints -- Status of user-settable breakpoints  

info catch -- Exceptions that can be caught in the current stack frame  

info checkpoints -- Help  

info classes -- All Objective-C classes  

......  

Type "help info" followed by info subcommand name for full documentation.  

Command name abbreviations are allowed if unambiguous.  

(gdb)   

在系統抛出異常處設定斷點

有時候我們的程式不知道跑到哪個地方就 crash 了,而 crash 又很難重制。保守的做法是在系統抛出異常之前設定斷點,具體來說是在 objc_exception_throw處設定斷點。設定步驟為:首先在 XCode 按 CMD + 6,進入斷點管理視窗;然後點選右下方的 +,增加新的 Symbolic Breakpoint,在 Symbol 一欄輸入:objc_exception_throw,然後點選 done,完成。 這樣在 Debug 模式下,如果程式即将抛出異常,就能在抛出異常處中斷了。比如在前面的代碼中,我讓 [firstObjctcrashTest]; 抛出異常。在 objc_exception_throw 處設定斷點之後,程式就能在該代碼處中斷了,我們進而知道代碼在什麼地方出問題了。

Xcode調試技巧之一 GDB輸出
  1. 命 令                        解釋  
  2. break NUM               在指定的行上設定斷點。  
  3. bt                      顯 示所有的調用棧幀。該指令可用來顯示函數的調用順序。  
  4. clear                   删 除設定在特定源檔案、特定行上的斷點。其用法為:clear FILENAME:NUM。  
  5. continue                繼續執行正在調試的程式。該指令用在程式 由于處理信号或斷點而  
  6.                         導緻停止運作 時。  
  7. display EXPR            每次程式停止後顯示表達式的值。表達式由程式定 義的變量組成。  
  8. file FILE               裝載指定的可執行檔案進行調試。  
  9. help NAME               顯 示指定指令的幫助資訊。  
  10. info break              顯 示目前斷點清單,包括到達斷點處的次數等。  
  11. info files              顯 示被調試檔案的詳細資訊。  
  12. info func               顯示所有的函數名稱。  
  13. info local              顯 示當函數中的局部變量資訊。  
  14. info prog               顯示被調試程式的執行狀 态。  
  15. info var                顯示所有的全局和靜态變量名稱。  
  16. kill                    終 止正被調試的程式。  
  17. list                    顯示源代碼段。  
  18. make                    在 不退出 gdb 的情況下運作 make 工具。  
  19. next                    在 不單步執行進入其他函數的情況下,向前執行一行源代碼。  
  20. print EXPR              顯 示表達式 EXPR 的值。  
  21. print- object            列印一個對象  
  22. print (int) name      列印一個類型  
  23. print- object [artist description]   調用一個函數  
  24. set artist = @"test"    設定變量值  
  25. whatis                      查 看變理的資料類型