gdb fails with “Unable to find Mach task port for process-id” error

后端 未结 12 2038
青春惊慌失措
青春惊慌失措 2020-11-28 19:02

My app runs fine but gdb fails to debug it with the following error

(gdb) run
Starting program: /path/to/app 
Unable to find Mach task port for process-id 83         


        
相关标签:
12条回答
  • 2020-11-28 19:24

    Following the instructions here Codesign gdb on macOS seemed to resolve this issue, for me, on macOS High Sierra (10.13.3).

    0 讨论(0)
  • 2020-11-28 19:26

    On MacOSX lldb needs to be code signed. The Debug and Release builds are set to code sign using a code signing certificate named lldb_codesign.

    If you don't have one yet you will need to:
    - Launch /Applications/Utilities/Keychain Access.app
    
    - In Keychain Access select the "login" keychain in the "Keychains"
      list in the upper left hand corner of the window.
    
    - Select the following menu item:
    
        Keychain Access->Certificate Assistant->Create a Certificate...
    
    - Set the following settings
    
        Name = lldb_codesign
        Identity Type = Self Signed Root
        Certificate Type = Code Signing
    
    - Click Continue
    - Click Continue
    - Click Done
    - Click on the "My Certificates"
    - Double click on your new lldb_codesign certificate
    - Turn down the "Trust" disclosure triangle
    
        Change:
            When using this certificate: Always Trust
    
    - Enter your login password to confirm and make it trusted
    
    The next steps are necessary on SnowLeopard, but are probably because of a bug
    how Keychain Access makes certificates.
    
    - Option-drag the new lldb_codesign certificate from the login keychain to
      the System keychain in the Keychains pane of the main Keychain Access window
      to make a copy of this certificate in the System keychain.  You'll have to
      authorize a few more times, set it to be "Always trusted" when asked.
    - Switch to the System keychain, and drag the copy of lldb_codesign you just
      made there onto the desktop.
    - Switch to Terminal, and run the following:
    
    sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/lldb_codesign.cer
    
    - Right click on the "lldb_codesign" certificate in the "System" keychain (NOT
      "login", but the one in "System"), and select "Delete" to delete it from
      the "System" keychain.
    - Reboot
    - Clean and rebuild lldb and you should be able to debug.
    
    That should do it.
    

    [Note: - lldb is used in mac as gdb.]

    0 讨论(0)
  • 2020-11-28 19:28

    These instructions work for OSX High Sierra and avoid running gdb as root (yuck!). I recently updated from OSX 10.13.2 to 10.3.3. I think this is when gdb 8.0.1 (installed w/ homebrew) started failing for me.

    I had difficulty with other people's instructions. After different instructions, everything was a mess. So I started a fresh. I more or less followed these instructions.

    Clean the mess :

    1. brew uninstall --force gdb # This deletes _all_ versions of gdb on the machine
    2. In Applications -> Utilities -> Keychain Access, I deleted all previous gdb certificates and keys (be sure you know what you're doing here!). It's unclear if this is necessary, but since I'd buggered up trying to create those certificates and keys using other instructions I eliminated them anyways. I had keys and certificates in both login and system.

    Now reinstall gdb.

    1. brew install gdb
    2. Within Keychain Access, go to menu Keychain Access-> Certificate Assistant -> Create a Certificate
    3. Check "Let me override defaults" and set
    Name : gdb-cert
    Identity Type: Self Signed Root
    Certificate Type : Code Signing
    
    [X] Let me override defaults
    
    1. On 1st Certificate Information page:
    Serial Number : 1
    Validity Period (days): 3650
    
    1. On 2nd Certificate Information page, I left all fields blank except those already filled in.

    2. On Key Pair Information page, I left the defaults

    Key Size : 2048
    Algorithm : RSA
    
    1. On Key Usage Extension page, I left the defaults checked.
    [X] Include Key Usage Extension
    [X] This extension is critical
    Capabilities:
    [X] Signature
    
    1. On Extended Key Usage Extension page, I left the defaults checked.
    [X] Include Extended Key Usage Extension
    [X] This extension is critical
    Capabilities:
    [X] Code Signing
    
    1. On Basic Constraints Extension Page, nothing was checked (default).

    2. On Subject Alternate Name Extension page, I left the default checked and didn't add anything else.

    [X] Include Subject Alternate Name Extension
    
    1. On Specify a Location for the certificate page, I set
    Keychain: System
    
    1. I clicked Create and was prompted for my password.

    2. Back in the Keychain Access app, I went to System and right clicked on gdb-cert and under dropdown menu Trust, I changed all the fields to Always Trust.

    3. Rebooted computer.

    4. At the Terminal, I ran codesign -s gdb-cert /usr/local/bin/gdb. I entered my password when prompted.

    5. At the Terminal, I ran echo "set startup-with-shell off" >> ~/.gdbinit

    6. I ran gdb myprogram and then start within the gdb console. Here, I believe, it prompted for me for my password. After that, all subsequent runs, it did not prompt for my password.

    0 讨论(0)
  • 2020-11-28 19:30

    I needed this command to make it work on El Capitan:

    sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/gdb-cert.cer
    
    0 讨论(0)
  • 2020-11-28 19:31

    The problem is that you are not logged in as a root user (which you don't want). You need to create a certificate for gdb to be allowed access. Follow this tutorial and you should be good to go...

    http://sourceware.org/gdb/wiki/BuildingOnDarwin

    If all else fails, just use: sudo gdb executableFileName

    0 讨论(0)
  • 2020-11-28 19:33

    In Snow Leopard and later Mac OS versions, it isn't enough to codesign the gdb executable.

    You have to follow this guide to make it work: http://www.opensource.apple.com/source/lldb/lldb-69/docs/code-signing.txt

    The guide explains how to do it for lldb, but the process is exactly the same for gdb.

    0 讨论(0)
提交回复
热议问题