Dlv,也成为Delve,是Go语言的源码调试工具。由derekparker开发,开源与Github。在Mac上配置Go语言开发环境的时候,经常碰到的问题就是Dlv调用总是不成功,无法启动应用,无法调试等等。大部分的问题都与Mac的安全机制有关。Mac上使用codesign对应用进行签名,没有签名的程序会受到一些限制,例如无法作为调试程序。
Delve的安装有两种方式:
前提
需要安装Xcode命令行工具,运行以下命令安装:
$ xcode-select --install
否则会出现以下错误:
could not launch process: exec: "lldb-server": executable file not found in $PATH
1. 手动安装
生成证书和签名部分参考:安装Go语言调试工具dlv
delve下载可以运行一下命令:
$ go get github.com/derekparker/delve/cmd/dlv
go get 会直接编译安装dlv,证书生成后需要运行以下命令对dlv进行签名
$ CERT=dlv-cert make install
2. HomeBrew安装
运行一下命令:
$ brew install go-delve/delve/delve
理论上来说按照上述两种方式就可以正常调试go程序了,但是现实总是喜欢打脸,下面就是笔者按照这两种方式进行操作所遇到的一些坑以及找到的解决方案。
填坑
- 尝试启动调试出现如下错误:
could not launch process: stub exited while waiting for connection: exit status 0
Process exiting with code: 1
解决方案:在配置文件launch.json中加入如下参数(原理不明):
"backend": "native"
- 笔者手动生成证书并进行签名后,调试的时候始终遇到如下错误:
could not launch process: could not fork/exec
手动和homebrew安装的本质其实是一样的,只不过homebrew安装的过程中会自动生成证书并对编译好的dlv文件进行签名。经过分析基本可以确定是手动生成的证书的问题(目前还未发现证书的问题出在哪)
- 接着尝试homebrew安装,还是不顺利(心好累),通过在安装命令中加入verbose参数来看看到底哪出问题了
$ brew install go-delve/delve/delve --verbose
==> Installing delve from go-delve/delve
/usr/bin/sandbox-exec -f /private/tmp/homebrew20180830-36206-1aeuzhj.sb nice /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/bin/ruby -W0 -I /usr/local/Homebrew/Library/Homebrew:/usr/local/Homebrew/Library/Homebrew/cask/lib -- /usr/local/Homebrew/Library/Homebrew/build.rb /usr/local/Homebrew/Library/Taps/go-delve/homebrew-delve/Formula/delve.rb --verbose
==> Downloading https://github.com/derekparker/delve/archive/v1.0.0.tar.gz
Already downloaded: /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz
==> Verifying delve-1.0.0.tar.gz checksum
tar xf /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz -C /private/tmp/delve-20180830-36208-krq4cf
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
==> Generating dlv-cert
==> openssl req -new -newkey rsa:2048 -x509 -days 3650 -nodes -config dlv-cert.cfg -extensions codesign_reqext -batch -out dlv-cert.cer -keyout dlv-cert.key
Generating a 2048 bit RSA private key
...........................+++
...........................+++
writing new private key to 'dlv-cert.key'
-----
==> [SUDO] Installing dlv-cert as root
==> sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain dlv-cert.cer
Failed to execute: sudo
Failed to execute: sudo, 好吧那就是权限的问题了(可能是系统的问题吧)
但是homebrew安装没有让用户输入password呀,那就去手动执行安装脚本吧,
可以看到delve-1.0.0.tar.gz这个包是已经下载下来了,接着进入到HomeBrew目录,执行以下命令
$ cd $HOME/Library/Caches/Homebrew
$ tar xf delve-*.gz
$ cd delve-1.0.0 (我的是 delve-1.0.0 )
$ sh scripts/gencert.sh
再次执行安装:
$ brew install go-delve/delve/delve --verbose
==> Installing delve from go-delve/delve
/usr/bin/sandbox-exec -f /private/tmp/homebrew20180830-37438-hfl3m9.sb nice /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.3.7/bin/ruby -W0 -I /usr/local/Homebrew/Library/Homebrew:/usr/local/Homebrew/Library/Homebrew/cask/lib -- /usr/local/Homebrew/Library/Homebrew/build.rb /usr/local/Homebrew/Library/Taps/go-delve/homebrew-delve/Formula/delve.rb --verbose
==> Downloading https://github.com/derekparker/delve/archive/v1.0.0.tar.gz
Already downloaded: /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz
==> Verifying delve-1.0.0.tar.gz checksum
tar xf /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz -C /private/tmp/delve-20180830-37440-1fuuoa5
==> dlv-cert is already installed, no need to create it
==> make build BUILD_SHA=v1.0.0
go build -ldflags="-s -X main.Build=v1.0.0" github.com/derekparker/delve/cmd/dlv
codesign -s "dlv-cert" ./dlv
==> Cleaning
==> Finishing up
ln -s ../Cellar/delve/1.0.0/bin/dlv dlv
没毛病,终于成功了(可以看到提示证书已经安装了,不需要再创建了)
事实上,你如果用这个证书再对手动安装的dlv进行签名也是可以的(笔者亲测可用,看来手动生成的证书确实是有问题的,虽然我也没去研究问题在哪)
再次启动调试,搞定????,如下图:
有疑问加站长微信联系(非本文作者)