简体   繁体   中英

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM