xcode lldb 错误:变量不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13040777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
lldb error: variable not available
提问by ari gold
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 sometimesit returns NULL, tho I can't, as of now, figure out why.
对于它的价值,我正在尝试调试第一行,因为有时它返回 NULL,但我现在无法弄清楚原因。
回答by Jason Molenda
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 编译器进行构建 - 一直在努力改进优化代码调试,并且您希望充分利用自己到目前为止,您可以使用工具。
回答by ff10
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. As @carlos_ms has pointed out here, a temporary solution is to define the variable as mutable, i.e.
在 Swift 中,可能从 Xcode 9 开始,但在 Xcode 10 中仍然存在问题,甚至在构建设置中关闭代码优化时也会出现这种情况。正如@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()
可能会帮助你。