Getting the type of an argument in a method

I am trying to subclass a specific class and override some of its private methods. This is dangerous, it may fail, it cannot pass the Apple review (in the AppStore), it can have terrible effects, etc., but this is only for experimental / educational purposes :)

For example, let's say I want to know the type of the first argument of an instance method keyboardInput:shouldInsertText:isMarkedText: UITextView:

SEL selector = @selector(keyboardInput:shouldInsertText:isMarkedText:);
Class class = [UITextView class];
Method method = class_getInstanceMethod(class, selector);
char *arg = method_copyArgumentType(method, 0);
printf("_%s_\n", arg);
free(arg);

However, on the console, I only get _@_. I think what the @object means, but what class object? (I thought I was getting the class name of the argument). Is it possible to get the class of this parameter using other objective-c runtime functions or any other value?

PS: In the example, I used the Cocoa touch class, but I could try the same with the Cocoa class. Therefore, it should not be specific to iOS.

SOLVE:

I tried in the simulator what Dave suggested, it worked!

(gdb) info all-registers gave me a long list of values, ...

((gdb) po *(id*)($ebp + 8)
<MyTextView: 0x5911270; baseClass = UITextView; frame = (80 70; 240 323); text = 'Lorem ipsum dolor sit er ...'; clipsToBounds = YES; autoresize = RM+BM; layer = <CALayer: 0x5c0c7d0>; contentOffset: {0, 0}>

(gdb) p *(SEL*)($ebp + 12)
$5 = (SEL) 0xbd19

(gdb) po *(id*)($ebp + 16)
<UIWebDocumentView: 0xa02f000; frame = (0 0; 240 457); text = 'Lorem ipsum dolor sit er ...'; opaque = NO; userInteractionEnabled = NO; layer = <UIWebLayer: 0x5c35070>>

(gdb) po *(id*)($ebp + 20)
t

(gdb) p *(id*)($ebp + 24)
$6 = (id) 0x0

Which makes sense since the key I pressed was the only "t", so it was not marked with text ("0x0"), so I'm fine to think that the first argument must be of type UIWebDocumentView.

Just one little thing (in this case it does not matter), how can I get the method name from SEL to gbd? for example $5?

+5
source share
3 answers

Assuming this is only for academic interests ....

, , - GDB . , (.. float vs object ..), p . ​​

, :

  • gdb b -[UITextView keyboardInput:shouldInsertText:isMarkedText:]
  • Xcode -[UITextView keyboardInput:shouldInsertText:isMarkedText:]

, (). , , , .

, , . - info all-registers, ..


, , , * char* s:

p (char*)$5

:

${some number} = 0x{some address} "keyboardInput:shouldInsertText:isMarkedText:"
+6

. id Objective-C, . -class. , , swizzle , , . Google " swizzling" , Objective-C.

+9

@ "", , . __OBJC , , , .

.:)

0

All Articles