Since my Twitter complaint about gdb under Xcode suddenly refusing to let me examine symbols, I’ve gotten a lot of search hits on my site for the “unable to access variable xcode” key phrase for my post titled “Unrolled Loops, the Debugger, & YOU“. I figured I’d share a more complete post on the subject of buggered debuggers for the most common causes.

The Symptoms

You might see an “unable to access variable” message when using “po” (“print object”) in the debugger console or by hovering over a symbol. Some objects may show nil ( 0×0 ) if you print them or hover over them as well. Even an array might show as non-nil but with zero elements. You might see other messages (which have slipped my mind) as well, but in general Xcode’s helpful GUI debugging elements and even gdb’s console suddenly become most decidedly unhelpful.

The Cause

The cause is simple: either there are no debugging symbols in your app at the time of debugging or you’ve optimized something somewhere in such a way that the symbols are either gone or don’t make sense.

Possible Cures

There’s no universal checkbox that’ll fix this problem for everyone, but there are a limited number of things to check.

Release Mode

By far the most common cause is you’re trying to debug a release build (whose debugging symbols are stripped down to nothing or close to). The resolution for this should be obvious, but in case it’s not, switch to debug, rebuild, then try debugging again.

Project/Target Info Settings

In Xcode 3, project and target build information is complex (I have it on good authority that it’s going to get a bit simpler soonish). There are project- and target-level settings per configuration (as in “Debug” or “Release”). Make sure at all levels under “Debug” configuration that the following conditions are met:

  • “Strip Debug Symbols During Copy” is unchecked.
  • “Generate Debug Symbols” is checked.
  • “Optimization Level” is set to “None [-O0]“.
  • “Use Separate Strip” is unchecked.
  • “Additional Strip Flags” has nothing set (I don’t think this matters if strip isn’t run).

Basically you want all debug symbols, no optimization, and no stripping (your beta code isn’t mature, and minors shouldn’t be strippers anyway).

Per-File Compiler Flags

This is what bit me. If you “Get Info” on an individual file (like Foo.m) and switch to the Build tab, you’ll notice the largish text box wherein you can place “Additional Compiler Flags for Target…”. Make sure there are no flags here that have anything to do with optimization. In my case, I chose “-O3 -funroll-loops” and this was my problem. Xcode 3 gives you no way to set these per configuration (ie, only use these flags in Release mode). This option was causing loop debugging to become loopy based on how the concept of “loop unrolling” (or unwinding) works.

Conclusion

So … I hope that helps. Feel free to add any other conditions (or corrections) via comments.

 

I’ve just completed a 16-hour (off and on) “what the fuck?!” session with Xcode (3 and 4). Specifically, when debugging a specific class, gdb kept telling me, “Unable to access variable…” any time I’d “po” a variable or hover over the symbol in the editor. I blamed plenty of different possible culprits.

I didn’t remember having changed any of the target’s settings, but I tried playing with various combinations of compiler, debugging symbol levels, etc. to no avail. Because I’ve been testing Xcode 4 heavily, I then assumed the problem had something to do with having 3 and 4 installed alongside each other. To test this, I tried simply reinstalling 3. No luck. Uninstalled 4 and 3, reinstalled 3. Still no luck.

Frustrated beyond all fucking reason, I started stepping through every single call that lead up to the problem area. An interesting pattern emerged. Everything worked fine (po and hover-over-symbol) until I’d get inside a loop in certain files. Now it took awhile for me to notice this because of its apparent intermittence. Then it hit me. Those were files I … er … optimized.

Long story short: the “unroll loops” compiler flag and the debugger don’t get along very well.

I had forgotten that for some of the “loopier” classes I’d set this complier flag and gained measurable performance increases. I did this by clicking the file, then hitting Cmd-I for the file’s info. Under the build tab, I added this:

-funroll-loops

So once I removed this, magically the debugger (and Xcode’s editor) began providing helpful information once again. As I said on Twitter, it’s quite a stupid thing to lose two days over.

(sigh)

Update: For other causes and cures for debugger problems, see my post titled “Buggered Debugger“.

© 2011 Joshua NozziJoshua Nozzi is a Cocoa developer for hire.Suffusion theme by Sayontan Sinha