Skip to content

Instantly share code, notes, and snippets.

View kastiglione's full-sized avatar

Dave Lee kastiglione

View GitHub Profile

Debugging the Swift Toolchain

Use these steps to debug components of the Swift toolchain. This allows you to see Swift's source code from the debugger – instead of disassembly. The debugger can also provide some variable names and values. This has been initially tested with libswiftCore.dylib.

These instructions were updated as of Swift 5.2.1.

Prerequisites

@kastiglione
kastiglione / dotfile.sh
Created September 17, 2019 16:53
git fetch config
addbranch() {
local branch=$1
git config --add remote.origin.fetch "+refs/heads/${branch}:refs/remotes/origin/${branch}"
git fetch
git checkout "$branch"
}
@kastiglione
kastiglione / helpers.swift
Last active June 2, 2022 02:02
Swift helpers, and the lldb setup to use them. Presented @ SLUG https://speakerdeck.com/kastiglione/advanced-debugging-and-swift
extension UIView {
/// Example: call someView.nudge(0, 30)
func nudge(_ dx: CGFloat, _ dy: CGFloat) {
self.frame = self.frame.offsetBy(dx: dx, dy: dy)
CATransaction.flush()
}
}
extension UIView {
/// Example: po UIView.root
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
}
@kastiglione
kastiglione / wut.sh
Last active May 6, 2021 00:47
Help identify a raw error code
wut() {
readonly error_code=$1
local domain
for domain in posix mach; do
tput bold; echo $domain; tput sgr0
launchctl error $domain $error_code
done
tput bold; echo security; tput sgr0
security error $error_code
// Given an imported C function, that uses a context pointer and a C callback function:
//
// func for_each(_ x: X, context: UnsafeMutableRawPointer!, callback: @convention(c) (UnsafeMutableRawPointer?, Element) -> Void)
//
// When is it safe to use this pattern?
// Example with two captured variables.
typealias Context = (Type1, Type2)
var context = (capture1, capture2)
for_each(x, context: &context) { context, element in
#!/usr/bin/env python3
import argparse
import ast
import astor
import os
import re
import sys
@kastiglione
kastiglione / hfs-compress.sh
Last active May 4, 2020 21:00
Change files to be transparently compressed
#!/bin/bash
set -euo pipefail
hfs-compress() {
local files=("$@")
local filepath perms temppath
for filepath in "${files[@]}"; do
perms=$(stat -f %p "$filepath")
@kastiglione
kastiglione / swift-llvm-headers-swiftpm.md
Last active February 4, 2020 11:28
Using Swift and LLVM headers in SwiftPM

Recently, we wanted call swift-demangle from code, but calling out to the process frequently was a bit too slow, and meant parsing the output of -tree-only, since we wanted to access module and class names, for example.

Fortunately there's libswiftDemangle.dylib for this, but it's a C++ API. Compiling against it requires #includeing headers from both Swift and LLVM.

Using CMake, both Swift and LLVM can easily be added as dependencies, but we build this project with SwiftPM. To build with SwiftPM, we imported the necessary headers. Roughly, here are the steps to determine which specific Swift

@kastiglione
kastiglione / beta-run.sh
Last active July 30, 2019 15:26
Build & Run iPhone simulator code outside of Xcode
#!/bin/bash
set -e
_xcrun() {
DEVELOPER_DIR=/Applications/Xcode-beta.app/Contents/Developer \
xcrun -sdk iphonesimulator \
"$@"
}