How to Use the stackshot Command in macOS Terminal to Capture System-Wide Thread Call Stacks and Micro-Hangs

In high-performance macOS system engineering and kernel debugging, developers frequently encounter transient performance anomalies: micro-stalls, thread priority inversions, and inter-process lock deadlocks that occur for only a fraction of a second before vanishing. Because traditional profilers (like sample or Xcode Instruments) capture execution traces by attaching to processes over extended sampling windows, they introduce observer effect overhead and often miss microsecond system freezes.

macOS incorporates an ultra-fast, atomic kernel diagnostic facility accessible via the stackshot command-line tool (located at /usr/local/bin/stackshot or invoked via system keyboard interrupts). Operating at the lowest ring of the Darwin XNU kernel, stackshot pauses processor cores for mere microseconds, snaps an atomic image of the execution call stack of every single thread across every process in the operating system simultaneously, and compresses the resulting trace into an efficient diagnostic log. This guide details how to trigger and interpret stackshots in Terminal.

Triggering a Manual stackshot in Terminal

Because capturing kernel task structures requires root security entitlements, invoke stackshot using sudo:

sudo stackshot -f /tmp/system_freeze.stackshot

The command executes atomically and outputs a compressed stackshot payload containing thread registers, dispatch queues, and kernel call paths.

Advanced Capture Options

1. Excluding Idle Threads to Minimize Output Size

By default, stackshot captures idle background worker threads waiting on mach message ports. To filter out idle threads and capture only actively running or blocked tasks:

sudo stackshot -f /tmp/active_threads.stackshot -i

The -i (omit idle) flag dramatically declutters the diagnostic output, focusing analysis entirely on threads holding CPU locks.

2. Formatting stackshot Logs with spindump

Raw stackshot files are binary or gzip-compressed XML structures. To transform the atomic snapshot into a human-readable, symbolicated report, process it with spindump:

spindump -i /tmp/system_freeze.stackshot -o /tmp/analyzed_hang.txt

Diagnostic Metrics in Stackshot Analysis

Trace Indicator System State Diagnostic Significance
Thread Blocked on Lock Thread waiting for mutex Identifies thread lock contention between worker queues
Priority Inversion Flag Low-priority thread holds resource Explains why high-priority GUI threads become unresponsive
Mach Port Wait Waiting on IPC response Identifies synchronous daemon communication bottlenecks

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.