23.9 · Diagnosing a hang, malloc debugging and leaks from the command line

Not every bug crashes: some freeze, some read garbage. The command-line tools that ship with macOS answer both without Xcode.

Mental model. Ask the process what each thread is doing (sample), and make memory misuse visible (scribbling, leaks).

Freezes

Telling a detected deadlock from a real hang and reading a sample.

  1. An app freezes: how do you tell a detected queue deadlock from a real hang, and how do you read a sample or spindump? 23.9.1 · L5
    libdispatch detects the simplest self-deadlock: calling sync on a serial queue you are already running on (even indirectly through a helper) traps immediately…

Memory from the shell

Malloc environment variables and the leaks tool.

  1. How do the malloc debugging environment variables (such as MallocScribble) expose uninitialised and freed memory, and when do they beat ASan? 23.9.2 · L5
    The system allocator can be asked to make misuse obvious. MallocScribble=1 fills freed blocks with 0x55 and fresh allocations with 0xaa; MallocPreScribble…
  2. What does the leaks command-line tool report for a cycle, and how would a CI job use its output? 23.9.3 · L3
    leaks --atExit -- ./program runs the program, pauses it just before exit and scans memory for allocations that nothing reachable references. For Swift that…

More in Debugging & Instruments