douzengjian1535 2019-06-02 10:53 采纳率: 100%
浏览 285
已采纳

调试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 2019-06-14 07:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件