简体   繁体   English

lldb 错误:变量不可用

[英]lldb error: variable not available

Here are my two lines of code:这是我的两行代码:

NSString *frontFilePath = [[NSBundle mainBundle] pathForResource:[self.bookendFileNames objectAtIndex:self.randomIndex] ofType:@"caf"];
NSLog(@"frontFilePath = %@", frontFilePath );

I put a break point on the second line and when there, I try to print it:我在第二行放了一个断点,当在那里时,我尝试打印它:

(lldb) po frontFilePath

But I get the following error:但我收到以下错误:

error: variable not available

I'm confused because if I step over the NSLog statement, the variable does indeed print to the console.我很困惑,因为如果我跳过 NSLog 语句,变量确实会打印到控制台。

For what it's worth, I'm trying to debug the first line since sometimes it returns NULL, tho I can't, as of now, figure out why.对于它的价值,我正在尝试调试第一行,因为有时它返回 NULL,但我现在无法弄清楚原因。

This is an artifact of debugging optimized code.这是调试优化代码的神器。 When the compiler's optimization is enabled in your build settings, it moves variables between memory and registers as it decides is best.当编译器的优化在您的构建设置中启用时,它会在内存和寄存器之间移动变量,因为它认为是最好的。 At the point where you're examining the variable in lldb, it may not exist in registers or memory at all -- even though it looks like it should still be available for display.在您检查 lldb 中的变量时,它可能根本不存在于寄存器或内存中——即使它看起来应该仍可用于显示。

It's possible it is a shortcoming of the debug information output by the compiler.可能是编译器输出的调试信息存在缺陷。 Sometimes the compiler will copy a variable into a register for its use and only list that register location in the debug information.有时,编译器会将变量复制到寄存器中供其使用,并且仅在调试信息中列出该寄存器的位置。 Later the register is repurposed for other uses;后来寄存器被重新用于其他用途; value is still present on the stack but the compiler hasn't told the debugger that the value can be found there. value 仍然存在于堆栈中,但编译器没有告诉调试器可以在那里找到该值。

The only way to really tell whether it's insufficient debug info or if the value genuinely doesn't exist at that particular instruction is to examine the assembly code by hand.真正判断它是调试信息不​​足还是该特定指令中的值确实不存在的唯一方法是手动检查汇编代码。 As soon as you turn on optimization with the compiler, the source code becomes a weak view into what's actually being executed in any particular order.一旦您使用编译器打开优化,源代码就会成为以任何特定顺序实际执行的内容的弱视图。

Instead of wandering too far into the wacky world of optimized code debugging, I strongly recommend turning off optimization (Optimization Level in your Build Settings) for your build and debugging it that way, if at all possible.如果可能的话,我强烈建议关闭优化(构建设置中的优化级别)并以这种方式调试,而不是在优化代码调试的古怪世界中徘徊太远。 If you do need to debug your app with optimization, make sure you're building with the latest Apple LLVM compiler supported by your Xcode -- there is always work being done to improve optimized code debugging and you want to avail yourself of the most up to date tools you can.如果您确实需要通过优化调试应用程序,请确保您使用 Xcode 支持的最新 Apple LLVM 编译器进行构建 - 一直在努力改进优化代码调试,并且您希望充分利用自己到目前为止,您可以使用工具。

In Swift, possibly starting with Xcode 9 and still an issue in Xcode 10, this could even come up when code optimization is turned off in the build settings.在 Swift 中,可能从 Xcode 9 开始,但在 Xcode 10 中仍然存在问题,甚至在构建设置中关闭代码优化时也会出现这种情况。 As @carlos_ms has pointed out here, a temporary solution is to define the variable as mutable, ie正如@carlos_ms 在这里指出的,临时解决方案是将变量定义为可变的,即

Turn转动

let foo = Bar().string

into进入

var foo = Bar().string

in order to cause optimization to skip on this variable.为了使优化跳过此变量。 Note that this might not work in all instances.请注意,这可能不适用于所有情况。

In this case, a good ol' debugPrint() might help you out.在这种情况下,一个好的 ol' debugPrint()可能会帮助你。

The "Address Sanitizer" in the diagnostics seems to make the values of variables unavailable, too.诊断中的“Address Sanitizer”似乎也使变量的值不可用。

Schemes > Run > Diagnostics > Address Sanitizer方案 > 运行 > 诊断 > 地址清理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM