如何在GDB中将地址转换为类型并打印出来,用于Golang。

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

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

  1. 安装delve
go get -u github.com/go-delve/delve/cmd/dlv
  1. 切换到main.go所在的路径
cd $GOPATH/src/YOUPROJECT/main.go
  1. 调试delve
$GOPATH/bin/delve debug main.go
  1. 在main函数设置断点
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x4a7bbf for main.main() main.go:12
  1. 运行
(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: }
  1. 在第14行设置断点
(dlv) b 14
Breakpoint 2 set at 0x4a7bf0 for main.main() main.go:14
  1. 继续执行
(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: }
  1. 显示变量值
(dlv) locals
testType = main.TestType {a: 1, b: 2}

最后,使用Goland、VSCode进行调试更加方便。

英文:

The delve tool is better than gdb. https://github.com/go-delve/delve

  1. install delve
go get -u github.com/go-delve/delve/cmd/dlv
  1. move path on main.go
cd $GOPATH/src/YOUPROJECT/main.go
  1. debug delve
$GOPATH/bin/delve debug main.go
  1. break point main
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x4a7bbf for main.main() main.go:12
  1. 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: }
  1. break point 14 line
(dlv) b 14
Breakpoint 2 set at 0x4a7bf0 for main.main() main.go:14
  1. 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: }
  1. show value
(dlv) locals
testType = main.TestType {a: 1, b: 2}

Finally, using goland, vscode is more convenient for debug.

huangapple
  • 本文由 发表于 2016年11月30日 15:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/40882206.html
匿名

发表评论

匿名网友

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

确定