Monday, September 10, 2012

LLDB Custom Object Summaries Implemented Using Python, not Objective-C

In the seemingly benignly titled WWDC 2012 session Debugging in Xcode, the speaker team shows you how to implement custom summaries on your own objects for LLDB. This is pretty important since LLDB is now the recommended default debugger as of Xcode 4.3 (Xcode 4.4.1 is the latest at the time of this writing, with Xcode 4.5 just days away).

What a second, what's wrong with implementing [ClassInstance description]? The speaker sternly says it's an action fraught with danger since you might run code and change the state of the object inside description. I admit, I never really thought about it much. But one of the side affects calling description caused was seeing the dreaded Summary Unavailable next to your variable where its description should be. When implementing LLDB, Apple choose another way to get custom summaries which really surprised me.

For LLDB, you create a Python script that directly inspects member variables of your class, then you use Python code to do any summarization, formatting, or transformation, and finally return a Python string. The video shows exactly how this works, and of course you need to register a script and function in the script to call when LLDB sees your Objective-C class.

I'd never seen Python before. It doesn't look radically hard to pick up, and seeing it used for LLDB sure made me wonder if it will have a more prominent role at Apple in the future. Things that make you go hmmmm…