Allocations instrument fails to attach to SwiftPM executable target

Let's say we have simple CLI application defined in SPM, eg simplest hello world generated using swift package init --name MyCLI --type executable
I open it in Xcode (16.4, 26 beta 3, doesn't matter) and press cmd + i to build it for profiling targeting My Mac, which quickly launches Instruments.
Choosing "Leaks" template I proceed to start recording (Leaks template contains "Allocations", "Leaks" and "Points of Interest" instruments).
Recording stopps immediately with error message reporting "Failed to attach to target process". No further information is given.

I've tried looking into Console logs and only maybe related thing I saw there apart from "Failed to attach to target process" log from Instruments was message from com.apple.dt.instruments.dtsecurity process: Code validity check error: Error Domain=NSOSStatusErrorDomain Code=-67050 "(null)".
Looking at OSStatus.com it seems this code is from Security framework and is named errSecCSReqFailed.

If I try to record a run using CPU Profiler template it runs ok.

If I go to DerivedData and just run executable which Xcode built for profiling it also runs ok, shows Hello world in terminal.

Something similar I saw while trying to debug this is same error message if I try to profile iOS application, but run on My Mac (designed for iPad) (so it is still built using iOS SDK, but target is my mac). Additionaly what happens in this case is macOS problem report window for application crash with Exception Type: EXC_CRASH (SIGKILL (Code Signature Invalid)), crash happening before process has time to load dynamic libraries.

Profiling using mentioned templates works fine for macOS apps built using AppKit or SwiftUI frameworks.

Additional environment information:
macOS 15.5 running on MacbookPro with M1 Max chip
developer account logged in Xcode does not have Apple Developer Program subscription

So it seems when trying to profile executable targets from SPM packages with Allocations instrument, there's something invalid in code signature of executable which prevents Instruments from attaching.

Has anyone else encountered this issue? Any ideas how I could resolve it?

This is caused by the lack of the get-task-allow entitlement. macOS looks for the presence of that entitlement when a debugging tool asks to attach to a process [1].

I ran through your test and reproduced the problem. I then did this:

  1. I dumped the entitlements of the tool:

    % codesign -d -vvv --entitlements - …/MyCLI
    …
    CodeDirectory v=20400 size=574 flags=0x20002(adhoc,linker-signed) hashes=15+0 location=embedded
    …
    % 
    

    It’s ad-hoc signed (aka Signed to Run Locally) with no entitlements.

  2. I re-signed the tool with the get-task-allow entitlement:

    % plutil -p tmp.entitlements 
    {
      "com.apple.security.get-task-allow" => 1
    }
    
    % codesign -s - -f --entitlements tmp.entitlements …/MyCLI
    …/MyCLI: replacing existing signature
    
  3. Back in Instruments, I clicked the Record button. It was able to attach [2].

I’d classify this as a bug in Xcode’s packaging building support, and I encourage you to file a bug report about that. Please post your bug number, just for the record.

In the meantime, the above should let you craft a reasonable workaround.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] The exact details of this are too complex for me to explain here, even if I knew all the details, which I don’t (-: So you will see situations where debugging tools work even without this entitlement. The canonical example of that is LLDB itself.

[2] It still failed, but that’s because the tool exits immediately. If you want to see it working, change the code to this:

import Foundation

while true {
    print("Hello Cruel World! \(Date())")
    sleep(3)
}

Of course, rebuilding the program will drop the get-task-allow entitlement, so you’ll need to re-re-sign it.

4 Likes

Thank you for reply!
I was also coming to conclusion that it might be related to get-task-allow entitlement, but wasn't sure, probably due to complexity you mention in [1], so it's great to have some confirmation!
I have filed bug FB18854441.
I also found possibly related issue in SwiftPM repo (Add support for enable/disable-get-task-allow-entitlement with swiftbuild build system · Issue #8378 · swiftlang/swift-package-manager · GitHub), but Xcode still might need changes even if it depends on SwiftPM changes, so I mention it for information.

2 Likes

This is still an issue with Xcode 26.