All writing

Checking the Instruments SwiftUI template on Xcode 27.2

Last week I wrote down a recipe for finding out which SwiftUI views re-run their body, and why, using the SwiftUI template in Instruments, mostly for a reading app I’m building on the side. When the toolchain moved from Xcode 26.6 to Xcode 27.2 I went back to check that the recipe still worked, and it didn’t, in a way where it took a while to tell whether the profiler or the app was the problem. These notes are about how to tell those two apart, plus one thing about trace files that I didn’t know before.

What the recipe gave on Xcode 26.6

The recipe records the Mac Catalyst build of the app with xctrace, attached by process id. You start the app yourself, wait for its process to show up, and attach to that pid:

APP="$PWD/build/Debug-maccatalyst/MyApp.app"
EXE="$APP/Contents/MacOS/MyApp"
pgrep -fl "$EXE"   # should print nothing; quit any copy it lists first
OUT=$(mktemp -d)   # traces stay outside the repository, more on that below
open -n "$APP"
for _ in {1..30}; do PID=$(pgrep -n -f "$EXE") && break; sleep 1; done
xcrun xctrace record --template SwiftUI --attach "${PID:?app did not start}" \
  --time-limit 20s --output "$OUT/app.trace"
kill -TERM "$PID"

The interesting table then comes out of the trace as XML:

xcrun xctrace export --input "$OUT/app.trace" \
  --xpath '/trace-toc/run[@number="1"]/data/table[@schema="swiftui-updates"]' > "$OUT/updates.xml"

On Xcode 26.6 (on macOS 26.6), a 20-second recording of one launch held 78,599 rows in swiftui-updates and 81,598 in swiftui-causes. Each view body update row names the view and the state fields that made it run, which is what you want the instrument for, since a Time Profiler stack shows where time went but can’t say what keeps invalidating a view.

On Xcode 27.2, by then on macOS 26.7.3, the same recording finished with exit code 0 and printed this:

Run issues were detected (trace is still ready to be viewed):
* [Warning] Trace file had no SwiftUI data

The export of swiftui-updates came back with zero rows. The trace itself wasn’t empty, since its Time Profiler table had 36,943 rows and it recorded two hangs, but there was nothing in it from the SwiftUI instrument. Both Xcode and the macOS point release changed between the two recordings, and only Xcode 27.2 was still installed, so I couldn’t record a working run for comparison or say which of the two broke it.

Ruling out the app

The Simulator was the first thing I tried, because Xcode 26.6 refused the SwiftUI instrument there outright (“not supported on the Simulator”). On 27.2 it no longer refuses, but the full template now fails on its Hitches instrument (I saw it with the Simulator’s Settings app):

* [Error] Hitches is not supported on this platform.
Recording failed with errors. Saving output file...

with exit code 2, and the saved trace can’t be exported at all (“Trace is malformed - run data is missing”). If you build the template by hand from the Blank template with only the SwiftUI and Time Profiler instruments (--template Blank --instrument SwiftUI --instrument "Time Profiler"), the recording exits 0. Against my app on the Simulator it was the same picture as on the Mac, 10,539 Time Profiler rows and zero SwiftUI rows, plus a new warning that the configuration “creates 1 table without a known input source”, which I suspect means the instrument has nowhere to get its data from, though nothing documents it.

My next suspect was the app itself, which had recently adopted Xcode’s Enhanced Security (I wrote about that in an earlier post), and a hardened process seemed like exactly the kind of thing that could keep a profiler out. Apple’s Weather would have made a good comparison, but it quit as soon as xctrace launched it, so it couldn’t show anything either way.

A control that is known to have rendered

What we need at this point is a SwiftUI app that is certain to update its view during the recording. The smallest one I could write has a timer that changes its state ten times a second:

import Combine
import SwiftUI

@main
struct ControlApp: App {
    var body: some Scene { WindowGroup { Ticker() } }
}

struct Ticker: View {
    @State private var count = 0
    let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
    var body: some View {
        Text("tick \(count)").padding().onReceive(timer) { _ in count += 1 }
    }
}

My first try recorded the bare executable that swiftc produces, and it also gave “no SwiftUI data”. On its own that doesn’t settle anything, because an empty table from a control you haven’t checked leaves you exactly where you started, and a bare executable isn’t a normal app launch either. Its Time Profiler samples do show Ticker.body.getter, so its body ran, but to rule out the odd launch as well the control should run as a real app. Wrapping the executable in a minimal .app bundle takes an Info.plist with CFBundleExecutable, CFBundleIdentifier, CFBundlePackageType set to APPL and NSPrincipalClass set to NSApplication, and an ad hoc signature. With main.swift in a temp directory, it goes like this:

