Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 881
ari gold Avatar asked Oct 23 '12 23:10

ari gold


3 Answers

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.

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.

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.

like image 63
Jason Molenda Avatar answered Sep 29 '22 09:09

Jason Molenda


The "Address Sanitizer" in the diagnostics seems to make the values of variables unavailable, too.

Schemes > Run > Diagnostics > Address Sanitizer

like image 43
hEADcRASH Avatar answered Sep 29 '22 11:09

hEADcRASH


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.

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.

like image 36
ff10 Avatar answered Sep 29 '22 10:09

ff10