英文:
golang gdb - print variables
问题
我有一个关于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
版权所有 (C) 2014 Free Software Foundation, Inc.
GNU GPL版本3或更高版本的许可证 <http://gnu.org/licenses/gpl.html>
这是自由软件:您可以自由更改和重新发布它。
根据法律的规定,没有任何保证。输入“show copying”和“show warranty”以获取详细信息。
此GDB配置为“x86_64-apple-darwin14.0.0”。
输入“show configuration”以获取配置详细信息。
有关错误报告说明,请参见:
<http://www.gnu.org/software/gdb/bugs/>。
在线查找GDB手册和其他文档资源:
<http://www.gnu.org/software/gdb/documentation/>。
输入“help”获取帮助。
输入“apropos word”以搜索与“word”相关的命令...
读取符号从start...完成。
警告:在文件/Users/chmurson/Dev/goprojects/misc/src/helloworld/start的部分.debug_gdb_scripts节中引用的自动加载脚本丢失
使用“info auto-load python-scripts [REGEXP]”列出它们。
(gdb)
(gdb) source /usr/local/go/src/pkg/runtime/runtime-gdb.py
加载Go运行时支持。
接下来是我要做的:
(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
在文件/Users/chmurson/Dev/goprojects/misc/src/helloworld/start.go的第9行设置断点。
(gdb) run
正在启动程序:/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异常 <type 'exceptions.OverflowError'> signed integer is greater than maximum:
$1 =
(gdb) p i
$2 = 8725692800
(gdb)
你可以看到在检查变量'p'时出现了Python异常,而在显示'i'的值时完全不是3。出了什么问题?
这是我的go版本:
chmurson-osx:helloworld chmurson$ go version
go version go1.3.1 darwin/amd64
和gdb配置:
(gdb) show configuration
此GDB的配置如下:
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
(“可重定位”意味着该目录可以与GDB安装树一起移动,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.)
答案1
得分: 2
GDB支持对于Go团队来说并不是一个优先事项,因此不太可能很快修复这类问题的数量。请参考以下内容:
> GDB对Go程序的理解能力有限。堆栈管理、线程和运行时包含的方面与GDB预期的执行模型有足够的差异,这可能会使调试器混淆,即使程序是使用gccgo编译的。因此,尽管在某些情况下GDB可能有用,但它不是Go程序的可靠调试器,尤其是在涉及大量并发的情况下。此外,解决这些问题并不是Go项目的优先事项,因为它们很困难。
然而,您可以使用Gccgo工具链成功调试Go程序。不过,在Mac上安装它有点麻烦。
英文:
The GDB support is not a priority for the Go team so it is unlikely that the number of such issues will be fixed soon. Consider the following:
> GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger, even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations, it is not a reliable debugger for Go programs, particularly heavily concurrent ones. Moreover, it is not a priority for the Go project to address these issues, which are difficult.
However, you can use the Gccgo toolchain and successfully debug the go programs. However, installing it on a Mac is a bit cumbersome.
答案2
得分: 2
根据@AlexAtNet的回答,Go 1.2.x之后的版本破坏了GDB的支持,所以要么使用go 1.2.x进行调试,要么使用gccgo(请记住,gcc 4.8.x支持go 1.1,gcc 4.9.x支持go 1.2)。
另一个选择是使用自己的跟踪函数,虽然不太美观,但目前是go 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()
}
[kbd]playground/kbd
英文:
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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论