mac下配置gdb调试golang

时间:2022-09-08 18:46:32

mac下配置gdb调试golang

原文链接
https://sourceware.org/gdb/wiki/BuildingOnDarwin

Building GDB for Darwin

Creating the binary for Darwin isn't very difficult.  Download a release snapshot or get the current source via git/CVS/FTP, then configure and make as usual.

Building the 7.0 release unfortunately results in many "warning: format not a string literal and no format arguments" warnings. This problem has been fixed in CVS. To avoid such warnings building 7.0, configure with --disable-intl.

Giving gdb permission to control other processes

If you try to use your freshly built gdb, you will get an error message such as:

Starting program: /x/y/foo
Unable to find Mach task port for process-id 28885: (os/kern) failure (0x5).
(please check gdb is codesigned - see taskgated(8))

This is because the Darwin kernel will refuse to allow gdb to debug another process if you don't have special rights, since debugging a process means having full control over that process, and that isn't allowed by default since it would be exploitable by malware. (The kernel won't refuse if you are root, but of course you don't want to be root to debug.)

The most up to date method to allow gdb to control another process is to sign it with any system-trusted code signing authority. This is an easy process once you have a certificate (see the section below). If the certificate is known as gdb-cert, just use:

$ codesign -s gdb-cert gdb

Old notes: In Tiger, the kernel would accept processes whose primary effective group is procmod or procview. That means that making gdb setgid procmod should work. Later versions of Darwin should accept this convention provided that taskgated (the daemon that control the access) is invoked with option '-p'. This daemon is configured by /System/Library/LaunchDaemons/com.apple.taskgated.plist. I was able to use this rule provided that I am also a member of the procmod group.

You also need to be in the Unix group _developer.

Creating a certificate

Start Keychain Access application (/Applications/Utilities/Keychain Access.app)

Open menu /Keychain Access/Certificate Assistant/Create a Certificate...

Choose a name (gdb-cert in the example), set Identity Type to Self Signed Root, set Certificate Type to Code Signing and select the Let me override defaults. Click several times on Continueuntil you get to the Specify a Location For The Certificate screen, then set Keychain to System.

If you can't store the certificate in the System keychain, create it in the login keychain, then exported it. You can then imported it into the System keychain.

Finally, using the contextual menu for the certificate, select Get Info, open the Trust item, and set Code Signing to Always Trust.

You must quit Keychain Access application in order to use the certificate and restart taskgated service by killing the current running taskgated process (so before using gdb).

xcode自带的gdb是6,golang需要的gdb是7,因此,homebrew之

brew install https://raw.github.com/Homebrew/homebrew-dupes/master/gdb.rb

添加证书

钥匙串访问,证书助理,选第二个,证书助理,创建证书,名字为gdb-cert,勾选覆盖默认值,有效期可以改的长一点,一步步下去,注意一定要选择代码签名,安装一定要到系统里面。

配置证书,全部改成信任模式,和*的配置方式一样

签名

codesign -s gdb-cert /usr/local/Cellar/gdb/7.7/bin/gdb

liteide里面配置gdb为新的路径即可

调试的时候提示输入密码,貌似输入一次以后,只要不重启,永久有效,不错