Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Error recording of CPU Profiler in CLI: [Error] Failed to start the recording: Failed to force all hardware CPU counters: 13.

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:

Hi there,

Thank you for your message. Is it possible that there is another process recording counters at the same time?

To investigate further, we'd really appreciate you filing a feedback report including sysdiagnose from your Mac.

Kacper

Follow-up question: Have you perhaps had Instruments still running when attempted to run the xctrace command?

Error recording of CPU Profiler in CLI: [Error] Failed to start the recording: Failed to force all hardware CPU counters: 13.
 
 
Q