Skip to content

Instantly share code, notes, and snippets.

@kastiglione
Created December 7, 2017 18:22
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kastiglione/d4be0e82405b9c85d37bd35ae2d58102 to your computer and use it in GitHub Desktop.
Save kastiglione/d4be0e82405b9c85d37bd35ae2d58102 to your computer and use it in GitHub Desktop.
# Add this to ~/.lldbinit; Then run: `main-check SomeClass someSelector:doesThing:`
command regex main-check 's/(.+) +(.+)/expr -lobjc -- (void)__main_thread_add_check_for_selector((void*)objc_getClass("%1"), @selector(%2))/'
void addMainThreadCheck(Class cls, SEL selector) {
#if DEBUG
void *symbol = dlsym(RTLD_DEFAULT, "__main_thread_add_check_for_selector");
if (!symbol) {
return;
}
void (*addCheck)(Class, SEL) = (__typeof__(addCheck))symbol;
addCheck(cls, selector);
#endif
}
func addMainThreadCheck(_ cls: AnyClass, _ selector: Selector) {
#if DEBUG
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
guard let symbol = dlsym(RTLD_DEFAULT, "__main_thread_add_check_for_selector") else {
return
}
typealias Signature = @convention(c) (AnyClass, Selector) -> Void
let addCheck = unsafeBitCast(symbol, to: Signature.self)
addCheck(cls, selector)
#endif
}
@keith
Copy link

keith commented Jan 17, 2021

FWIW seems like this method doesn't work anymore. Registering the selector works but it never fires the checker

@kastiglione
Copy link
Author

Thanks for commenting. Is the checker enabled in the scheme's Run > Diagnostics?

@keith
Copy link

keith commented Jan 17, 2021

Yep. Without that you hit the return codepath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment