1. debug by gdb:html
office doclinux
download the runtime-gdb file.git
$ wget -q -O - https://golang.org/src/runtime/runtime-gdb.py |grep '<span id="L' > runtime-gdb.pygithub
處理脫字符golang
$ sed -i -e "s/<span.*span>//g" -e "s/<pre>//g" -e "s/^\t//g" -e 's/"/"/g' -e "s/'/'/g" -e "s/>/>/g" -e "s/</</g" -e "s/amp;//g" runtime-gdb.py bash
linux 下應該有脫字符處理工具(iconv)。 或者wget 自己應該能處理掉脫字符。ide
run gdb by: 工具
$ $GOROOT=`go env |grep GOROOT |cut -d "=" -f2`ui
$ gdb your_bin -d $GOROOTidea
$ gdb cpuinfo_main -d `go env |grep GOROOT |cut -d "=" -f2`
source ~/go/src/runtime/runtime-gdb.py
發現最新的golang1.6已經自帶了runtime-gdb.py
進入gdb
(gdb) add-auto-load-safe-path /usr/local/go/src/runtime/runtime-gdb.py
echo "set auto-load safe-path /" > line to your configuration file "/home/shaohef//.gdbinit".
直接運行
$ gdb your_bin
2. godebug
http://studygolang.com/articles/2899
Download:
$ go get -u github.com/mailgun/godebug
將 _ = "breakpoint" 插入代碼。
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 _ = "breakpoint" 9 fmt.Print("**********************\n") 10 }
build godbug
$ go build
mv godbug to goroot bin
$ sudo cp godebug /usr/local/go/bin/
$ debug
godbug run your go_file.
3. Go語言debug調試
http://studygolang.com/articles/2057
4. 採用idea IDE環境。
http://www.cnblogs.com/lingdhox/p/4189517.html
5. 使用Delve進行Golang代碼的調試
http://www.qingpingshan.com/jb/go/111271.html
$ dlv exec ./sample-controller -- -kubeconfig=$HOME/.kube/config
使用Delve調試test case
dlv test --build-flags='github.com/dlsniper/u/tmp/mypack' -- -test.run ^TestHello$
https://github.com/derekparker/delve/issues/422
1 package main 2 3 import ( 4 "fmt" 5 "runtime/debug" 6 ) 7 8 func test1() { 9 test2() 10 } 11 12 func test2() { 13 test3() 14 } 15 16 func test3() { 17 fmt.Printf("%s", debug.Stack()) 18 debug.PrintStack() 19 } 20 21 func main() { 22 test1() 23 }