douzengjian1535
douzengjian1535
采纳率100%
2019-06-02 10:53

调试Golang可执行文件时,无法在vscode中获取局部变量

已采纳

I create a sample project in Golang:

sampleapp/
sampleapp/main.go

which has the following code:

package main

import (
    "flag"
    "fmt"
)

func main() {
    var name = flag.String("name", "user1", "user name")
    var age = flag.Int("age", 20, "user age")
    fmt.Println(*name)
    fmt.Println(*age)
}

I followed https://code.visualstudio.com/docs/editor/debugging and created the following launch.json

 {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/main.go",
            "env": {},
            "args": []
        },
        {
            "name": "Launch exec",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "program": "${workspaceFolder}/sampleapp",
            "env": {},
            "args": []
        }
    ]
}

When I use Launch File debug mode setting breakpoints on main - I am able to single step through the program and can see the values of username and age as follows:

enter image description here

But when I use Launch exec debug mode after building the app like so:

go build

I can single step through the code but the Local section hangs with a spinner going on continuously and doesn't show any local variables as per the following:

enter image description here

I have to do a Reload window to get rid of the hanging Local vars spinner. I checked vscode issues and saw https://github.com/microsoft/vscode-go/issues/2444 but in that case the variables are not shown when there is a panic. But in my case, I am just printing 2 variables.

I thought it might be an issue with my dlv installation but when I debugged using dlv with the executable, I was able to get the values (deleting unnecessary lines from dlv output):

dlv exec sampleapp
Type 'help' for list of commands.
(dlv) break main.main
Breakpoint 1 set at 0x109e1f3 for main.main() ./main.go:8
(dlv) s
> main.main() ./main.go:8 (hits goroutine(1):1 total:1) (PC: 0x109e1f3)
Warning: debugging optimized function
     3: import (
     4:         "flag"
     5:         "fmt"
     6: )
     7:
=>   8: func main() {
     9:         var name = flag.String("name", "user1", "user name")
    10:         var age = flag.Int("age", 20, "user age")
    11:         fmt.Println(*name)
    12:         fmt.Println(*age)
    13: }
(dlv) s
> main.main() ./main.go:9 (PC: 0x109e202)
Warning: debugging optimized function
     4:         "flag"
     5:         "fmt"
     6: )
     7:
     8: func main() {
=>   9:         var name = flag.String("name", "user1", "user name")
    10:         var age = flag.Int("age", 20, "user age")
    11:         fmt.Println(*name)
    12:         fmt.Println(*age)
    13: }
(dlv) s
> main.main() ./main.go:11 (PC: 0x109e29f)
Warning: debugging optimized function
     6: )
     7:
     8: func main() {
     9:         var name = flag.String("name", "user1", "user name")
    10:         var age = flag.Int("age", 20, "user age")
=>  11:         fmt.Println(*name)
    12:         fmt.Println(*age)
    13: }
(dlv) stepout
user1
> main.main() ./main.go:12 (PC: 0x109e31a)
Warning: debugging optimized function
     7:
     8: func main() {
     9:         var name = flag.String("name", "user1", "user name")
    10:         var age = flag.Int("age", 20, "user age")
    11:         fmt.Println(*name)
=>  12:         fmt.Println(*age)
    13: }
(dlv) p name
*"user1"
(dlv) p age
*20

Is there something very basic that I am missing here?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzhan1492 dongzhan1492 2年前

    I could reproduce your issue initially but after I upgrade to VSCode Version 1.35.1 (I am on MacOSX), the issue is gone. I can see the variable values during debug with "Launch exec"

    I think the compiler optimisation made it problematic. Should works fine for executable built with go build -gcflags="-N -l"

    -N: disable optimization -l: disable inlining

    Golang officially suggested it as well: https://golang.org/doc/gdb

    点赞 评论 复制链接分享

相关推荐