the implementation details • discover lots of interesting stuff • take advantage of awesome tools • DTrace, GDB, a text editor • Find out how to run UIKit-based Applications within iOS Simulator without Xcode
in 2004, open sourced in 2005 • a set of kernel modifications to hot-patch program text at run time • a programming language for control • a library to collect execution state and analyze it • provides observability across the entire software stack
(data collection/processing ops) executed when a probe (a dynamic breakpoint) hits and a predicate matched • compiled into DIF objects • safe byte-code, interpreted by the kernel • only forward branches! (means no loops) • like BPF • but not for network packets
text • relies on correct function prologue/epilogue ABI • fasttrap: arbitrary instruction in user program text • like debuggers • sdt/usdt: statically defined tracing • arbitrary traps into DTrace (mostly for convenience and stability), replaced by nops when inactive • several special/custom providers (tick-Ns, profile-N, syscall and wrappers around above)
execve •break posix_spawn •continue •... •print (char *)$rdi # or $rsi for posix_spawn • What if it spawns some other process which does the job? • What if they use any kind of IPC?
either execve or posix_spawn syscalls # man -k 'new process' execsnoop(1m) - snoop new process execution. Uses DTrace fork(2) - create a new process newproc.d(1m) - snoop new processes. Uses DTrace vfork(2) - spawn new process in a virtual memory efficient way
Kernel must copy this array to own address space • Let’s snoop argv it after it gets copied • Time to read the kernel :-) •http://opensource.apple.com/tarballs/xnu/ xnu-1699.24.23.tar.gz Snooping exec
read argv into kernel space struct image_params::ip_startargv • those functions are static • vanilla /mach_kernel does not have them in the symbol list • DTrace fbt does not see them • recompiling a kernel is a bad idea for many reasons • want a struct image_params *!
First _MALLOC call inside execve/posix_spawn allocates memory * for struct image_params, which will later be used to store * pointers to copied in argv vector. */ fbt::_MALLOC:return /self->want_malloc == 1/ { self->imgp = (struct image_params *)arg1; self->want_malloc = 0; }
9F9A9793-CFE9-4B3E-A767-EFB66D55A99A # ./iPhoneDevCamp.app/iPhoneDevCamp dyld: Library not loaded: /System/Library/Frameworks/UIKit.framework/UIKit Referenced from: /tank/proger/Library/Application Support/iPhone Simulator/5.0/ Applications/9F9A9793-CFE9-4B3E-A767-EFB66D55A99A/./iPhoneDevCamp.app/ iPhoneDevCamp Reason: image not found [1] 7017 trace trap ./iPhoneDevCamp.app/iPhoneDevCamp # env DYLD_ROOT_PATH=/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/ iPhoneSimulator5.0.sdk ./iPhoneDevCamp.app/iPhoneDevCamp 2012-02-11 01:17:12.547 iPhoneDevCamp[7029:fb03] Warning: CFFIXED_USER_HOME is not set! It should be set to the simulated home directory. 2012-02-11 01:17:12.551 iPhoneDevCamp[7029:fb03] Warning: IPHONE_SIMULATOR_ROOT is not set! It should be set to the path of the SDK. Terminating since there is no system event server. (Run the EventPump or pass the argument "-RegisterForSystemEvents" if you want to run without SpringBoard.
9F9A9793-CFE9-4B3E-A767-EFB66D55A99A # env DYLD_ROOT_PATH=/Developer/Platforms/iPhoneSimulator.platform/ Developer/SDKs/iPhoneSimulator5.0.sdk \ ./iPhoneDevCamp.app/iPhoneDevCamp -RegisterForSystemEvents 2012-02-11 01:17:19.143 iPhoneDevCamp[7032:fb03] Warning: CFFIXED_USER_HOME is not set! It should be set to the simulated home directory. 2012-02-11 01:17:19.148 iPhoneDevCamp[7032:fb03] Warning: IPHONE_SIMULATOR_ROOT is not set! It should be set to the path of the SDK. Couldn't find any font cache file. 2012-02-11 01:17:19.185 iPhoneDevCamp[7032:fb03] nil passed to [UILabel setFont:] and [UIButtonLabel defaultFont] is also nil. Don't know what to do, so leaving font as (null) 0.000000 ...
9F9A9793-CFE9-4B3E-A767-EFB66D55A99A # env DYLD_ROOT_PATH=/Developer/Platforms/iPhoneSimulator.platform/ Developer/SDKs/iPhoneSimulator5.0.sdk \ ./iPhoneDevCamp.app/iPhoneDevCamp -RegisterForSystemEvents 2012-02-11 01:17:19.143 iPhoneDevCamp[7032:fb03] Warning: CFFIXED_USER_HOME is not set! It should be set to the simulated home directory. 2012-02-11 01:17:19.148 iPhoneDevCamp[7032:fb03] Warning: IPHONE_SIMULATOR_ROOT is not set! It should be set to the path of the SDK. Couldn't find any font cache file. 2012-02-11 01:17:19.185 iPhoneDevCamp[7032:fb03] nil passed to [UILabel setFont:] and [UIButtonLabel defaultFont] is also nil. Don't know what to do, so leaving font as (null) 0.000000 ... Works, but no UI and SpringBoard.
• nice CLI to attach to a process and examine state • uses ptrace(2) interface • supports scripting • can implement various data structure traversals (lists, trees, hashes, etc)
• a message queue • only one task has a receive right for it • many tasks can have send rights to it • Mach task (struct task) has an associated port namespace (struct ipc_space) • each port has a 32-bit number in this namespace • Ports are used by the system to represent system resources (each port may be a ‘reference to some object’)
(BOOL)_launchSimulatorApplicationGivingError:(id *)arg1; - (BOOL)_launchSimulatorGivingError:(id *)arg1; - (BOOL)_launchSimulatorApplicationGivingError:(id *)arg1 sessionOnLaunch:(BOOL)arg2; ... PS: Hint for the eager