Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log Objective-c message sends on a device

When running an iOS app in the simulator, there's an environment variable NSObjCMessageLoggingEnabled that causes all objc_msgSend calls to be logged out to a file. (Detailed explanation).

I'm trying to trace out all the messages that are being sent in my app, but I can't run it in the simulator. It has to be on a physical device because I'm interacting with hardware through the lightning connector. For the same reason, I can't attach a debugger to the process.

It would be helpful to write a log to disk like the one created with NSObjCMessageLoggingEnabled, but write it locally in my app's Documents directory, or similar.

Is this possible?

like image 963
funroll Avatar asked Oct 01 '22 00:10

funroll


1 Answers

All NSObjCMessageLoggingEnabled does is cause CoreFoundation to automatically call instrumentObjcMessageSends at startup and shutdown.

The problem is that Apple has intentionally hidden that function in the native iOS SDK, so you can't just call it.

But it still exists in the dylib at runtime, so you can always do this:

#include <dlfcn.h>

typedef void functype(BOOL);
void *libobjc = dlopen("/usr/lib/libobjc.dylib", RTLD_LAZY);
functype *instrumentObjcMessageSends = dlsym(libobjc, "instrumentObjcMessageSends");
if (!instrumentObjcMessageSends) {
    NSLog(@"Couldn't get instrumentObjcMessageSends");
    exit(1);
}
instrumentObjcMessageSends(YES);
NSLog(@"Did it");

I have no idea where, if anywhere, the logs are being written to. I assume you're going to want to call logObjcMessageSends to register your own ObjCLogProc, as explained in the post you linked to.

like image 75
abarnert Avatar answered Oct 17 '22 00:10

abarnert