In Objective-C, is there a way to get a list of methods called by a method?

I did some research online and found that with a package ObjectiveC

in Objective C you can get a list of all methods in a class using class_copyMethodList()

, and I see that you can get the implementation ( IMP

) of a method using instanceMethodForSelector:

. Apple's documentation here has been helpful so far, but I'm stuck and not sure what I'm really looking to find.

I need a list of methods / functions to be called in the implementation of a given method , so I can build a call tree.

Any suggestions? Thanks in advance!

+3


source to share


2 answers


This solution is quite tricky and will call a line of code in each method. You can also use sqlite and keep tracked methods.

MethodTracker.h

@interface MethodTracker : NSObject

@property (nonatomic) NSMutableArray *methodTrackArr;

+ (MethodTracker *)sharedVariables;

@end

      

MethodTracker.m

@implementation MethodTracker

static id _instance = nil;

+ (MethodTracker *)sharedVariables
{
    if (!_instance)
        _instance = [[super  allocWithZone:nil] init];

    return _instance;
}

// optional
- (void)addMethod:(NSString *)stringedMethod
{
    // or maybe filter by: -containObject to avoid reoccurance
    [self.methodTrackArr addObject:stringedMethod];
    NSLog("current called methods: %@", methodTrackArr);
}

@end

      

and using it like:



OtherClass.m

- (void)voidDidLoad
{
    [super viewDidLoad];
    [[MethodTracker sharedVariables] addMethod:[NSString stringWithUTF8String:__FUNCTION__]];
    // or directly
    [[MethodTracker sharedVariables].methodTrackArr addObject:[NSString stringWithUTF8String:__FUNCTION__]];
}
- (void)someOtherMethod
{
    // and you need to add this in every method you have (-_-).. 
    [[MethodTracker sharedVariables] addMethod:[NSString stringWithUTF8String:__FUNCTION__]];
}

      

I suggest you import this one MethodTracker.h

inside [ProjectName]-Prefix.pch

.

Sorry , for a double answer I deleted the other one and I don't know how it happened ..

Hope this helped you or at least gave you an idea .. Happy coding, Hooray!

0


source


I think at runtime the track method

is possible, but function

not.

I created a DaiMethodTracing tool to track all activity methods in one class for some of my needs. This is based on the objective-c swizzling method. So there is an idea to do this



  • List of all classes in your application.
  • swizze all methods in every class.
  • filter out the method you want to track.

Finally, you can get the call path method

.

0


source







All Articles