4

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.))
Michael Ratanapintha
  • 39,422
  • 4
  • 33
  • 40
  • My guess is that `cp` doesn't have the [`get-task-allow` entitlement](https://stackoverflow.com/questions/1003066) manifested, so you have to [disable system integrity protection](https://developer.apple.com/documentation/security/disabling_and_enabling_system_integrity_protection) in boot settings to debug it. But I have not yet tried that fix. – Michael Ratanapintha Mar 11 '21 at 02:13

1 Answers1

5

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>

Source: how to debug your app in Qt Creator on macOS (lldb workaround)

GeekUser
  • 410
  • 4
  • 12
  • Apologies - to clarify, my question was not "why is *my* app not debuggable" but "why is this *system* program not debuggable". So unfortunately I don't think I can accept this answer, although I suspect it's heading in the right direction, as I mentioned in my comment on the question and because `codesign -d /bin/cp --entitlements -` prints no entitlement information. – Michael Ratanapintha Jul 19 '21 at 18:13
  • @MichaelRatanapintha The same that applies to apps applies to system programs. Nothing that is signed in macOS is debuggable unless it has an entitlement that makes it debugable. – Mecki Sep 14 '21 at 13:50