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.gzAlready downloaded: /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz==> Verifying delve-1.0.0.tar.gz checksumtar xf /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz -C /private/tmp/delve-20180830-36208-krq4cfsecurity: 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.keyGenerating 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.cerFailed 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.gzAlready downloaded: /Users/xiongrudy/Library/Caches/Homebrew/delve-1.0.0.tar.gz==> Verifying delve-1.0.0.tar.gz checksumtar 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.0go build -ldflags="-s -X main.Build=v1.0.0" github.com/derekparker/delve/cmd/dlvcodesign -s "dlv-cert" ./dlv==> Cleaning==> Finishing upln -s ../Cellar/delve/1.0.0/bin/dlv dlv
沒毛病,終於成功了(可以看到提示認證已經安裝了,不需要再建立了)
事實上,你如果用這個認證再對手動安裝的dlv進行簽名也是可以的(筆者親測可用,看來手動產生的認證確實是有問題的,雖然我也沒去研究問題在哪)
再次啟動調試,搞定,如:
螢幕快照 2018-08-30 11.23.33.png