2014-09-02 148 views
3

我有2個關於GDB + golang的問題?Golang使用GDB進行調試?

1) Go build GCC flags 

當我運行「go build」,哪個gcc標誌做Go生成器用來構建程序?構建值與在環境中設置的「GOGCCFLAGS」相同?

GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common" 

因爲我沒有看到用於添加代碼符號的任何「-g」或「-g3」標誌。如果是,那麼符號表怎麼編譯?

2)如何在GDB中打印一個值 我按照這裏的教程GDB debug go tutorial,但它的價值似乎不是我設定的。

The value print 1, while actual is 1024

順便說一句,我注意到有一個關於它的帖子gdb debug go 然而,對我也不起作用。 the value is not what I set

回答

4

Go在GDB中效果不佳,其中一個已知問題是打印值。

更多細節請見here

+0

謝謝,有什麼選擇,而不是GDB? – python 2014-09-02 15:20:47

+4

有沒有其他的選擇 - 看看[這裏](http://stackoverflow.com/questions/16492509/does-any-golang-interactive-debugger-exist/23387017#23387017) – metakeule 2014-09-02 17:05:22

+3

我試過去了1.4.rc2它似乎與gdb一起工作。 – 2014-12-10 02:40:37

9

Golang現在GDB

效果很好

下面是一個例子golang應用gdbtest

- gdbtest/ 
    - main.go 

看看下面的例子main.go

package main 

import "fmt" 

type MyStruct struct { 
    x string 
    i int 
    f float64 
} 

func main() { 
    x := "abc" 
    i := 3 
    fmt.Println(i) 
    fmt.Println(x) 

    ms := &MyStruct{ 
     x: "cba", 
     i: 10, 
     f: 11.10335, 
    } 
    fmt.Println(ms) 
} 

保存,爲爲主。去。然後用下面的gcflag標誌進行編譯。

go build -gcflags "-N"

與新建golang應用

gdb gdbtest 
# or 
gdb <PROJECT_NAME> 

打開GDB你現在有GDB的完全控制。例如,添加一個斷點與br <linenumber>命令,然後用run

(gdb) br 22 
Breakpoint 1 at 0x2311: file /go/src/github.com/cevaris/gdbtest/main.go, line 22. 
(gdb) run 
Starting program: /go/src/github.com/cevaris/gdbtest/gdbtest 
3 
abc 

Breakpoint 1, main.main() at /go/src/github.com/cevaris/gdbtest/main.go:22 
22    fmt.Println(ms) 
(gdb) 

執行的應用程序現在可以打印所有的局部變量

(gdb) info locals 
i = 3 
ms = 0x20819e020 
x = 0xdb1d0 "abc" 

即便可以訪問指針

(gdb) p ms 
$1 = (struct main.MyStruct *) 0x20819e020 
(gdb) p *ms 
$2 = {x = 0xdb870 "cba", i = 10, f = 11.103350000000001}