您的位置:首页 > 其它

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?
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 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.


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
print
or
help
thread
. If you ever forget what the
help
command
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: