LLDB调试

Have you ever been stuck trying to understand your code and logged the contents of a variable?

NSLog(@"%@", whatIsInsideThisThing);

Or skipped a function call to simplify the behavior of the program?

NSNumber *n = @7; // theFunctionThatShouldReallyBeCalled();

Or short-circuited a logical check?

if (1 || theBooleanAtStake) { ... }

Or faked the implementation of a function?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值