Xcode高级调试技巧6

这篇教程介绍了如何利用LLDB,Xcode内置的高性能调试器,来调试iOS和watchOS应用程序。内容涵盖LLDB命令行使用、在Xcode中暂停并执行代码、导入模块、设置全局变量以及结合断点使用LLDB进行更复杂的调试操作。此外,还提到了使用LLDB命令在运行时注入代码以改变应用行为,以及如何与其他开发者共享带有断点动作的项目。
摘要由CSDN通过智能技术生成

LLDB (http://lldb.llvm.org), described on the official webpage as a next generation, high-performance debugger, is the debugger that’s currently shipped with Xcode. Just a few years ago, LLDB replaced gdb, and now LLDB is the only debugger supported by Xcode. In this app developer tutorial, I will show you how to use the LLDB debugger and theexpression command when developing iOS and watchOS apps.

You can run LLDB from the Terminal typing: xcrun lldb. The xcrun command points the Terminal to the LLDB version shipped with Xcode (the Xcode bundle contains tons of tools). If you omit the xcrun command, the Terminal executes LLDB installed in the /usr/bin/ directory, which could contain a different version than the LLDB used by Xcode.

Unfortunately, executing LLDB from the Terminal app is only useful to debug apps that are not sandboxed. If you want to debug an iOS or watchOS app, you need to use LLDB from the Xcode console when executing the app with the Apple IDE. After compiling and running an app, you can access the LLDB console at any time by pressing the Pause button in the Xcode UI (see next figure).

LLDB

The execution of the app is paused and LLDB attaches to it. At this point, you can type LLDB commands in the console, as you can see in the following picture.

LLDB Console

You can learn more about the different LLDB commands by typing help in the console or reading the documentation provided with Xcode. Another valuable resource is the previously mentioned LLDB webpage (http://lldb.llvm.org). If you want to know more about a particular command, just type help in the console followed by the command name, and LLDB will print out the help for that particular command.

One of the LLDB commands is expression and in this post, I will show you how to use expression to debug Swift or Objective-C applications by injecting code at runtime and interacting with the objects of your app. You can also useexpression in combination with the Xcode breakpoints, which I will show you later.

Evaluating Swift or Objective-C code

If you type help expression in the console, you get the following description:

As you can see, you can evaluate Objective-C++ and Swift expressions, using user defined variables and also variables within the scope. So let’s try to use the expression command. First of all, if you don’t need any of the options provided by expression, you can use the short form p followed by the <expr> that you want LLDB to evaluate. So, instead of writing:

you can simply use:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值