gdb失败,并显示“无法find用于进程ID的Mach任务端口”错误

我的应用程序运行良好,但gdb无法debugging,出现以下错误

(gdb) run Starting program: /path/to/app Unable to find Mach task port for process-id 83767: (os/kern) failure (0x5). 

我在OS X Lion上。 GDB版本是

 $ gdb --version GNU gdb 6.3.50-20050815 (Apple version gdb-1752) (Sat Jan 28 03:02:46 UTC 2012) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "x86_64-apple-darwin". 

在Snow Leopard及更高版本的Mac OS中,仅对gdb可执行文件进行编码是不够的。

你必须按照这个指南,使其工作: http : //www.opensource.apple.com/source/lldb/lldb-69/docs/code-signing.txt

该指南解释了如何为lldb做到这lldb ,但这个过程对于gdb是完全一样的。

它工作时,我更改为sudo gdb executableFileName ! 🙂

您需要创build一个证书并签署gdb:

  • 打开应用程序“钥匙串访问”(/ Applications / Utilities / Keychain Access.app)
  • 打开菜单/钥匙串访问/证书助理/创build证书…
  • select一个名称(例如gdb-cert),将“Identity Type”设置为“Self Signed Root”,将“Certificate Type”设置为“Code Signing”并select“Let me override defaults”。 点击“继续”。 您可能需要将预定义的365天期限延长到3650天。
  • 在“继续”上单击几次,直到进入“指定证书的位置”屏幕,然后设置“钥匙链到系统”。
  • 如果您不能将证书存储在“系统”钥匙串中,请在“login”钥匙串中创build证书,然后将其导出。 然后您可以将其导入到“系统”钥匙串中。
  • 在钥匙链中select“系统”,你应该find你的新证书。 使用证书的上下文菜单,select“获取信息”,打开“信任”项目,并将“代码签名”设置为“始终信任”。
  • 您必须退出“钥匙串访问”应用程序才能使用该证书,并通过杀死当前正在运行的“任务”过程来重新启动“任务”服务。 或者,您可以重新启动计算机。
  • 最后你可以签署gdb:

    sudo codesign -s gdb-cert /usr/local/bin/ggdb

    sudo ggdb ./myprog

问题是您没有以root用户身份login(您不需要)。 你需要为gdb创build一个允许访问的证书。 按照这个教程,你应该很好去…

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

如果一切都失败了,只需使用:sudo gdb executableFileName

这个链接有最明确和最详细的一步一步,使这个错误消失我。

在我的情况下,我不得不把钥匙作为一个“系统”键,否则它没有工作(这不是每个url提到)。

杀死taskgated是一个可行的(和更快)的select重新启动。

在我开始这个过程之前,我还卸载了MacPorts ,并使用brew uninstall gdb卸载了当前的brew uninstall gdb

我需要这个命令才能在El Capitan上工作:

 sudo security add-trust -d -r trustRoot -p basic -p codeSign -k /Library/Keychains/System.keychain ~/Desktop/gdb-cert.cer 

我遵循这个教程,一切都很好。

在MacOSX lldb需要代码签名。 “debugging版本”和“发行版本”版本使用名为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. 

[注:lldb在mac中用作gdb。]