Xcode 調試與快捷鍵

1.主題及字體

“command+,” 呼叫出偏好設置(首選項),選擇“Fonts & Colors”,選中一種主題(theme),例如“Midnight”,然後shift選擇Source Editor/Console中的所有項,點擊Fonts設置字體。Xcode默認字體爲menlo,可選其他等寬字體Consolas/Monaco。

2.View and Navigate

View Navigator

 

command+0:Show/Hide left tool panel

command+1-8:Project/Symbol/Find/Issue/Test/Debug/Breakpoint/Log Navigator

 

option+command+0:Show/Hide right tool panel

option+command+1:show the file inspector

option+command+2:show quick help inspector


View Editor Organization

control+1:Show Related Items(例如Callers/Callees、Includes/Included By)。可輸入實時搜索匹配。

control+2/3:Show Previous/Next History。可輸入實時搜索匹配。

control+4:Show Top Level Items
control+5:Show Group Files(當前文件夾內的所有文件)。可輸入實時搜索匹配。

control+6:Show Document Items(當前文件的Symbols)。可輸入實時搜索匹配。

可直接選中符號,然後“Navigate->Reveal in Symbol Navigator”,打開該接口的符號列表。

 

command+J:焦點切換(Move Focus),可配合鼠標和方向鍵。帶‘+’的“Move focus to a new assistant editor”可以快速在輔助編輯窗口中打開頭文件(*.h)/實現文件(*.m,*.mm)。

shift+command+J:在項目導航中定位當前文件(Reveal in Project Navigator)。


3.基本操作

command+[/]:向前/向後縮進

command+/:註釋選中的代碼

command+shift+[/]:切換標籤頁
單指左右滑動(control+command+←/→):在單標籤頁打開的多個文件間切換
option+command+←/→:摺疊當前代碼塊

option+shift+command+←/→:摺疊該文件內所有代碼塊(方法/函數)

option+command+[/]:Move Line Up/Down

shift+command+Y:顯示控制檯(Show/Hide the debug area

option+command+R:編輯配置(Edit Scheme)


括號匹配:雙擊某個分隔符(如{}、()、[] 等),Xcode會選中匹配代碼塊。

焦點列:灰色深度與代碼嵌套深度相關,鼠標懸停可突出顯示右側相應代碼塊,鼠標單擊可摺疊右側相應代碼塊(Code Folding)。

說明:從左到右,依次是“導航窗格(Navigator)->邊列(Gutter)->焦點列(Focus Ribbon)->代碼編輯窗口(Standard Editor)”。


4. 符號定位跳轉

control+command+↑/↓:切換頭文件/實現文件(switch between a source file (.m,*.mm,*.cc) and the associated header (.h) file)。
shift+command+O:Open Quickly,可快速查找“文件”、符號”。
shift+command+F(command+3):全局查找。

command+點擊Editor中選中的符號:跳轉到符號定義(jump to definition)。

control+command+J:跳轉到指定符號的定義處或實現處(Go to Declaration/Definition)。

右鍵或菜單File->Show in Finder:在Finder中定位該文件

5.輔助編輯窗口(Assistant Editor)

Assistant Editor有點類似VC中的Code Definition Window。
option+command+enter:打開Assistant Editor。

command+enter:關閉Assistant Editor。

使用快捷鍵進行切換或跳轉動作時,若同時按下option可以在輔助編輯窗口中打開相應文件或符號(For optional navigation (Option-clicking or Option-choosing a file), opens the file in a new Assistant editor pane.)。若在輔助窗口中操作,則在主窗口(Standard Editor)中打開。

 

option+點擊Project Navigator中選中的文件:在輔助編輯窗口中打開選中文件。

option+command+點擊Editor中選中的符號:在輔助編輯窗口中打開符號定義(jump to definition in assistant editor)。

option+control+command+↑/↓:在輔助窗口中打開對應的頭文件(*.h)/實現文件(*.m,*.mm,*.cc)。

 

 

點擊查看shift+command+Oshift+command+F(command+3)選中的文件或符號時,可同時按下option在輔助編輯窗口中打開。

control+1~6中打開選擇結果時,均可同時按下option在Assistant Editor中打開。

若在按下option的同時按下shift通常會出現一個導航窗格,可選擇在new window/tab/assistant-editor顯示打開。

For Option-Shift navigation (Option-Shift-click or Option-Shift-choose a file), Xcode displays a graphical navigation chooser showing the current layout. The chooser prompts you to open the file in any open editor pane in any window and tab, or to open the file in a new editor pane, window, or tab.

6.代碼自動完成功能

esc就當前輸入上下文呼出/隱藏Auto Completion提示;上下方向鍵在提示中選擇選項,enter(return)選中,tab可一截一截匹配;tab可在各個佔位符之間移動

輸入Objective-C對象及 ],自動完成中綴符(infix natation)包圍。

 


7.幫助文檔(Documentation and API References Help)

option+點按:查看選中符號的幫助提示(Quick Help for Selected Item)。
option+雙擊:打開選中符號的幫助文檔。

 


 

8.環境變量(Build Setting Macros

 

(1)查看環境變量宏

命令行進入HelloWorld工程目錄,執行xcodebuild命令並帶上“-showBuildSettings”參數:

 

iMac-Faner:HelloWorld faner$ xcodebuild -project HelloWorld.xcodeproj -target HelloWorld -configuration Debug -showBuildSettings > xcodebuild_showBuildSettings.txt

xcodebuild_showBuildSettings.txt中保存了Build settings for action build and target "HelloWorld”:,其中dump了所有的環境變量。

(2)Xcode5(macosx10.9)的部分環境變量

約定1:~=當前賬戶的HOME目錄,例如“/Users/faner”。

約定2:build構建基礎路徑BUILD_PATH = ~/Library/Developer/Xcode/DerivedData/Build。可通過“File->Project Settings”查看Derived Data Location

約定3:環境變量宏(Build Setting Macros)引用格式:${MACRO},同Build Phases Run Script中的語法。

下面是摘選自xcodebuild_showBuildSettings.txt的部分常用環境變量。

(a) ARCH & PLATFORM & SDK

ARCHS = i386

CURRENT_ARCH = i386


PLATFORM_DIR = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform

PLATFORM_NAME = macosx


SDKROOT = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk

SDK_DIR = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk

SDK_NAME = macosx10.9

(b) PROJECT & SOURCE

PROJECT = HelloWorld

PROJECT_DIR =~/Projects/Learn Objective-C/HelloWorld

PROJECT_FILE_PATH =${PROJECT_DIR}/HelloWorld.xcodeproj

PROJECT_NAME = HelloWorld


SOURCE_ROOT =${PROJECT_DIR}

SRCROOT =${PROJECT_DIR}

(c) BUILD & CONFIGURATION

BUILD_DIR =BUILD_PATH/Products

BUILD_ROOT =BUILD_PATH/Products

BUILT_PRODUCTS_DIR =BUILD_PATH/Products/Debug


CONFIGURATION = Debug

CONFIGURATION_BUILD_DIR =BUILD_PATH/Products/Debug

CONFIGURATION_TEMP_DIR =BUILD_PATH/Intermediates/HelloWorld.build/Debug

(d) PRODUCT & TARGET

PRODUCT_NAME = HelloWorld

PRODUCT_TYPE = com.apple.product-type.tool// Project Template: Command Line Tool


TARGET_BUILD_DIR =BUILD_PATH/Products/Debug

TARGET_NAME = HelloWorld


 

9.運行調試

 

command + B:構建(Buid)

command + R:運行(Run),可能會先編譯。若按下control直接運行上次build的product(Run Without Building)。

command + .:停止運行(Stop)


command + \:當前行設置/取消斷點;通過鼠標點擊藍色斷點來啓用/禁用當前行斷點。

 

command + Y:全局激活或禁用所有的斷點,激活進入調試模式(此時斷點藍色可見)。


編輯斷點(Edit Breakpoint):

 

Condition:設置斷點的觸發條件,例如“i==3”(注意不能有空格)表示當i等於3時該斷點纔會被觸發。

Ignore:設置斷點需要被忽略多少次纔會中斷,若設置成5則表示第6次遇到該斷點時才觸發。

Action:設置斷點觸發時的動作,可以爲Debugger Command、Log Message、Shell Command或Sound。

例如可設置以下Debugger Command:

(1)讀取std::string sig的內存buffer值:mem read sig.c_str() -c sig.size()

(2)打印NSData實例sig:po sig

 


F6:下一步(Step Over),逐過程單步調試,不進入函數體。

(fn+)F7:進入(Step Into)函數體。可能與多媒體鍵有衝突,故需要fn輔助。

(fn+)F8:跳出(Step Out)函數體。可能與多媒體鍵有衝突,例如呼叫iTunes,故需要fn輔助。

control+command+Y:逐斷點(continue)繼續執行。


shift+command+M:Debug Workflow->View Memory

command+K:Debug Workflow->Clear Console

Debug Workflow->ShowDisassembly When Debugging,可進行彙編指令級調試。


lldb調試命令

(1)n/next:step over;

(2)s/step:step into;

(3)finish:step out;

(4)c/continue:goto next breakpoint;

(5)p: print;

(6)expression:後接表達式,同p。

(7)memory read-- Read from the memory of the process being debugged.

dump指定地址的內存,後接起止地址或-c指定count加起始地址。可help mem read查看幫助:

Syntax

 

memory read <cmd-options> <address-expression> [<address-expression>]

 

 

Command Options Usage:

 

 

size指定內存塊(block/item)的大小,默認爲1byte。

 

    --size <byte-size> )The size in bytes to use when displaying with the selected format.

 

count指定內存塊(block/item)的個數,可配合起始地址使用。

 

    -c <count> ( --count <count> )The number of total items to display.

 

format指定內容顯示格式,格式符同print:c-char,s-string,d-decimal,x-hex。

 

    -f <format> ( --format <format> )Specify a format to be used for display.

 

 

Command Samples:

 

 

(a)起止地址

 

(lldb)mem read 0x10b88f0c 0x10b88f0c+9

 

0x10b88f0c: 39 38 37 36 35 34 33 32 31                       987654321

 

(b)起始地址+內存塊count

 

(lldb)mem read 0x10b88f0c -c 9

 

0x10b88f0c: 39 38 37 36 35 34 33 32 31                       987654321

 

(c)起始地址+內存塊size+內存塊count(dump hex format)

 

(lldb)memory read -s 1 -f x -c 9 0x10b88f0c

 

0x10b88f0c: 0x39 0x38 0x37 0x36 0x35 0x34 0x33 0x32

 

0x10b88f14: 0x31

 

(d)起始地址+內存塊size+內存塊count(dump char format)

 

(lldb)memory read -s 1 -f c -c 9 0x10b88f0c

 

0x10b88f0c: 987654321

 

(e)起始地址+內存塊size+內存塊count(dump string format)

 

(lldb)mem read 0x10b5cf2c -f s -c 1

 

0x10b88f0c: "987654321"

 

(f)起始地址+內存塊size+內存塊count(dump int format)

 

(lldb)memory read -s 4 -f x -c 3 0x10b88f0c

 

0x10b88f0c: 0x36373839 0x32333435 0x109f0031

 

(8)memory write-- Write to the memory of the process being debugged.

改寫指定地址的內存,可help mem write查看幫助。

 

Syntax: memory write <cmd-options> <address> <value> [<value> [...]]


10.設置NSZombieEnabled調試EXC_BAD_ACCESS

 

當你對已釋放的對象發送消息(90%的可能是對引用計數爲0的對象再release)或release那些autorelease對象時,就會出現報EXC_BAD_ACCESS這樣的錯誤。
默認設置下 Xcode不會給你定位具體是哪一行代碼不該去使用已釋放的對象,或者release用錯了。
Product -> EditSchemeoption+command+R -> Diagnostics ,勾選“Objective-C”之後的“Enable Zombie Objects”。
設置NSZombieEnabled環境變量後,一個對象銷燬時會被轉化爲_NSZombie;設置NSZombieEnabled後,當你向一個已經釋 放的對象發送消息,這個對象就不只是報EXC_BAD_ACCESS Crash,還會放出一個錯誤消息,然後以一種可預測的可以產生debug斷點的方式消失, 因此我們可以找到具體或者大概是哪個對象被錯誤的釋放或引用了。
注意:NSZombieEnabled只能在調試的時候使用,千萬不要忘記在產品發佈的時候去掉,因爲NSZombieEnabled不會真正去釋放dealloc對象的內存,一直開啓後果自負!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章