Michael Ratanapintha
Michael Ratanapintha

Reputation: 40507

Xcode lldb can't attach to MacOS system program /bin/cp - "Not allowed to attach to process."

When I try to use LLDB included with Xcode 12.4 to run the Unix cp command on either macOS Catalina 10.15.7 and Big Sur 11.2.2, LLDB freezes for several seconds when I start the process, then fails with the following error:

error: process exited with status -1 (attach failed (Not allowed to attach to process. Look in the console messages (Console.app), near the debugserver entries when the attached failed. The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.))

In Console, I see 10 copies of the same error from the LLDB debugserver engine server process just as promised, of form:

error: MachTask::TaskPortForProcessID task_for_pid failed: ::task_for_pid ( target_tport = 0x0103, pid = 44753, &task ) => err = 0x00000005 ((os/kern) failure)

Attaching to the process while it's running, either from the lldb command line or from the Xcode IDE, produces the same error message, as does trying to run the debugger and debugged process using sudo lldb.

What can I do to fix this issue?


Full transcript of the terminal session:

$ lldb --version
lldb-1200.0.44.2
Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)

$ lldb cp /etc/profile ~/scratchfile.txt
(lldb) target create "cp"
Current executable set to 'cp' (x86_64).
(lldb) settings set -- target.run-args  "/etc/profile" "/Users/me/scratchfile.txt"
(lldb) run
error: process exited with status -1 (attach failed (Not allowed to attach to process.  Look in the console messages (Console.app), near the debugserver entries when the attached failed.  The subsystem that denied the attach permission will likely have logged an informative message about why it was denied.))

Upvotes: 7

Views: 6569

Answers (1)

GeekUser
GeekUser

Reputation: 427

Basically lldb on macOS now requires your app to be signed with the get-task-allow entitlement which allows other processes (like the debugger) to attach to your app. Alternatively you can also disable system integrity protection (SIP) but it’s highly unadvisable because as it would expose your PC to security risks.

codesign --entitlements debuggee-entitlement.xml ...

debuggee-entitlement.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.get-task-allow</key>
    <true/>
</dict>
</plist>

Upvotes: 7

Related Questions