英文:
How to cast the address to a type and print it in GDB for Golang
问题
这是一段代码:
package main
import (
"fmt"
)
type TestType struct {
a int
b int
}
func main() {
var testType TestType = TestType{1, 2}
fmt.Println(testType)
}
这是 gdb 的调试输出:
(gdb) r
Starting program: /home/bzhang/common/src/go/src/test/testBinary
Breakpoint 1, main.main () at /home/bzhang/common/src/go/src/test/main.go:14
14 fmt.Println(testType)
(gdb) p testType
$1 = {a = 1, b = 2}
(gdb) p &testType
$2 = (main.TestType *) 0xc820059ee8
(gdb) p ('main.TestType') 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) p ('TestType') 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) whatis testType
type = main.TestType
(gdb)
当然,我知道可以直接打印 testType。但是如果它是一个局部变量,有时候它的值无法直接打印出来,只能打印它的地址。所以我想打印它的值,并指示其类型。但是似乎没有正确工作。感谢您的帮助!
英文:
This is the piece of code:
package main
import (
"fmt"
)
type TestType struct {
a int
b int
}
func main() {
var testType TestType = TestType{1, 2}
fmt.Println(testType)
}
And this is the gdb debug output:
(gdb) r
Starting program: /home/bzhang/common/src/go/src/test/testBinary
Breakpoint 1, main.main () at /home/bzhang/common/src/go/src/test/main.go:14
14 fmt.Println(testType)
(gdb) p testType
$1 = {a = 1, b = 2}
(gdb) p &testType
$2 = (main.TestType *) 0xc820059ee8
(gdb) p ('main.TestType'*) 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) p ('TestType'*) 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) whatis testType
type = main.TestType
(gdb)
Of course I know that I can print testType directly. But if it is a local variable, sometimes its value can not be printed out directly, and only its address is available. So I want to print its value with the indication of its type. But it seems not work correctly.
Appreciate for your help!
答案1
得分: 1
The delve工具比gdb更好。https://github.com/go-delve/delve
- 安装delve
go get -u github.com/go-delve/delve/cmd/dlv
- 切换到main.go所在的路径
cd $GOPATH/src/YOUPROJECT/main.go
- 调试delve
$GOPATH/bin/delve debug main.go
- 在main函数设置断点
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x4a7bbf for main.main() main.go:12
- 运行
(dlv) c
> main.main() main.go:12 (hits goroutine(1):1 total:1) (PC: 0x4a7bbf)
7: type TestType struct {
8: a int
9: b int
10: }
11:
=> 12: func main() {
13: var testType TestType = TestType{1, 2}
14: fmt.Println(testType)
15: }
- 在第14行设置断点
(dlv) b 14
Breakpoint 2 set at 0x4a7bf0 for main.main() main.go:14
- 继续执行
(dlv) c
> main.main() main.go:14 (hits goroutine(1):1 total:1) (PC: 0x4a7bf0)
9: b int
10: }
11:
12: func main() {
13: var testType TestType = TestType{1, 2}
=> 14: fmt.Println(testType)
15: }
- 显示变量值
(dlv) locals
testType = main.TestType {a: 1, b: 2}
最后,使用Goland、VSCode进行调试更加方便。
英文:
The delve tool is better than gdb. https://github.com/go-delve/delve
- install delve
go get -u github.com/go-delve/delve/cmd/dlv
- move path on main.go
cd $GOPATH/src/YOUPROJECT/main.go
- debug delve
$GOPATH/bin/delve debug main.go
- break point main
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x4a7bbf for main.main() main.go:12
- run
(dlv) c
> main.main() main.go:12 (hits goroutine(1):1 total:1) (PC: 0x4a7bbf)
7: type TestType struct {
8: a int
9: b int
10: }
11:
=> 12: func main() {
13: var testType TestType = TestType{1, 2}
14: fmt.Println(testType)
15: }
- break point 14 line
(dlv) b 14
Breakpoint 2 set at 0x4a7bf0 for main.main() main.go:14
- continue
(dlv) c
> main.main() main.go:14 (hits goroutine(1):1 total:1) (PC: 0x4a7bf0)
9: b int
10: }
11:
12: func main() {
13: var testType TestType = TestType{1, 2}
=> 14: fmt.Println(testType)
15: }
- show value
(dlv) locals
testType = main.TestType {a: 1, b: 2}
Finally, using goland, vscode is more convenient for debug.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论