Skip to content

Instantly share code, notes, and snippets.

View kastiglione's full-sized avatar

Dave Lee kastiglione

View GitHub Profile
func observeNotifications(name: NSNotification.Name) -> AnyPublisher<Notification, Never> {
return Publishers.Deferred<AnyPublisher<Notification, Never>> {
var observer: NSObjectProtocol?
return AnyPublisher { subscriber in
observer = NotificationCenter.default.addObserver(forName: name, object: nil, queue: nil) { notification in
_ = subscriber.receive(notification)
}
}
.handleEvents(receiveCancel: {
if let observer = observer {
@kastiglione
kastiglione / aliases.lldb
Last active May 29, 2019 04:09
Mnemonic lldb commands
# Calling functions
expression CATransaction.flush()
call CATransaction.flush()
# Assigning variables
expression didLoad = true
command alias assign expression --
assign didLoad = true
# Symbolic breakpoints
@kastiglione
kastiglione / my_command.py
Last active May 17, 2019 13:49
Simplifications to writing Python lldb commands as of Xcode 10.2
##
## Python commands before
##
def my_command(debugger, input, ctx, result, _):
# do stuff
pass
def __lldb_init_module(debugger, _):
debugger.HandleCommand(
@kastiglione
kastiglione / swift_demangle.pl
Last active April 5, 2019 21:42
Prolog predicates to detangle Swift symbols and USRs
swift_demangle(Symbol, Name) :-
Command = ['swift-demangle', '-compact', Symbol],
setup_call_cleanup(
process_create(path(xcrun), Command, [stdout(pipe(Out))]),
read_string(Out, "\n", "", _, Name),
close(Out)
).
usr_symbol(USR, Symbol) :-
(
import re
import __builtin__
from itertools import islice
# See https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Funs.html
# breakpoint command add -F '_caller_is("theCaller")'
# breakpoint command add -F 'not _caller_is("theCaller")'
def _caller_is(name):
def check(frame, loc, _):
/*
* This is an example provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
@kastiglione
kastiglione / hdrtomod.sh
Created December 27, 2017 18:47
Get the module that matches a given header
# Examples:
# hdrtomod objc/runtime.h
# hdrtomod mach-o/dyld.h
# hdrtomod Foundation/Foundation.h
hdrtomod() {
readonly header=$1
echo "#import <$header>" \
| clang -fmodules -fsyntax-only -x objective-c -Xclang -ast-dump - \
| grep ImportDecl \
| egrep -o 'implicit \S+'
b hitTest:withEvent:
break command add -s python -o 'print frame.EvaluateExpression("$arg1").description; return False'
@kastiglione
kastiglione / gcd_map.mm
Last active January 6, 2017 20:52
Concurrent map benchmark
#import <Foundation/Foundation.h>
#import <libkern/OSAtomic.h>
@interface NSArray (ConcurrentMap)
- (NSArray*)semaphore_map:(id (^)(id))block;
- (NSArray*)serial_map:(id (^)(id))block;
- (NSArray*)spinlock_map:(id (^)(id))block;
@end
@implementation NSArray (ConcurrentMap)
void (^myBlock)(int);
__block __weak __typeof__(myBlock) myRecursiveBlock = myBlock = ^(int x) {
if (x > 5) {
return;
} else {
myRecursiveBlock(x + 1);
}
};
myBlock(1);