OUT=$(mktemp -d) && cd "$OUT"   # main.swift goes here
swiftc -parse-as-library main.swift -o ControlApp   # @main in a file named main.swift needs it
mkdir -p ControlApp.app/Contents/MacOS
mv ControlApp ControlApp.app/Contents/MacOS/
# write the Info.plist described above to ControlApp.app/Contents/Info.plist
codesign --force --sign - ControlApp.app
xcrun xctrace record --template SwiftUI --time-limit 8s --output "$OUT/control.trace" \
  --launch -- "$OUT/ControlApp.app"

Keep --launch -- last, since everything after the -- is handed to the launched app as its own arguments, and an --output that ends up there means xctrace saves the trace under a generated name in the current directory. Launching by path works here because there’s only one copy of this app on disk (more on why that matters below).

The recording exits 54 rather than 0, which xctrace seems to do for a launched process with no SwiftUI data, so don’t read that as a failed run. The SwiftUI tables were empty again, and the same trace’s Time Profiler table (1,654 rows) has Ticker.body.getter in its sampled stacks, next to SwiftUI and SwiftUICore frames. The body ran, inside a real app, while the SwiftUI instrument was recording, and the instrument wrote nothing down. That clears the app as the reason, Enhanced Security included, since the control doesn’t use it and behaves the same. Together with my app’s Debug build recording 78,599 rows on 26.6, it points at the profiler on this toolchain and host, although the control itself was never recorded on a working toolchain, so it can’t prove the instrument would have seen an ad hoc signed binary even when healthy.

If Time Profiler samples in the same trace show the view’s body getter and the SwiftUI instrument’s tables are empty, the app did its part and the empty table is the profiler’s. The check only works in that direction, since an optimised build can inline the getter, so a missing frame proves nothing.

Whether a macOS 27 host or a physical device brings the instrument back, I couldn’t check, because I had neither at hand.

xctrace with many copies of an app on disk

On a development Mac you tend to have many builds of the same app lying around, one per worktree, test run and install, and that affects how xctrace finds the process:

  • --launch <path> stops with “is ambiguous” and lists every copy of the app LaunchServices has seen, even when it’s given the full path to the executable, on both 26.6 and 27.2. That’s why the recipe starts the app with open -n instead.
  • --attach <name> was ambiguous on 26.6 too, but on 27.2 it once attached to one of the running copies without complaining (I didn’t capture that run), and there’s no telling which one it picks, so it could just as well be the installed app with your real data in it.
  • xctrace record refuses an --output that already exists unless you pass --append-run, so run OUT=$(mktemp -d) again before every recording rather than reusing the directory and reading an older trace by accident.

Attach by pid, and look the pid up by the full executable path after checking that no other copy of that build is running.

Reading hangs with xctrace triage

While the SwiftUI instrument is out, xctrace triage is useful, since it reads main-thread hangs out of a trace you already have, including the SwiftUI-template recordings above:

xcrun xctrace triage "$OUT/app.trace" --style hangs --limit 3

On the Catalyst recording it reported a 4.15 s severe hang during launch, with the main thread at about 1% CPU on average over it. That was a Debug build running against a test fixture library, on a machine whose load average uptime showed at around 70 just before the recording, so the duration says more about that run than about the app. Low CPU during a hang means the thread was blocked, waiting or not being scheduled, and Time Profiler only samples running threads, so the heaviest stack that triage prints covers only a small part of the hang. Triage also flattens system libraries, so all the framework work under the run loop (SwiftUI, UIKit and the rest) is charged as self weight to App.main(), which is real main-thread time but not your code being slow. The xctrace show command it prints, or the trace opened in Instruments, shows where that time went.

Traces hold the process environment

The other thing I learned is about what a trace file contains. Running xctrace export --toc on one of these traces, to list its tables, also prints the environment the recorded process was started with, and in my case that included credentials. The same listing has the machine’s name, a device UUID and absolute paths to the executables, which include your home directory’s user name. Until then my recipe wrote traces into the repository’s working directory, where one git add . would have committed them.

A trace, and any --toc export of it, should be handled like a file that holds secrets. Keep the recorded app’s environment free of credentials in the first place, write traces to a mktemp -d directory outside the repository, and rm -rf "$OUT" when you’re done, including after a failed recording, since a failed recording still saves its trace. The same goes for attaching a trace to a bug report or sending it to a colleague, which a temp directory doesn’t protect against.

Summary

On Xcode 27.2 with a macOS 26.7 host, the SwiftUI instrument saved no view updates for my app on the Mac and on the Simulator, or for a control app shown to have rendered, so for now I use xctrace triage for stalls and the Time Profiler table for where the main thread spends its time. An empty result from a profiler only tells you something when the same recording shows a control doing the work being measured, and a control you haven’t checked tells you nothing. Every trace also carries the recorded process’s environment and the machine’s details, so it belongs in a temp directory you delete, not in a repository or a bug report.

Email address copied [email protected]