ryyst
ryyst

Reputation: 9791

Objective-C - print method parameters with GDB

I'm struggling to debug my Objective-C program with GDB. I have a function - (NSString *)reverse:(NSString *)someString which I want to debug.

Here's how I set the breakpoint:

(gdb) break -[MyClass reverse:]

Now, when the code gets to the breakpoint, how do I print the addresses or even better the values of self and the method argument? I've done some googling and found suggestions like po $rdx but nothing I found works.

How can I solve this?

Upvotes: 3

Views: 3011

Answers (3)

logancautrell
logancautrell

Reputation: 8772

Implement the description method in your class. You can format the values however you like. From the docs:

The debugger’s print-object command indirectly invokes this method to produce a textual description of an object.

Upvotes: 1

Jesse Black
Jesse Black

Reputation: 7976

When debugging with gdb you can print with po and print ()

po self
po someString


print (int) myInt

po acts like NSLog(@"%@", self); print () acts like NSLog(@"%d", myInt);

*you can print more types than int

Upvotes: 3

Rob Napier
Rob Napier

Reputation: 299355

Clark Cox has written the best explanation of this I've ever found. I refer to this page all the time and have made a local copy in case it ever goes away.

http://www.clarkcox.com/blog/2009/02/04/inspecting-obj-c-parameters-in-gdb/

The quick version for x86_64 and non-floating-point parameters is:

first ObjC arg => $rdx
second ObjC arg => $rcx
third ObjC arg => $r8
fourth ObjC arg => $r9

Remember, the first two things passed to a method (in $rdi and $rsi) are self and _cmd. I'm not counting those here.

If you're passing floating points, structs, or more than four arguments, things get more complicated, and you should read the calling conventions in the AMD64 ABI section 3.2.3. If you're dealing with i386, PPC, or ARM, see Clark's post, which covers those cases well for the common cases.

Upvotes: 13

Related Questions