http://lists.apple.com/archives/Cocoa-dev/2006/Mar/msg00058.html
Aleaxander Cohen wrote:
> Once in a while, my application breaks in the debugger on a
> 'objc_msgsend', is there any way to find out what object is being
> passed to this function so i can understand what is going on in my
> app?
This isn't easy on i386, because the exact location changes depending on how far inside objc_msgSend() you got.
This procedure works well, though it's not fully automatic:
1. Run `x/s $ecx`. This should print the name and address of the selector. If it doesn't, you're too far inside objc_msgSend() to get an easy answer.
3. Run `x/8x $esp`. This is the top 8 words of the stack.
4. Look for the selector address (from step 1) in the stack contents. The word just before the selector address is the receiver object's address. The method's other arguments, if any, start after the selector.
If step #1 doesn't work, check whether you're stopped at the very first instruction of objc_msgSend(). If so, step forward one instruction (`si`) and try again. That first instruction is the one that moves the selector into $ecx.
Example:
0x9ff57eef in objc_msgSend ()
(gdb) x/s $ecx
0x9ffcb230 <_errNotSuper+412640>: "sharedSpellChecker"
// The selector is "sharedSpellChecker", and its address is 0x9ffcb230
(gdb) x/8x $esp
0xbfffee34: 0xbfffee88 0x003539b0 0x93624629 0xa34ab0c0
0xbfffee44: 0x9ffcb230 0xbfffef18 0x9ff57f36 0xa34ac480
// The selector address is the 5th word on the stack, so the receiver is 0xa34ab0c0
(gdb) p (char *)object_getClassName(0xa34ab0c0)
$1 = 0x932aa1750 "NSSpellChecker"
// The receiver is either class NSSpellChecker or one of its instances. This case happens to be the class itself, for + [NSSpellChecker sharedSpellChecker]
Eric Albert wrote:
> Printing arguments on Intel really isn't all that hard. On
> PowerPC, you'd print $r3, $r4, $r5, etc. On Intel, it's $ebp + 8,
> $ebp + 12, $ebp + 16, etc.
This doesn't work for objc_msgSend(). Ordinary functions set up $ebp as the frame pointer, but objc_msgSend() usually doesn't do that, for performance reasons.
--
Greg Parker email@hidden Runtime Wrangler