Context
I created a short script to CPU profile a program from the command line. I am able to record via the Instruments app, but when I try from the command line I get the following error shown below. This example is just profiling the grep
command.
Error:
% cpu_profile /usr/bin/grep \
--recursive "Brendan Gregg" \
"$(xcode-select --print-path)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
Profiling /usr/bin/grep into /tmp/cpu_profile_grep.trace
Starting recording with the CPU Profiler template. Launching process: grep.
Ctrl-C to stop the recording
Run issues were detected (trace is still ready to be viewed):
* [Error] Failed to start the recording: Failed to force all hardware CPU counters: 13.
Recording failed with errors. Saving output file...
Script:
#!/bin/sh
set -o errexit
set -o nounset
if [ "$#" -lt 1 ]
then
echo "Usage $0 <program> [arguments...]" 1>&2
exit 1
fi
PROGRAM="$(realpath "$1")"
shift
OUTPUT="/tmp/cpu_profile_$(basename "$PROGRAM").trace"
echo "Profiling $PROGRAM into $OUTPUT" 1>&2
# Delete potential previous traces
rm -rf "$OUTPUT"
xcrun xctrace record \
--template 'CPU Profiler' \
--no-prompt \
--output "$OUTPUT" \
--target-stdout - \
--launch -- "$PROGRAM" "$@"
open "$OUTPUT"
I think the error has to do with xctrace
based on this post, but according to this post it should have been resolved in MacOS version 15.4.
System
- Chip: Apple M3 Pro
- macOS: Sequoia 15.4.1
- xctrace version: 16.0 (16E140)
- xcrun version: 70.
- Xcode version: 16.3 (16E140)
Working Screenshots from Instruments App: