golang gdb - 打印变量

我有 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) 

您可以看到在检查 'p' 变量时存在 Python 异常,而在显示 'i' 的值时完全不是 3。怎么了 ?




慕哥6287543
浏览 289回答 2
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) .另一种选择是使用您自己的跟踪功能,虽然不漂亮,但它是目前 go 1.3+ 唯一真正的调试选项。我个人使用这样的东西进行调试:var traceLock sync.Mutexfunc trace(a ...interface{}) {&nbsp; &nbsp; traceLock.Lock()&nbsp; &nbsp; pc, f, ln, ok := runtime.Caller(1)&nbsp; &nbsp; fn := ""&nbsp; &nbsp; if ok {&nbsp; &nbsp; &nbsp; &nbsp; fn = runtime.FuncForPC(pc).Name()&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("trace: %s %s:%d", fn, filepath.Base(f), ln)&nbsp; &nbsp; if len(a) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(append([]interface{}{": "}, a...)...)&nbsp; &nbsp; }&nbsp; &nbsp; traceLock.Unlock()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go