Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Objective-C method implementation at runtime

Tags:

objective-c

Is it possible to print/log the implementation of a certain class method at runtime to the console screen? I am assuming the log will be in assembly which is fine by me.

like image 243
Tarek Avatar asked Aug 27 '13 14:08

Tarek


1 Answers

You could add a breakpoint at the start of the line, step through line by line and call "disassemble" in the debugger:

One line of my code (with private information replaced) for example produced this:

-(void) method
{
    __weak typeof(self) selfReference = self; // <-- This call was disassembled.
    ...

Project`-[Class method] + 32 at Class.m:176:
-> 0x9c5cc:  ldr    r1, [sp, #304]
0x9c5ce:  add    r0, sp, #296
0x9c5d0:  blx    0x33abec                  ; symbol stub for: objc_initWeak
0x9c5d4:  ldr    r1, [sp, #304]

Edit

I can't verify it's working perfectly since I'm not too handy with assembly, but you can use the debugger (Clang I'm using) to just call

disassemble -n methodName

This claims to

Disassemble entire contents of the given function name.

NB: I did this with a breakpoint at the start of the method I was using to test

Try creating a symbolic breakpoint to stop at the method in question:

enter image description here

like image 94
James Webster Avatar answered Nov 10 '22 11:11

James Webster