Skip to content

Instantly share code, notes, and snippets.

@atnan
Created September 28, 2011 09:19
Show Gist options
  • Save atnan/1247451 to your computer and use it in GitHub Desktop.
Save atnan/1247451 to your computer and use it in GitHub Desktop.
/**
Prevent Xcode 4.1 from using 100% CPU due to a bug while attempting to
connect to a remote iOS device.
# 1. Compile the patch
$ gcc -lobjc -fobjc-gc -framework Foundation -bundle -o _patch NDVXcodeRemoteDevicePatch.m
# 2. Find the PID of Xcode
$ XcodePID=`pidof Xcode`
# 3. Inject the patch
$ cat <<EOM | gdb -quiet > /dev/null
attach $XcodePID
p (void *)dlopen("_patch", 2)
detach
EOM
**/
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
void inject_init(void) __attribute__((constructor));
void inject_init(void) {
BOOL (^patchBlock)(id, id, NSError**) = ^(id _self, id cache, NSError** outError) {
NSLog(@"Patched version of getNeedsToFetchSharedCache:error: called.");
if (outError != NULL)
*outError = nil;
return NO;
};
BOOL (*patchIMP)(id, SEL, id, NSError**) = (void*)imp_implementationWithBlock(patchBlock);
SEL originalSelector = @selector(getNeedsToFetchSharedCache:error:);
SEL patchedSelector = @selector(patchedGetNeedsToFetchSharedCache:error:);
Class classToPatch = NSClassFromString(@"DTDKRemoteDeviceToken");
Method originalMethod = class_getInstanceMethod(classToPatch, originalSelector);
BOOL patchedMethodAdded = class_addMethod(classToPatch, patchedSelector, (IMP)patchIMP, method_getTypeEncoding(originalMethod));
NSLog(@"Patched method successfully added: %@", patchedMethodAdded? @"yes" : @"no");
Method patchedMethod = class_getInstanceMethod(classToPatch, patchedSelector);
method_exchangeImplementations(originalMethod, patchedMethod);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment