`

Xcode 调试与快捷键

    博客分类:
  • ios
 
阅读更多

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对象的内存,一直开启后果自负!

分享到:
评论

相关推荐

    XCode iOS 15.4 真机调试包

    使用方法: 将下载好的调试包解压,回到桌面 快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode...

    XCode iOS 14.5 真机调试包

    使用方法: 将下载好的调试包解压,回到桌面 快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode...

    PrettyPrintJSON:从Xcode调试控制台漂亮地打印JSON

    ##用法在控制台中突出显示JSON字符串,然后使用“ Edit &gt;“ Pretty Print JSON或键盘快捷键Ctrl + J 如果字符串格式正确,它将被附加到控制台底部。 如果发生解析错误,则会打印特定错误。 该插件使用XCode插件...

    XCode iOS 14.4 真机调试包

    使用方法: 将下载好的调试包解压,回到桌面 快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode...

    XCode iOS 15.2 真机调试包

    使用方法: 将下载好的调试包解压,回到桌面 快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode...

    XCode iOS 15.0 真机调试包

    使用方法: 将下载好的调试包解压,回到桌面 快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode...

    Xcode使用教程.doc

    讲述了编辑代码时的快捷键比如自动完成、查找、运行调试快捷键

    Xcode iOS 12.0-13.3 真机调试包.zip

    最全Xcode iOS 12.0-13.3 真机调试包 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 然后打开访达-按快捷键command+shift+G 粘贴进去前往

    ios13 真机调试包下载

    将下载好的调试包解压,然后打开Finder(访达)窗口,前往文件夹 ,(快捷键command⌘+shift⇧+g也可以,反正我记不住。。。。) 输入:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/...

    iOS17.0真机调试包

    备注:Xcode运行iOS17.0的包进行真机调试会在启动页卡住很久才能进入到主页面, 找了相关资料说去掉Debug executabe能解决但是没了断点功能,各位自行取舍吧。 路径:Product -&gt; Scheme -&gt; Edit scheme... -&gt; Run ...

    ios17.1真机调试包

    备注:Xcode运行iOS17.1的包进行真机调试会在启动页卡住很久才能进入到主页面,找了相关资料说去掉Debug executabe能解决但是没了断点功能,各位自行取舍吧。路径:Product -&gt; Scheme -&gt; Edit scheme... -&gt; Run Debug...

    xcode-ios 12.1真机包

    使用方法: 将下载好的调试包解压,然后打开Finder(访达)窗口,前往文件夹 ,(快捷键command⌘+shift⇧+g也可以,反正我记不住。。。。) 输入:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS....

    xcode ios 12.1真机包

    将下载好的调试包解压,然后打开Finder(访达)窗口,前往文件夹 ,(快捷键command⌘+shift⇧+g也可以,反正我记不住。。。。) 输入:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/...

    Xcode使用教程详细讲解(全)

    Xcode使用教程详细讲解是本文要介绍的内容,Xcode是一个款强大的IDE开发环境,就像你在写Windows程序时需要VS2005一样 需要要Xcode为你写Mac程序提供环境。因此,如果你要成为Mac 程序的开发者,灵活运用Xcode工具是...

    iOS17.2真机调试包

    备注:Xcode运行iOS17.2的包进行真机调试会在启动页卡住很久才能进入到主页面,找了相关资料说去掉Debug executabe能解决但是没了断点功能,各位自行取舍吧。路径:Product -&gt; Scheme -&gt; Edit scheme... -&gt; Run ...

    xcode ios 12.0真机包

    将下载好的调试包解压,然后打开Finder(访达)窗口,前往文件夹 ,(快捷键command⌘+shift⇧+g也可以,反正我记不住。。。。) 输入:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/...

    IOS 15.4 真机调试包

    打开Finder 按下快捷键:command + shift + G 真机调试包路径:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 放入文件 重启Xcode

    ios13.3 (17C54)真机调试包

    ios13.3真机调试包 ,.xcode真机调试包路径:点击finder,然后快捷键command + shift + G 前往文件夹路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 将更新包解压进去...

    iOS 16.4真机开发包

    将下载好的调试包解压,快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode,然后重新启动即可 ...

    iOS 16.1 真机开发包 正式版

    将下载好的调试包解压,快捷键command+shift+g前往文件夹: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件放入到该目录下,关闭Xcode,然后重新启动即可 ...

Global site tag (gtag.js) - Google Analytics