使用VSCode、Go和Nakama进行远程调试

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

Remote debug with VSCode, go & nakama

问题

我有以下问题。我尝试使用Docker在Go中运行Nakama游戏服务器。
为了调试目的,我想使用delve。

我不确定我是否在正确进行,所以也许我的问题实际上是完全不同的。但是我认为我的delve没有连接到nakama构建。

到目前为止我做了什么?
我创建了一个新的Go项目,并在main.go中放了一些代码。之后我创建了一个Dockerfile和一个docker-compose.yml。
我认为这两个文件中有一个错误。
我的Dockerfile如下所示:

FROM golang

ENV GOPATH /home/marc/go_projects
ENV PATH ${GOPATH}/bin:/usr/local/go/bin:$PATH

RUN go install github.com/go-delve/delve/cmd/dlv@latest

FROM heroiclabs/nakama-pluginbuilder:3.3.0 AS go-builder

ENV GO111MODULE on
ENV CGO_ENABLED 1

WORKDIR $GOPATH/gamedev

COPY go.mod .
COPY main.go .
COPY vendor/ vendor/

RUN go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so

FROM heroiclabs/nakama:3.3.0

COPY --from=go-builder /backend/backend.so /nakama/data/modules/
COPY local.yml /nakama/data/

我的docker-compose.yml如下所示:

version: '3'
services:
  postgres:
    container_name: postgres
    image: postgres:9.6-alpine
    environment:
      - POSTGRES_DB=nakama
      - POSTGRES_PASSWORD=localdb
    volumes:
      - data:/var/lib/postgresql/data
    expose:
      - "8080"
      - "5432"
    ports:
      - "5432:5432"
      - "8080:8080"
  nakama:
    container_name: nakama
    image: heroiclabs/nakama:3.12.0
    entrypoint:
      - "/bin/sh"
      - "-ecx"
      - >
        /nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama &&
        exec /nakama/nakama --name nakama1 --database.address postgres:localdb@postgres:5432/nakama --logger.level DEBUG --session.token_expiry_sec 7200        
    restart: always
    links:
      - "postgres:db"
    depends_on:
      - postgres
    volumes:
      - ./:/nakama/data
    expose:
      - "7349"
      - "7350"
      - "7351"
      - "2345"
    ports:
      - "2345:2345"
      - "7349:7349"
      - "7350:7350"
      - "7351:7351"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7350/"]
      interval: 10s
      timeout: 5s
      retries: 5
volumes:
  data:

当我构建和运行Docker镜像时,它没有任何投诉地运行。我可以打开nakama的Web界面,所以这个运行得很好。
但是当我尝试连接调试器时,它看起来像是创建了一个成功的连接,但立即关闭了它。
所以我的launch.json配置如下:

 "name": "Connect to server",
 "type": "go",
 "request": "attach",
 "mode": "remote",
 "remotePath": "/home/marc/go_projects/bin/dlv",
 "port": 2345,
 "host": "127.0.0.1",
 "trace": "verbose"

这是我在/tmp/vs-code-debug.txt中得到的内容:

[07:24:05.882 UTC] From client: initialize({"clientID":"vscode","clientName":"Visual Studio Code","adapterID":"go","pathFormat":"path","linesStartAt1":true,"columnsStartAt1":true,"supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"locale":"de","supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryReferences":true})
[07:24:05.882 UTC] InitializeRequest
[07:24:05.882 UTC] To client: {"seq":0,"type":"response","request_seq":1,"command":"initialize","success":true,"body":{"supportsConditionalBreakpoints":true,"supportsConfigurationDoneRequest":true,"supportsSetVariable":true}}
[07:24:05.883 UTC] InitializeResponse
[07:24:05.883 UTC] From client: attach({"name":"Connect to server","type":"go","request":"attach","mode":"remote","remotePath":"/home/marc/go_projects/bin/dlv","port":2345,"host":"127.0.0.1","trace":"verbose","__configurationTarget":5,"packagePathToGoModPathMap":{"/home/marc/go_projects/gamedev":"/home/marc/go_projects/gamedev"},"debugAdapter":"legacy","showRegisters":false,"showGlobalVariables":false,"substitutePath":[],"showLog":false,"logOutput":"debugger","dlvFlags":[],"hideSystemGoroutines":false,"dlvLoadConfig":{"followPointers":true,"maxVariableRecurse":1,"maxStringLen":64,"maxArrayValues":64,"maxStructFields":-1},"cwd":"/home/marc/go_projects/gamedev","dlvToolPath":"/home/marc/go_projects/bin/dlv","env":{"ELECTRON_RUN_AS_NODE":"1","GJS_DEBUG_TOPICS":"JS ERROR;JS LOG","USER":"marc","SSH_AGENT_PID":"1376","XDG_SESSION_TYPE":"x11","SHLVL":"0","HOME":"/home/marc","DESKTOP_SESSION":"ubuntu","GIO_LAUNCHED_DESKTOP_FILE":"/usr/share/applications/code.desktop","GTK_MODULES":"gail:atk-bridge","GNOME_SHELL_SESSION_MODE":"ubuntu","MANAGERPID":"1053","DBUS_SESSION_BUS_ADDRESS":"unix:path=/run/user/1000/bus","GIO_LAUNCHED_DESKTOP_FILE_PID":"6112","IM_CONFIG_PHASE":"1","MANDATORY_PATH":"/usr/share/gconf/ubuntu.mandatory.path","LOGNAME":"marc","_":"/usr/share/code/code","JOURNAL_STREAM":"8:44286","DEFAULTS_PATH":"/usr/share/gconf/ubuntu.default.path","XDG_SESSION_CLASS":"user","USERNAME":"marc","GNOME_DESKTOP_SESSION_ID":"this-is-deprecated","WINDOWPATH":"2","PATH":"/home/marc/.nvm/versions/node/v17.8.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/local/go/bin:/usr/local/go/bin","SESSION_MANAGER":"local/mobile:@/tmp/.ICE-unix/1458,unix/mobile:/tmp/.ICE-unix/1458","INVOCATION_ID":"fe605ca56aa646859602b81e264bf01b","XDG_RUNTIME_DIR":"/run/user/1000","XDG_MENU_PREFIX":"gnome-","DISPLAY":":0","LANG":"de_DE.UTF-8","XDG_CURRENT_DESKTOP":"Unity","XAUTHORITY":"/run/user/1000/gdm/Xauthority","XDG_SESSION_DESKTOP":"ubuntu","XMODIFIERS":"@im=ibus","SSH_AUTH_SOCK":"/run/user/1000/keyring/ssh","SHELL":"/bin/bash","QT_ACCESSIBILITY":"1","GDMSESSION":"ubuntu","GPG_AGENT_INFO":"/run/user/1000/gnupg/S.gpg-agent:0:1","GJS_DEBUG_OUTPUT":"stderr","QT_IM_MODULE":"ibus","PWD":"/home/marc","XDG_DATA_DIRS":"/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop","XDG_CONFIG_DIRS":"/etc/xdg/xdg-ubuntu:/etc/xdg","_WSREP_START_POSITION":"","CHROME_DESKTOP":"code-url-handler.desktop","ORIGINAL_XDG_CURRENT_DESKTOP":"ubuntu:GNOME","VSCODE_CWD":"/home/marc","GDK_BACKEND":"x11","VSCODE_NLS_CONFIG":"{\"locale\":\"de\",\"availableLanguages\":{\"*\":\"de\"},\"_languagePackId\":\"b61d3f473b0358bc955527db7340fd23.de\",\"_translationsConfigFile\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/tcf.json\",\"_cacheRoot\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de\",\"_resolvedLanguagePackCoreLocation\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/30d9c6cd9483b2cc586687151bcbcd635f373630\",\"_corruptedFile\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/corrupted.info\",\"_languagePackSupport\":true}","VSCODE_CODE_CACHE_PATH":"/home/marc/.config/Code/CachedData/30d9c6cd9483b2cc586687151bcbcd635f373630","VSCODE_IPC_HOOK":"/run/user/1000/vscode-432c1660-1.68.1-main.sock","VSCODE_PID":"6112","NVM_INC":"/home/marc/.nvm/versions/node/v17.8.0/include/node","LS_COLORS":"","NVM_DIR":"/home/marc/.nvm","LESSCLOSE":"/usr/bin/lesspipe %s %s","LESSOPEN":"| /usr/bin/lesspipe %s","NVM_CD_FLAGS":"","NVM_BIN":"/home/marc/.nvm/versions/node/v17.8.0/bin","GOPATH":"/home/marc/go_projects","VSCODE_AMD_ENTRYPOINT":"vs/workbench/api/node/extensionHostProcess","VSCODE_PIPE_LOGGING":"true","VSCODE_VERBOSE_LOGGING":"true","VSCODE_LOG_NATIVE":"false","VSCODE_HANDLES_UNCAUGHT_ERRORS":"true","VSCODE_LOG_STACK":"false","VSCODE_IPC_HOOK_EXTHOST":"/run/user/1000/vscode-ipc-8cf508cc-d427-4616-b6b5-61d3c3e5d99f.sock","APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL":"1","GOMODCACHE":"/home/marc/go_projects/pkg/mod","GOPROXY":"https://proxy.golang.org,direct"},"__sessionId":"1893ab9e-5a19-45e7-8b39-46db079cdbe3"})
[07:24:05.883 UTC] AttachRequest
[07:24:05.884 UTC] Start remote debugging: connecting 127.0.0.1:2345
[07:24:06.191 UTC] To client: {"seq":0,"type":"event","event":"initialized"}
[07:24:06.192 UTC] InitializeEvent
[07:24:06.192 UTC] To client: {"seq":0,"type":"response","request_seq":2,"command":"attach","success":true}
[07:24:06.194 UTC] [Error] Socket connection to remote was closed
[07:24:06.194 UTC] Sending TerminatedEvent as delve is closed
[07:24:06.194 UTC] To client: {"seq":0,"type":"event","event":"terminated"}
[07:24:06.201 UTC] From client: configurationDone(undefined)
[07:24:06.201 UTC] ConfigurationDoneRequest
[07:24:06.225 UTC] From client: disconnect({"restart":false})
[07:24:06.225 UTC] DisconnectRequest

我尝试多次更改launch.json中的远程路径,尝试匹配Docker文件中的路径。
也许我需要更改Docker中delve的实现,但是老实说我不太知道如何做。我真的找不到关于如何做这个的好文档。

英文:

i have the following problem. I try to run a nakama gameserver in go with docker.
For the debugging purposes i want to use delve.

I am not really sure if i am proceeding right, so maybe my problem is actually a complete different. But i think my delve does not connect to the nakama build.

What have i done so far?
I created a new go project and put a bit code in the main.go. After that i created a Dockerfile and an docker-compose.yml.
I think in one of these two files is the mistake.
My Dockerfile looks like this:

FROM golang

ENV GOPATH /home/marc/go_projects
ENV PATH ${GOPATH}/bin:/usr/local/go/bin:$PATH

RUN go install github.com/go-delve/delve/cmd/dlv@latest

FROM heroiclabs/nakama-pluginbuilder:3.3.0 AS go-builder

ENV GO111MODULE on
ENV CGO_ENABLED 1

WORKDIR $GOPATH/gamedev

COPY go.mod .
COPY main.go .
COPY vendor/ vendor/

RUN go build --trimpath --mod=vendor --buildmode=plugin -o ./backend.so

FROM heroiclabs/nakama:3.3.0

COPY --from=go-builder /backend/backend.so /nakama/data/modules/
COPY local.yml /nakama/data/

An my docker-compose.yml

version: '3'
services:
  postgres:
    container_name: postgres
    image: postgres:9.6-alpine
    environment:
      - POSTGRES_DB=nakama
      - POSTGRES_PASSWORD=localdb
    volumes:
      - data:/var/lib/postgresql/data
    expose:
      - "8080"
      - "5432"
    ports:
      - "5432:5432"
      - "8080:8080"
  nakama:
    container_name: nakama
    image: heroiclabs/nakama:3.12.0
    entrypoint:
      - "/bin/sh"
      - "-ecx"
      - >
        /nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama &&
        exec /nakama/nakama --name nakama1 --database.address postgres:localdb@postgres:5432/nakama --logger.level DEBUG --session.token_expiry_sec 7200        
    restart: always
    links:
      - "postgres:db"
    depends_on:
      - postgres
    volumes:
      - ./:/nakama/data
    expose:
      - "7349"
      - "7350"
      - "7351"
      - "2345"
    ports:
      - "2345:2345"
      - "7349:7349"
      - "7350:7350"
      - "7351:7351"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7350/"]
      interval: 10s
      timeout: 5s
      retries: 5
volumes:
  data:

When i build and run the docker image, it runs with no complains. I can open the webinterface of nakama, so this is running fine.
But when i try to connect the debugger, it looks like he create a succesfull connection but closes it right away.
SO my launch.json config is the following:

 "name": "Connect to server",
 "type": "go",
 "request": "attach",
 "mode": "remote",
 "remotePath": "/home/marc/go_projects/bin/dlv",
 "port": 2345,
 "host": "127.0.0.1",
 "trace": "verbose"

This is what i get in /tmp/vs-code-debug.txt:

[07:24:05.882 UTC] From client: initialize({"clientID":"vscode","clientName":"Visual Studio Code","adapterID":"go","pathFormat":"path","linesStartAt1":true,"columnsStartAt1":true,"supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"locale":"de","supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryReferences":true})
[07:24:05.882 UTC] InitializeRequest
[07:24:05.882 UTC] To client: {"seq":0,"type":"response","request_seq":1,"command":"initialize","success":true,"body":{"supportsConditionalBreakpoints":true,"supportsConfigurationDoneRequest":true,"supportsSetVariable":true}}
[07:24:05.883 UTC] InitializeResponse
[07:24:05.883 UTC] From client: attach({"name":"Connect to server","type":"go","request":"attach","mode":"remote","remotePath":"/home/marc/go_projects/bin/dlv","port":2345,"host":"127.0.0.1","trace":"verbose","__configurationTarget":5,"packagePathToGoModPathMap":{"/home/marc/go_projects/gamedev":"/home/marc/go_projects/gamedev"},"debugAdapter":"legacy","showRegisters":false,"showGlobalVariables":false,"substitutePath":[],"showLog":false,"logOutput":"debugger","dlvFlags":[],"hideSystemGoroutines":false,"dlvLoadConfig":{"followPointers":true,"maxVariableRecurse":1,"maxStringLen":64,"maxArrayValues":64,"maxStructFields":-1},"cwd":"/home/marc/go_projects/gamedev","dlvToolPath":"/home/marc/go_projects/bin/dlv","env":{"ELECTRON_RUN_AS_NODE":"1","GJS_DEBUG_TOPICS":"JS ERROR;JS LOG","USER":"marc","SSH_AGENT_PID":"1376","XDG_SESSION_TYPE":"x11","SHLVL":"0","HOME":"/home/marc","DESKTOP_SESSION":"ubuntu","GIO_LAUNCHED_DESKTOP_FILE":"/usr/share/applications/code.desktop","GTK_MODULES":"gail:atk-bridge","GNOME_SHELL_SESSION_MODE":"ubuntu","MANAGERPID":"1053","DBUS_SESSION_BUS_ADDRESS":"unix:path=/run/user/1000/bus","GIO_LAUNCHED_DESKTOP_FILE_PID":"6112","IM_CONFIG_PHASE":"1","MANDATORY_PATH":"/usr/share/gconf/ubuntu.mandatory.path","LOGNAME":"marc","_":"/usr/share/code/code","JOURNAL_STREAM":"8:44286","DEFAULTS_PATH":"/usr/share/gconf/ubuntu.default.path","XDG_SESSION_CLASS":"user","USERNAME":"marc","GNOME_DESKTOP_SESSION_ID":"this-is-deprecated","WINDOWPATH":"2","PATH":"/home/marc/.nvm/versions/node/v17.8.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin:/usr/local/go/bin:/usr/local/go/bin","SESSION_MANAGER":"local/mobile:@/tmp/.ICE-unix/1458,unix/mobile:/tmp/.ICE-unix/1458","INVOCATION_ID":"fe605ca56aa646859602b81e264bf01b","XDG_RUNTIME_DIR":"/run/user/1000","XDG_MENU_PREFIX":"gnome-","DISPLAY":":0","LANG":"de_DE.UTF-8","XDG_CURRENT_DESKTOP":"Unity","XAUTHORITY":"/run/user/1000/gdm/Xauthority","XDG_SESSION_DESKTOP":"ubuntu","XMODIFIERS":"@im=ibus","SSH_AUTH_SOCK":"/run/user/1000/keyring/ssh","SHELL":"/bin/bash","QT_ACCESSIBILITY":"1","GDMSESSION":"ubuntu","GPG_AGENT_INFO":"/run/user/1000/gnupg/S.gpg-agent:0:1","GJS_DEBUG_OUTPUT":"stderr","QT_IM_MODULE":"ibus","PWD":"/home/marc","XDG_DATA_DIRS":"/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop","XDG_CONFIG_DIRS":"/etc/xdg/xdg-ubuntu:/etc/xdg","_WSREP_START_POSITION":"","CHROME_DESKTOP":"code-url-handler.desktop","ORIGINAL_XDG_CURRENT_DESKTOP":"ubuntu:GNOME","VSCODE_CWD":"/home/marc","GDK_BACKEND":"x11","VSCODE_NLS_CONFIG":"{\"locale\":\"de\",\"availableLanguages\":{\"*\":\"de\"},\"_languagePackId\":\"b61d3f473b0358bc955527db7340fd23.de\",\"_translationsConfigFile\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/tcf.json\",\"_cacheRoot\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de\",\"_resolvedLanguagePackCoreLocation\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/30d9c6cd9483b2cc586687151bcbcd635f373630\",\"_corruptedFile\":\"/home/marc/.config/Code/clp/b61d3f473b0358bc955527db7340fd23.de/corrupted.info\",\"_languagePackSupport\":true}","VSCODE_CODE_CACHE_PATH":"/home/marc/.config/Code/CachedData/30d9c6cd9483b2cc586687151bcbcd635f373630","VSCODE_IPC_HOOK":"/run/user/1000/vscode-432c1660-1.68.1-main.sock","VSCODE_PID":"6112","NVM_INC":"/home/marc/.nvm/versions/node/v17.8.0/include/node","LS_COLORS":"","NVM_DIR":"/home/marc/.nvm","LESSCLOSE":"/usr/bin/lesspipe %s %s","LESSOPEN":"| /usr/bin/lesspipe %s","NVM_CD_FLAGS":"","NVM_BIN":"/home/marc/.nvm/versions/node/v17.8.0/bin","GOPATH":"/home/marc/go_projects","VSCODE_AMD_ENTRYPOINT":"vs/workbench/api/node/extensionHostProcess","VSCODE_PIPE_LOGGING":"true","VSCODE_VERBOSE_LOGGING":"true","VSCODE_LOG_NATIVE":"false","VSCODE_HANDLES_UNCAUGHT_ERRORS":"true","VSCODE_LOG_STACK":"false","VSCODE_IPC_HOOK_EXTHOST":"/run/user/1000/vscode-ipc-8cf508cc-d427-4616-b6b5-61d3c3e5d99f.sock","APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL":"1","GOMODCACHE":"/home/marc/go_projects/pkg/mod","GOPROXY":"https://proxy.golang.org,direct"},"__sessionId":"1893ab9e-5a19-45e7-8b39-46db079cdbe3"})
[07:24:05.883 UTC] AttachRequest
[07:24:05.884 UTC] Start remote debugging: connecting 127.0.0.1:2345
[07:24:06.191 UTC] To client: {"seq":0,"type":"event","event":"initialized"}
[07:24:06.192 UTC] InitializeEvent
[07:24:06.192 UTC] To client: {"seq":0,"type":"response","request_seq":2,"command":"attach","success":true}
[07:24:06.194 UTC] [Error] Socket connection to remote was closed
[07:24:06.194 UTC] Sending TerminatedEvent as delve is closed
[07:24:06.194 UTC] To client: {"seq":0,"type":"event","event":"terminated"}
[07:24:06.201 UTC] From client: configurationDone(undefined)
[07:24:06.201 UTC] ConfigurationDoneRequest
[07:24:06.225 UTC] From client: disconnect({"restart":false})
[07:24:06.225 UTC] DisconnectRequest

I tried to change the remote path i the launch.json multiple times, try to math the paths in the docker files.
Maybe i have to change the implementation of delve in docker, but tbh i dont really know how. I dont really find a good documentation on how to do this.

答案1

得分: 1

我遇到了同样的问题。
我是这样解决的:
在我的launch.json文件中添加了一行:"debugAdapter": "dlv-dap"

{
"version": "0.2.0",
"configurations": [
{
"name": "Delve into Docker",
"type": "go",
"request": "attach",
"debugAdapter": "dlv-dap",
"mode": "remote",
"substitutePath": [
{
"from": "${workspaceFolder}/",
"to": "/app",
},
],
"port": 2345,
"host": "127.0.0.1",
"showLog": false,
"apiVersion": 2,
"trace": "verbose"
}
]
}
英文:

I was having the same problem.
I solved it like this:
adding line: "debugAdapter": "dlv-dap" on my launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Delve into Docker",
"type": "go",
"request": "attach",
"debugAdapter": "dlv-dap", 
"mode": "remote",
"substitutePath": [
{
"from": "${workspaceFolder}/",
"to": "/app",
},
],
"port": 2345,
"host": "127.0.0.1",
"showLog": false,
"apiVersion": 2,
"trace": "verbose"
}
]
}

huangapple
  • 本文由 发表于 2022年6月23日 16:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/72726620.html
匿名

发表评论

匿名网友

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

确定