Search code examples
objective-ccocoaxcode4gdb

print-object in Xcode4 not behaving as expected


I keep seeing references to being able to execute code from the debugger, but it never works for me. For example,

(gdb) po [NSNumber numberWithBool:NO]

gives me "No symbol "NSNumber" in current context.". Is there a setting I need to change? As far as I know I have a completely normal, uncustomized copy of Xcode 4.0.2 (I'm still on Snow Leopard so can't upgrade to 4.1).

Update - I tried it in 4.1 on my laptop - exactly the same error.

Update 2 - a friend figured out that if you add a method to any class in the project that returns an NSNumber then the above command works in GDB, even if the method is never called. Also it seems to be any and all class methods, not just NSNumber. Instance methods seem to work ok.

Can anyone explain what is going on here, and is this expected behavior or a bug?

BTW, if you are wondering why I would want to print that, the back story is here: http://objectivistc.tumblr.com/post/8992822737/a-chocking-mystery-po-nsnumber-numberwithbool-no


Solution

  • It looks like GDB optimises its symbol lookup table so that symbols that are not referenced in code are not loaded, which is why your friend in Update 2 was able to use NSNumber.

    One simple fix is to replace occurrences of class names with the corresponding NSClassFromString() calls, e.g. replace NSNumber with NSClassFromString(@"NSNumber"):

    po [NSClassFromString(@"NSNumber") numberWithBool:NO]