douxian7117 2014-09-20 16:25
浏览 102
已采纳

golang gdb-打印变量

I have a problem with gdb not printing variables correctly. Simple program is build in following way:

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

And then gdb executed:

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.

Here is what I do next:

(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) 

You can see that there is Python Exception on examination of 'p' variable, and totally not 3 when displaying value of 'i'. What's wrong ?

Here is my go version

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

And gdb configuration

(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条回答 默认 最新

  • douqin231881 2014-09-21 00:19
    关注

    Adding to @AlexAtNet's answer, everything after Go 1.2.x breaks GDB support, so either use go 1.2.x for debugging or use gccgo (keep in mind that gcc 4.8.x supports go 1.1, gcc 4.9.x is up to 1.2).

    Another option is to use your own trace function, not pretty but it's currently the only real debug option for go 1.3+.

    I personally use something like this for debugging:

    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()
    }
    

    <kbd>playground</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)