使用gdb打印变量的Go代码。

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

Go: print variables with gdb

问题

在这个程序中,如何使用调试器中断执行并打印i的值?

package main

import "fmt"

func main() {
        x := "abc"
        i := 3
        fmt.Println(i)
        fmt.Println(x)
}

我无法打印i的值。但是我可以打印x的值:

go build test.go
gdb test  
[...]Reading symbols from /home/ned/test...done.
(gdb) br 9
(gdb) run
(gdb) p i
No symbol "i" in current context.
(gdb) p x
$1 = "abc"
英文:

In this program, how can break execution with the debugger and print the value of i?

package main

import "fmt"

func main() {
        x := "abc"
        i := 3
        fmt.Println(i)
        fmt.Println(x)
}

I can't print i. However I can print x:

go build test.go
gdb test  
[...]Reading symbols from /home/ned/test...done.
(gdb) br 9
(gdb) run
(gdb) p i
No symbol "i" in current context.
(gdb) p x
$1 = "abc"

答案1

得分: 4

它听起来像是变量i可能被编译器优化掉了。你尝试过使用调试构建吗?

你可以使用go build -gcflags '-N'

英文:

It sounds like the variable i probably got optimized out of existence by the compiler. Did you try a debug build?

You can use go build -gcflags '-N'.

huangapple
  • 本文由 发表于 2012年5月22日 07:15:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/10693831.html
匿名

发表评论

匿名网友

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

确定