golang debug调试

1. debug by gdb:

office doc

 download the runtime-gdb file.

$ wget -q -O - https://golang.org/src/runtime/runtime-gdb.py  |grep '<span id="L' >  runtime-gdb.py

处理脱字符

$ sed -i -e "s/<span.*span>//g" -e "s/<pre>//g" -e "s/^\t//g" -e 's/&#34;/"/g' -e "s/&#39;/'/g" -e "s/&gt;/>/g" -e "s/&lt;/</g" -e "s/amp;//g" runtime-gdb.py 

linux 下应该有脱字符处理工具(iconv)。 或者wget 本身应该能处理掉脱字符。

 

run gdb by: 

$GOROOT=`go env  |grep GOROOT |cut -d "=" -f2`

$ gdb your_bin -d $GOROOT

$ 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 }
View Code

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代码的调试

git repo: https://github.com/go-delve/delve

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

6. 代码中打印栈信息

 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 }
View Code

 

posted @ 2017-03-07 19:42  lvmxh  阅读(13266)  评论(0编辑  收藏  举报