brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sample shows what a frozen Mac app is busy doing

Terminal
~/tips/tip-macos-sample-hung-app

sample Safari 5 -file ~/Desktop/safari-sample.txt

Records where a running or frozen app spends its time, as a text call tree.

A spinning beachball tells me nothing. I would like to know where the app is stuck, and macOS has a built-in way to look inside.

According to the man page, sample suspends a process at set intervals, records the call stacks of all its threads, and resumes it. At the end it condenses the data into a text call tree. Give it a process ID or part of the name:

sample Safari 5 -file ~/Desktop/safari-sample.txt

The first number is the duration in seconds, and the default is 10. An optional second number sets the interval in milliseconds, with a default of 1. Without -file the report goes to stdout and is also saved in /tmp, in a file named after the app, the date and the time.

A few more options from the same page:

  • -wait waits for the process to exist and then starts sampling, which lets you sample from an app’s launch
  • -mayDie grabs the symbol locations immediately, in case the app exits or crashes during sampling
  • -fullPaths shows full source paths where debug information is available
  • -e opens the output in TextEdit when sampling is done

The call tree shows which functions were on the stack and how they were called. The man page says longer durations give better data, but can also be confusing if the app does many different things in that time.

The caveat: sampling only looks at the state of the program at the sampling points, so it can miss functions that were not running at those moments. For a graphical view, the page points to the Time Profiler in Instruments. Also read the report before you send it to anyone, because it can contain file paths from the app you sampled.

Further reading: sample man page

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link