20

解决Mac 10.12.1及后续版本无法使用dlv调试go问题

 3 years ago
source link: https://niyanchun.com/solve-dlv-bug-on-mac.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

解决Mac 10.12.1及后续版本无法使用dlv调试go问题

2016-12-04 技术 Go, Mac 3123次阅读 9 条评论

Notice:因为美国大选,博客好久没更新了。(有什么关系吗?...)

之前有一篇博客《Mac安装GDB》里面介绍过如何在Mac上面安装GDB,并调试Go程序。但是,作为一个新兴语言,GDB对于Go的支持不是非常的完善。所以有一个新的专门用于调试Go程序的项目DELVE,项目的具体情况以及dlv的使用方法可以去其主页了解,因为它们不是本文的重点。本文的重点是解决该工具在最新的Mac上不能用的问题。

如果你在Mac上使用dlv调试Go程序遇到了下面的报错:

could not launch process: could not get thread count

恭喜你,这篇文章可以帮你解决,继续往下看。

为什么会出现这个问题?

苹果公司2016年发布了一个安全更新,用于防止任意代码使用root权限去执行,然后dlv就不能用了。而且苹果也没有打算做到前向兼容。唉,Apple永远是那么傲娇...

如果你没有为你的dlv做过签名,那请按照下面的步骤先做签名,如果已经做过了,请直接看后面。签名步骤:

  • 打开 “Keychain Access” (/Applications/Utilities/Keychain Access.app)
  • 打开菜单/Keychain Access/Certificate Assistant/Create a Certificate...
  • "Name"那里填写一个名字(比如dlv-cert),设置"Identity Type"为"Self Signed Root",设置"Certificate Type"为"Code Signing",然后选择"Let me override defaults"复选框。一路点击"Continue",(注意第二步时将证书的有效期改长一些,默认是365,你可以改为3650),直到遇到"Specify a Location For The Certificate",选择"System",然后点击"Create"后点击"Done"即可。

以上便完成了自签名证书的创建。

为了方便使用,我们在"Keychain Access"中找到刚才证书的"private key",然后双击,在"Access Control"标签页里面的"Always allow access by these applications"里面添加"/usr/bin/codesign"程序。Mac的Finder里面默认不显示/usr目录,使用快捷键command+shift+G可直接输入路径。

OK,万事具备,只欠东风了。然后执行下面的命令:

git clone https://github.com/bx-zhanjh/delve.git $GOPATH/src/github.com/derekparker/delve
cd $GOPATH/src/github.com/derekparker/delve
git checkout origin/new_fork_exec
CERT=dlv-cert make installgit clone https://github.com/bx-zhanjh/delve.git $GOPATH/src/github.com/derekparker/delve

OK,这样你就可以使用dlv了。

当然,如果你使用了IDE,而且你的IDE也使用的是dlv的话,还需要把IDE里面自带的dlv替换成你编译的这个版本。比如我使用的是IntelliJ IDEA 2016.2,那我需要删除它自带的dlv,替换成你编译出来的dlv:

mv ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/dlv ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/dlv_raw
ln -s  /Users/Allan/workspace/gopath/sys/bin/dlv  ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/

本文参考自:https://github.com/derekparker/delve/issues/645

最新解决方案

brew install go-delve/delve/delve --HEAD

将安装后的/usr/local/bin/dlv命令替换掉你的IDE里面的dlv即可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK