我在Golang中创建了一个示例项目:
sampleapp/
sampleapp/main.go
具有以下代码:
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)
}
我关注了https://code.visualstudio.com/docs/editor/debugging,并创建了以下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": []
}
]
}
当我在主机上使用启动文件调试模式设置断点时-我能够单步执行程序,并且可以看到用户名和 age的值如下:
但是当我像这样构建应用程序后使用启动exec 调试模式时:
go build
我可以单步执行代码,但是 Local 部分由于微调器持续运行而挂起,并且没有显示以下任何局部变量:
我必须做一个重新加载窗口,以摆脱悬挂的Local vars微调框。我检查了vscode问题,看到了https://github.com/microsoft/vscode-go/issues/2444,但在这种情况下,当出现紧急情况时不会显示变量。但就我而言,我只是打印两个变量。
我认为这可能与我的 dlv 安装有关,但是当我对可执行文件使用 dlv 进行调试时,我能够获取这些值(从中删除不必要的行 dlv 输出):
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
在这里我缺少一些非常基本的东西吗?
答案 0 :(得分:1)
我最初可以重现您的问题,但是在升级到VSCode 1.35.1版(我在MacOSX上)之后,该问题消失了。我可以在调试过程中使用“启动exec”看到变量值
我认为编译器优化使它成为问题。对于使用生成的可执行文件应该可以正常工作
go build -gcflags="-N -l"
-N: disable optimization
-l: disable inlining
Golang也正式提出了建议:https://golang.org/doc/gdb