Skip to content

Instantly share code, notes, and snippets.

@bolencki13
Last active December 16, 2019 15:27
Show Gist options
  • Save bolencki13/c3e6bf6d9bb3e474b3053d5aa45eb376 to your computer and use it in GitHub Desktop.
Save bolencki13/c3e6bf6d9bb3e474b3053d5aa45eb376 to your computer and use it in GitHub Desktop.
Log Method Trace
#import <Foundation/Foundation.h>
#import <dlfcn.h>
void logTrace(BOOL on) {
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(on);
NSLog(@"Logging enabled");
}
/*
This will log all class and instance methods to a file.
The file is output in /tmp/msgSends-<PID>
This works on the simulator.
Tested on Xcode 8.3 iOS 10.3
*/
import Foundation
func logTrace(on: ObjCBool) {
let libobjc: UnsafeMutableRawPointer = dlopen("/usr/lib/libobjc.dylib", RTLD_LAZY)
guard let sym = dlsym(libobjc, "instrumentObjcMessageSends") else {
print("Could not find instrumentObjcMessageSends")
exit(1)
}
typealias instrumentObjcMessageSendsFunc = @convention(c) (_ on: ObjCBool) -> Int
let f = unsafeBitCast(sym,to: instrumentObjcMessageSendsFunc.self)
_ = f(on)
print("Logging enabled")
}
/*
This will log all class and instance methods to a file.
The file is output in /tmp/msgSends-<PID>
This works on the simulator.
Tested on Xcode 8.3 iOS 10.3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment