使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

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

Debugging Go with tags in Visual Studio Code and Delve debugger

问题

根据putus的答案,我找到了以下配置,可以实现一键构建和调试。

首先,你需要添加一个任务来使用相应的标签构建二进制文件。

  1. {
  2. "version": "0.1.0",
  3. "command": "bash",
  4. "isShellCommand": true,
  5. "args": [""],
  6. "showOutput": "always",
  7. "tasks": [
  8. {
  9. "taskName": "buildBinWithTag",
  10. "command": "go",
  11. "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
  12. "isShellCommand": true
  13. }
  14. ]
  15. }

这个任务应该在调试器启动之前执行。

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "DebugBinWithTag", // 添加的配置
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "exec",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${workspaceRoot}/BinaryName",
  13. "env": {},
  14. "args": [],
  15. "showLog": true,
  16. "preLaunchTask": "buildBinWithTag"
  17. }
  18. ]
  19. }

原始问题:我正在使用构建标签来编译不同版本的Go程序,并使用"go build -tags THISISAFLAG"进行编译。

  1. //+build THISISAFLAG
  2. package main

这个方法完美地工作。但是有没有办法告诉调试器使用这些标签?我尝试使用以下的启动配置,但没有成功。

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Launch",
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${fileDirname}",
  13. "env": {},
  14. "args": ["-flags THISISAFLAG"],
  15. "showLog": true
  16. }
  17. ]
  18. }
英文:

Answer:
Based on putus answer, I figured out the following configuration to build and debug with one click

At first you need to add a task to build the binary with the respective tags.

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "0.1.0",
  5. "command": "bash",
  6. "isShellCommand": true,
  7. "args": [""],
  8. "showOutput": "always",
  9. "tasks": [
  10. {
  11. "taskName": "buildBinWithTag",
  12. "command": "go",
  13. "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
  14. "isShellCommand": true
  15. }
  16. ]
  17. }

This task should be executed before the debugger launches.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "DebugBinWithTag", //added config
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "exec",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${workspaceRoot}/BinaryName",
  13. "env": {},
  14. "args": [],
  15. "showLog": true,
  16. "preLaunchTask": "buildBinWithTag"
  17. }
  18. ]
  19. }

Original question:I'm using build tags for compiling different versions of a Go program and I compile it with "go build -tags THISISAFLAG"

  1. //+build THISISAFLAG
  2. package main

This works perfectly. But is there a way to tell the debugger to use these flags. I've tried to use a launch configuration like the following, but it didn't work.

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Launch",
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${fileDirname}",
  13. "env": {},
  14. "args": ["-flags THISISAFLAG"],
  15. "showLog": true
  16. }
  17. ]
  18. }

答案1

得分: 6

Visual Studio Code Go插件现在支持一个名为buildFlagslaunch.json键,允许您使用相应值"-tags Tag"指定构建标签(存在一个不允许多个标签的错误)。

插件Wiki中相关的摘录如下:

如果您的构建需要构建标签(例如go build -tags whatever_tag),则添加参数buildFlags,内容为"-tags whatever_tag"

如果您有不同的构建配置,每个配置都需要自己的构建标签,您可以为每个配置创建单独的启动配置。

英文:

The Visual Studio Code Go plugin now supports a launch.json key called buildFlags that allows you to specify the build tags with a corresponding value of "-tags Tag". (There appears to be a bug disallowing multiple tags.).

Relevant excerpt from the plugin Wiki:

> If your build needs build tags (e.g. go build -tags whatever_tag), then add the parameter buildFlags with the content "-tags whatever_tag".

If you have different build configurations each requiring their own build tag, you can create separate launch configurations for each.

答案2

得分: 1

你可以将预构建的二进制文件附加到调试器上。

  1. 通过命令行构建应用程序,例如 go build -o myapp.exe -tags THISISAFLAG
  2. launch.json 中添加配置 Launch Exe
  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Launch Debug", //现有配置
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${fileDirname}",
  13. "env": {},
  14. "args": [],
  15. "showLog": true
  16. },
  17. {
  18. "name": "Launch EXE", //新增配置
  19. "type": "go",
  20. "request": "launch",
  21. "mode": "exec",
  22. "remotePath": "",
  23. "port": 2345,
  24. "host": "127.0.0.1",
  25. "program": "${workspaceRoot}/myapp.exe",
  26. "env": {},
  27. "args": [],
  28. "showLog": true
  29. }
  30. ]
  31. }

注意:

由于编译器优化和此问题,在调试会话期间某些变量可能不会显示或以不同的名称显示(如下图所示)。将来,您可以在构建应用程序时添加 -gcflags='-N -l' 来禁用编译器优化。

使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

英文:

You can attach pre-built binary to debugger.

  1. Build the application from command line, e.g. go build -o myapp.exe -tags THISISAFLAG

  2. Add configuration Launch Exe to launch.json

    1. {
    2. "version": "0.2.0",
    3. "configurations": [
    4. {
    5. "name": "Launch Debug", //existing config
    6. "type": "go",
    7. "request": "launch",
    8. "mode": "debug",
    9. "remotePath": "",
    10. "port": 2345,
    11. "host": "127.0.0.1",
    12. "program": "${fileDirname}",
    13. "env": {},
    14. "args": [],
    15. "showLog": true
    16. },
    17. {
    18. "name": "Launch EXE", //added config
    19. "type": "go",
    20. "request": "launch",
    21. "mode": "exec",
    22. "remotePath": "",
    23. "port": 2345,
    24. "host": "127.0.0.1",
    25. "program": "${workspaceRoot}/myapp.exe",
    26. "env": {},
    27. "args": [],
    28. "showLog": true
    29. }
    30. ]
    31. }

Note:

Due to compiler optimization and this issue, some variables may not being displayed or displayed with different name during debug session (see below). In the future, you may add -gcflags='-N -l' when building the application to disable compiler optimization.

使用Visual Studio Code和Delve调试器在Go中使用标签进行调试

答案3

得分: 0

以下是我的测试配置:

  1. {
  2. "name": "Delve: Test",
  3. "type": "go",
  4. "request": "launch",
  5. "mode": "test",
  6. "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
  7. "program": "${file}",
  8. "showLog": true
  9. }
  1. 除非所选文件中不存在这些标签,否则它将持续出错。
  2. 根据您的用例,您可能需要将"${file}"更改为"${fileDirname}",如果您使用后者,至少一个文件应该具有所提到的标签。
英文:

Here are my test configurations:

  1. {
  2. "name": "Delve: Test",
  3. "type": "go",
  4. "request": "launch",
  5. "mode": "test",
  6. "buildFlags": "-tags 'unit_tests integration_tests all_tests'",
  7. "program": "${file}",
  8. "showLog": true
  9. }
  1. It will keep erroring out unless the tags don't exist in the selected file.
  2. You may want to change "${file}" to "${fileDirname}" depending on your usecase, if you are using the latter, at least one of the files should have the mentioned tag.

huangapple
  • 本文由 发表于 2017年4月24日 15:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/43582024.html
匿名

发表评论

匿名网友

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

确定