Xcode高级调试技巧4
2015-12-11 15:33
363 查看
Have you ever been stuck trying to understand your code and logged the contents of a variable?
Or skipped a function call to simplify the behavior of the program?
Or short-circuited a logical check?
Or faked the implementation of a function?
And had to recompile, and start over each time?
Building software is complicated and bugs will always appear. A common fix cycle is to modify the code, compile, run again, and wish for the best.
It doesn’t have to be that way. You can use the debugger! And even if you already know how to inspect values, there is a lot more it is capable of.
This article intends to challenge your knowledge of debugging, explain the basics in a bit more detail than you likely know, and then show you a collection of fun examples. Let’s take it for a spin and see where we end up.
LLDB is
an open-source debugger
that features a REPL, along with C++ and Python plugins. It comes bundled inside Xcode and lives in the console at the bottom of the window. A debugger allows you to pause a program at a specific moment of its execution, inspect the values of variables, execute
custom instructions, and then manipulate the advancement of the program as you see fit. (Here is
one explanation of how debuggers work in general.)
It’s likely that you have used a debugger before, even if only in Xcode’s UI to add breakpoints. But with a few tricks, there are some pretty cool things that you can do. The GDB
to LLDB reference is a great bird’s-eye view of the available commands, and you might also want to install Chisel,
an open-source collection of LLDB plugins that make debugging even more fun!
In the meantime, let’s begin our journey and start with how to print variables in the debugger.
Here is a small, simple program that logs a string. Notice that a breakpoint has been added on line 8, which was made by clicking in the gutter in the source view in Xcode:
The program will pause its execution at that line and the console will open, allowing us to interact with the debugger. What shall we type?
The easiest command to try is
which will list all the commands. And if you ever forget what a command does or want to know more, then you can read all the details with
does, then you can try
SELECT ALL[code]NSLog(@"%@", whatIsInsideThisThing);
Or skipped a function call to simplify the behavior of the program?
SELECT ALL[code]NSNumber *n = @7; // theFunctionThatShouldReallyBeCalled();
Or short-circuited a logical check?
SELECT ALL[code]if (1 || theBooleanAtStake) { ... }
Or faked the implementation of a function?
SELECT ALL[code]int calculateTheTrickyValue { return 9; /* Figure this out later. ... }
And had to recompile, and start over each time?
Building software is complicated and bugs will always appear. A common fix cycle is to modify the code, compile, run again, and wish for the best.
It doesn’t have to be that way. You can use the debugger! And even if you already know how to inspect values, there is a lot more it is capable of.
This article intends to challenge your knowledge of debugging, explain the basics in a bit more detail than you likely know, and then show you a collection of fun examples. Let’s take it for a spin and see where we end up.
LLDB
LLDB isan open-source debugger
that features a REPL, along with C++ and Python plugins. It comes bundled inside Xcode and lives in the console at the bottom of the window. A debugger allows you to pause a program at a specific moment of its execution, inspect the values of variables, execute
custom instructions, and then manipulate the advancement of the program as you see fit. (Here is
one explanation of how debuggers work in general.)
It’s likely that you have used a debugger before, even if only in Xcode’s UI to add breakpoints. But with a few tricks, there are some pretty cool things that you can do. The GDB
to LLDB reference is a great bird’s-eye view of the available commands, and you might also want to install Chisel,
an open-source collection of LLDB plugins that make debugging even more fun!
In the meantime, let’s begin our journey and start with how to print variables in the debugger.
The Basics
Here is a small, simple program that logs a string. Notice that a breakpoint has been added on line 8, which was made by clicking in the gutter in the source view in Xcode:The program will pause its execution at that line and the console will open, allowing us to interact with the debugger. What shall we type?
help
The easiest command to try is help,
which will list all the commands. And if you ever forget what a command does or want to know more, then you can read all the details with
help <command>, e.g.
help printor
help thread. If you ever forget what the
helpcommand
does, then you can try
help help, but if you know enough to do that, then maybe you haven’t entirely forgotten what the command does after all.
相关文章推荐
- 数据结构与内存中的堆与栈
- 如果查看应用性能图表是一种信仰
- android学习--使用adb工具管理sqlite3数据库
- Android 自定义imageview图片高度固定大小宽度按比例自适应
- Ajax跨域、Json跨域、Socket跨域和Canvas跨域等同源策略限制的解决方法
- java实现sms短信发送
- [Canvas绘图] 第25节 藏图阁(10) 厨房电器
- IOS-41-NSTimer定时器的使用
- 实现图片放大缩小,点击热点响应事件
- SVN导出/导入、SVN备份/还原 【小白版】
- Android 实现两屏幕互相滑动
- pom.xml
- Setting up a Passive FTP Server in Windows Azure VM(ReplyCode: 227, Entering Passive Mode )
- 算法:阿克曼函数(C++)
- 简单之微文件系统
- Struts2全局拦截器
- 将json转换成bean
- iOS 开发技巧
- shareSdk打包报错解决办法
- 透过汇编另眼看世界之DLL导出函数调用