Phil John
Phil John

Reputation: 1237

How to log all methods used in iOS app

I'm taking over the development of an iPad app for a client. There's a substantial amount of work that's already been done and I'm trying to piece together how the whole thing is designed to run.

One of the things I'd like to do is log which methods get called when the app runs. I've seen a custom DTrace script that's meant to log all methods from startup, but when I run it in Instruments I get no results.

What's the best way of logging the methods?

Upvotes: 46

Views: 13791

Answers (8)

BitParser
BitParser

Reputation: 3968

If you want to log methods in the Simulator on 64 bit, use the following command instead:

expr -- (void) printf("[%s %s]\n", (char *)object_getClassName($rdi), (char *) $rsi)

Or if that doesn't work, log it this way:

enter image description here

The main idea is to use $rdi for the object (self), and $rsi for the selector.

Upvotes: 2

Zmicier Zaleznicenka
Zmicier Zaleznicenka

Reputation: 1964

To trace the app code under Xcode 6 on device, I had to use the following debugger expression.

expr -- (void) printf("[%s %s]\n", (char *)object_getClassName($arg1),$arg2)

Upvotes: 8

Jeff
Jeff

Reputation: 6707

If you want to limit the output to just the messages sent to one class you can add a condition like this

(int)strcmp((char*)object_getClassName($r0), "NSString")==0

Upvotes: 4

Collin
Collin

Reputation: 208

Brad Larson's approach can be adapted to run on the device by using the debugger command:

printf "[%s %s]\n", (char *)object_getClassName($r0),$r1

More information can be found in the Technical Note here: technotes

Upvotes: 5

warhammerkid
warhammerkid

Reputation: 955

If you are using LLDB, you will need to use the following debugger commands. These were tested in Xcode 4.6.

Device:

expr -- (void) printf("[%s %s]\n", (char *)object_getClassName($r0),$r1)

Simulator:

expr -- (void) printf("[%s %s]\n", (char *)object_getClassName(*(long*)($esp+4)), *(long *)($esp+8))

Upvotes: 19

Ali Kıran
Ali Kıran

Reputation: 1006

later xcode versions you need to call like that

expr -- (void)printf("[%s, %s]\n",(char *) object_getClassName(*(long*)($esp+4)), (char *) *(long *)($esp+8) )

Upvotes: 4

Brad Larson
Brad Larson

Reputation: 170317

Inspired by tc's answer to a similar question here, I put together a debug breakpoint action that will log out the class and method name for every time objc_msgSend() is triggered in your application. This works similarly to the DTrace script I described in this answer.

To enable this breakpoint action, create a new symbolic breakpoint (in Xcode 4, go to the breakpoint navigator and create a new symbolic breakpoint using the plus at the bottom left of the window). Have the symbol be objc_msgSend, set it to automatically continue after evaluating actions, and set the action to be a debugger command using the following:

printf "[%s %s]\n", (char *)object_getClassName(*(long*)($esp+4)),*(long *)($esp+8)

Your breakpoint should look something like the following:

Breakpoint action

This should log out messages like this when run against your application:

[UIApplication sharedApplication]
[UIApplication _isClassic]
[NSCFString getCString:maxLength:encoding:]
[UIApplication class]
[SLSMoleculeAppDelegate isSubclassOfClass:]
[SLSMoleculeAppDelegate initialize]

If you're wondering where I pulled the memory addresses, read this Phrack article on the Objective-C runtime internals. The memory addresses above will only work against the Simulator, so you might need to tweak this to run against applications on the iOS devices. Collin suggests the following modification in his answer to run this on a device:

printf "[%s %s]\n", (char *)object_getClassName($r0),$r1

Also, I think you'll see that logging out every method called in your application will overwhelm you with information. You might be able to use some conditions to filter this, but I don't know if this will help you to learn how your code executes.

Upvotes: 57

DenVog
DenVog

Reputation: 4286

A fellow developer taught me to add the same two log statements to each method. One as the first line, the other as the last line. I think he has a script that does this automatically for his projects, but the result is:

NSLog(@"<<< Entering %s >>>", __PRETTY_FUNCTION__);
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);

At the console, this will spit out something like:

 <<< Entering -[MainListTableViewController viewDidLoad] >>>

Very helpful in tracking what is going on.

Upvotes: 1

Related Questions