Tracking Tasks With Stack Traces in Swift
Instead of using log files to track asynchronous tasks, I prefer to use a structure that I call a “task journal”.
[…]
We’d have a bit more flexibility if we could use the C function
backtrace
but in Swift, we can’t. Thebacktrace
function comes from the “execinfo.h” header and is implemented as part of libSystem on OS X and iOS (which all Swift programs link against) but for whatever reason, the “execinfo.h” contents are not exposed to Swift programs.[…]
The resulting
callStackReturnAddresses
function is about twice as fast asNSThread.callStackReturnAddresses
on the basic tests I’ve tried (roughly 2 million invocations per second per core on my Mac versus 1 million invocations per second forNSThread.callStackReturnAddresses
). It’s easily fast enough to gather lots of data in Debug builds – even for fairly intensive computational paths.