使用GDB调试Golang?

huangapple go评论87阅读模式
英文:

Golang debug using GDB?

问题

我有两个关于GDB + golang的问题:

1)Go构建GCC标志

当我运行"go build"时,Go构建器使用哪些gcc标志来构建程序?构建值与go环境中设置的"GOGCCFLAGS"相同吗?

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

因为我没有看到任何添加代码符号的"-g"或"-g3"标志。如果有的话,如何编译符号表?

2)如何在GDB中打印一个值
我按照这里的教程GDB调试go教程,但似乎打印出来的值不是我设置的值。

使用GDB调试Golang?

顺便说一下,我注意到有一个关于它的帖子gdb调试go
然而,对我也不起作用。
使用GDB调试Golang?

英文:

I got 2 questions about GDB + golang?

1) Go build GCC flags 

when I run "go build" , which gcc flags do the Go builder use to build a program? The build value is same as the "GOGCCFLAGS" set in the go envionment?

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

because I don't see any "-g" or "-g3" flags for adding code symbol. If yes, how could the symbol table be compiled ?

  1. How to print a value in GDB
    I followed the tutorial here GDB debug go tutorial, but it seems the value is not what I set.

使用GDB调试Golang?

By the way, I noticed there is a post about it gdb debug go
However, doesn't work for me either.
使用GDB调试Golang?

答案1

得分: 15

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

将其保存为main.go。然后使用以下gcflag标志进行编译。

go build -gcflags "-N"

使用新构建的Golang应用程序打开gdb。

gdb gdbtest
# 或者
gdb <PROJECT_NAME>

现在你完全控制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}

以上是你要翻译的内容。

英文:

Golang now works well with GDB

Here is an example golang app gdbtest

- gdbtest/
  - main.go

Take the following example main.go

package main

import &quot;fmt&quot;

type MyStruct struct {
	x string
	i int
	f float64
}

func main() {
	x := &quot;abc&quot;
	i := 3
	fmt.Println(i)
	fmt.Println(x)

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

Save that to main.go. Then compile with the follwing gcflag flag.

go build -gcflags &quot;-N&quot;

Open gdb with your newly built golang app

gdb gdbtest
# or 
gdb &lt;PROJECT_NAME&gt;

You now have full control of gdb. For example, add a breakpoint with br &lt;linenumber&gt; command, then execute the app with 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)

Now you can print all the local variables

(gdb) info locals
i = 3
ms = 0x20819e020
x = 0xdb1d0 &quot;abc&quot;

Even get access to the pointers

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

答案2

得分: 6

接受的答案已过时。
如果使用标志-gcflags=all="-N -l"进行构建,Golang目前可以与GDB一起使用(包括本地变量),如官方文档所述。

英文:

The accepted answer is outdated.
Golang currently works with GDB (including locals) if you build with the flags -gcflags=all=&quot;-N -l&quot;, as described on the official documentation

答案3

得分: 5

Go与GDB不兼容,其中一个已知的问题是值的打印。

更多详细信息可以在这里找到。

英文:

Go does not work well with GDB and one of the known problems is the printing of values.

More details can be found here.

huangapple
  • 本文由 发表于 2014年9月2日 11:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/25615421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定