Greg
Greg

Reputation: 33650

How do I print the result of an objective-c class method in gdb?

When using gdb (via the debug console) to debug an iPad program in Xcode 4, I'm trying to print out the result of running a class method:

(gdb) po [MyClass foo:@"bar"]

gdb outputs the following:

No symbol "MyClass" in current context.

Is there some way to print the result of +(NSString *)foo:(NSString *)string using gdb in Xcode 4?

Upvotes: 7

Views: 785

Answers (2)

Besi
Besi

Reputation: 22939

I had the same problem here. The solution in my case was to use NSClassFromString like this:

po [NSClassFromString(@"MyClass") foo:@"bar"]

Upvotes: 11

Joe
Joe

Reputation: 57169

The problem is you have not declared anything of type MyClass in your targets source. If your MyClass is only designed to have static methods you can try something like

#if DEBUG //gdb Static Method Fix
    MyClass *mc = nil;  //This makes the symbol available
    [mc class];         //supress unused warning
#endif

My guess is that by not declaring a type of the class anywhere in your code it has been optimized out of the lookup symbols. From my testing that call above does not even have to be called for it to work. If you look at printcmd.c of gdb line # 1250 this is where the error is printed from and this occurs after a call to lookup_minimal_symbol. And although gdb is unable to find the symbol in context it is still fine to only use static methods of MyClass in your source code without the fix above.

Upvotes: 6

Related Questions