Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 866
Greg Avatar asked Apr 25 '11 21:04

Greg


2 Answers

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

po [NSClassFromString(@"MyClass") foo:@"bar"]
like image 80
Besi Avatar answered Oct 30 '22 23:10

Besi


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.

like image 25
Joe Avatar answered Oct 30 '22 22:10

Joe