2014-09-20 113 views
4

我有一個gdb沒有正確打印變量的問題。 簡單的方案是建立在以下方式:golang gdb - 打印變量

chmurson-osx:helloworld chmurson$ go build -gcflags '-N' start.go 

然後GDB執行:

chmurson-osx:helloworld chmurson$ gdb start -d $GOROOT 
GNU gdb (GDB) 7.8 
Copyright (C) 2014 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-apple-darwin14.0.0". 
Type "show configuration" for configuration details. 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 
Find the GDB manual and other documentation resources online at: 
<http://www.gnu.org/software/gdb/documentation/>. 
For help, type "help". 
Type "apropos word" to search for commands related to "word"... 
Reading symbols from start...done. 
warning: Missing auto-load scripts referenced in section .debug_gdb_scripts 
of file /Users/chmurson/Dev/goprojects/misc/src/helloworld/start 
Use `info auto-load python-scripts [REGEXP]' to list them. 
(gdb) 
(gdb) source /usr/local/go/src/pkg/runtime/runtime-gdb.py 
Loading Go Runtime support. 

這是我下一步:

(gdb) list 
1 package main 
2 
3 import "fmt" 
4 
5 func main() { 
6  x := "abc" 
7  i := 3 
8  fmt.Println(i) 
9  fmt.Println(x) 
10 } 
(gdb) b 9 
Breakpoint 1 at 0x2106: file /Users/chmurson/Dev/goprojects/misc/src/helloworld/start.go, line 9. 
(gdb) run 
Starting program: /Users/chmurson/Dev/goprojects/misc/src/helloworld/start 
3 
[New Thread 0x1113 of process 14039] 

Breakpoint 1, main.main() at /Users/chmurson/Dev/goprojects/misc/src/helloworld/start.go:9 
9  fmt.Println(x) 
(gdb) p x 
Python Exception <type 'exceptions.OverflowError'> signed integer is greater than maximum: 
$1 = 
(gdb) p i 
$2 = 8725692800 
(gdb) 

你可以看到有一個Python異常在檢查'p'變量時,顯示'i'的值時完全不是3。怎麼了 ?

這是我去的版本

chmurson-osx:helloworld chmurson$ go version 
go version go1.3.1 darwin/amd64 

和GDB配置

(gdb) show configuration 
This GDB was configured as follows: 
    configure --host=x86_64-apple-darwin14.0.0 --target=x86_64-apple-darwin14.0.0 
      --with-auto-load-dir=:${prefix}/share/auto-load 
      --with-auto-load-safe-path=:${prefix}/share/auto-load 
      --with-expat 
      --with-gdb-datadir=/usr/local/share/gdb (relocatable) 
      --with-jit-reader-dir=/usr/local/lib/gdb (relocatable) 
      --without-libunwind-ia64 
      --without-lzma 
      --with-python=/System/Library/Frameworks/Python.framework/Versions/2.7 
      --without-guile 
      --with-separate-debug-dir=/usr/local/lib/debug (relocatable) 
      --with-zlib 
      --without-babeltrace 

("Relocatable" means the directory can be moved with the GDB installation 
tree, and GDB will still find it.) 

回答

2

添加到@AlexAtNet的答案中,Go 1.2.x之後的所有內容都打破了GDB的支持,所以要麼使用go 1.2.x進行調試,要麼使用gccgo(請記住,gcc 4.8.x支持go 1.1,gcc 4.9.x是最多1.2)。

另一種選擇是使用自己的跟蹤功能,但不是很漂亮,但它目前是1.3+的唯一真正的調試選項。

我個人使用這樣的調試:

var traceLock sync.Mutex 

func trace(a ...interface{}) { 
    traceLock.Lock() 
    pc, f, ln, ok := runtime.Caller(1) 
    fn := "" 
    if ok { 
     fn = runtime.FuncForPC(pc).Name() 
    } 
    fmt.Printf("trace: %s %s:%d", fn, filepath.Base(f), ln) 
    if len(a) > 0 { 
     fmt.Println(append([]interface{}{": "}, a...)...) 
    } 
    traceLock.Unlock() 
} 

playground

+1

降級轉到1.2.x的確有一招。 Gdb調試器非常漂亮地打印這些變量。另外Go運行時間源自動加載,無需每次都手動插入它們。 Go 1.3並不是必須要求我,因爲我是Go world的新手,所以我很高興。稍後將嘗試查看gccgo的工作方式。謝謝! – chmurson 2014-09-21 19:17:35

1

的GDB的支持是不是爲圍棋隊優先所以這是不可能的這樣的問題的數量將固定不久。考慮以下幾點:

https://golang.org/doc/gdb

GDB不明白圍棋程序很好。堆棧管理,線程和運行時包含與執行模型相差甚遠的方面GDB希望即使在使用gccgo編譯程序時,他們也會混淆調試器。因此,雖然GDB在某些情況下很有用,但它並不是Go程序的可靠調試器,尤其是高度併發的調試器。此外,圍繞這些困難的問題,Go項目並不是一個優先事項。

但是,您可以使用Gccgo工具鏈併成功調試go程序。但是,在Mac上安裝它有點麻煩。

+1

根據當前時刻的gcc版本https://gcc.gnu.org/gcc-4.9/changes的最新文檔。 html最多隻支持Go 1.2.1。 所以我現在不得不忘掉Go 1.3 – chmurson 2014-09-21 19:14:48