According to the official documentation, I need to specify processId, when I want to attach to a remote running process.
When using attach mode outside of local host, you need to specify the processId in the config since the processId resolution feature cannot gather information about processes running remotely.
First, I checked the PID of the process (api-svr) that I need to debug on the remote server:
<code> PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 1237620 6116 3024 S 0.0 0.0 3:24.63 supervisord
14 root 20 0 3110424 609456 21152 S 0.0 0.9 45:21.22 api-svr
<code> PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 1237620 6116 3024 S 0.0 0.0 3:24.63 supervisord
14 root 20 0 3110424 609456 21152 S 0.0 0.9 45:21.22 api-svr
</code>
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 1237620 6116 3024 S 0.0 0.0 3:24.63 supervisord
14 root 20 0 3110424 609456 21152 S 0.0 0.9 45:21.22 api-svr
Then, I forwarded a port locally using the command kubectl port-forward deployments/*** 11458:11458 -n *****
, and entered dlv dap –listen=:11458 –log –log-output=dap in the remote pod. After starting the debug session locally, the following error occurred in the pod:
<code>root:bin$ dlv dap --listen=:11458 --log --log-output=dap
DAP server listening at: [::]:11458
2024-05-29T18:26:56+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2024-05-29T18:26:56+08:00 debug layer=dap DAP server pid = 17968
2024-05-29T18:27:17+08:00 debug layer=dap DAP connection 1 started
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":1,"type":"request","command":"initialize","arguments":{"clientID":"vscode","clientName":"Visual Studio Code","adapterID":"go","locale":"en","linesStartAt1":true,"columnsStartAt1":true,"pathFormat":"path","supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"supportsMemoryReferences":true,"supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryEvent":true,"supportsArgsCanBeInterpretedByShell":true,"supportsStartDebuggingRequest":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"supportsConfigurationDoneRequest":true,"supportsFunctionBreakpoints":true,"supportsConditionalBreakpoints":true,"supportsEvaluateForHovers":true,"supportsSetVariable":true,"supportsExceptionInfoRequest":true,"supportsDelayedStackTraceLoading":true,"supportsLogPoints":true,"supportsDisassembleRequest":true,"supportsClipboardContext":true,"supportsSteppingGranularity":true,"supportsInstructionBreakpoints":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":2,"type":"request","command":"attach","arguments":{"name":"Attach to remote process","type":"go","debugAdapter":"dlv-dap","request":"attach","mode":"remote","program":"/usr/local/trpc/bin/api-svr","processId":14,"host":"127.0.0.1","port":11458,"__configurationTarget":6,"packagePathToGoModPathMap":{"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446":"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446","/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930":"/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930","/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740":"/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740"},"showRegisters":false,"showGlobalVariables":false,"substitutePath":[],"showLog":false,"logOutput":"debugger","dlvFlags":[],"hideSystemGoroutines":false,"dlvToolPath":"/data/workspace/go-env/bin/dlv","env":{"GOPATH":"/data/workspace/go-env/"},"__sessionId":"f2b8819c-65f7-4128-a239-ee57c9b57e9a"}}
2024-05-29T18:27:17+08:00 debug layer=dap parsed launch config: {
2024-05-29T18:27:17+08:00 debug layer=dap Failed to attach: no debugger found
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":2,"success":false,"command":"attach","message":"Failed to attach","body":{"error":{"id":3001,"format":"Failed to attach: no debugger found","showUser":true}}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":3,"type":"request","command":"disconnect","arguments":{}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":3,"success":true,"command":"disconnect"}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"event","event":"terminated","body":{}}
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopping...
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopped
<code>root:bin$ dlv dap --listen=:11458 --log --log-output=dap
DAP server listening at: [::]:11458
2024-05-29T18:26:56+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2024-05-29T18:26:56+08:00 debug layer=dap DAP server pid = 17968
2024-05-29T18:27:17+08:00 debug layer=dap DAP connection 1 started
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":1,"type":"request","command":"initialize","arguments":{"clientID":"vscode","clientName":"Visual Studio Code","adapterID":"go","locale":"en","linesStartAt1":true,"columnsStartAt1":true,"pathFormat":"path","supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"supportsMemoryReferences":true,"supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryEvent":true,"supportsArgsCanBeInterpretedByShell":true,"supportsStartDebuggingRequest":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"supportsConfigurationDoneRequest":true,"supportsFunctionBreakpoints":true,"supportsConditionalBreakpoints":true,"supportsEvaluateForHovers":true,"supportsSetVariable":true,"supportsExceptionInfoRequest":true,"supportsDelayedStackTraceLoading":true,"supportsLogPoints":true,"supportsDisassembleRequest":true,"supportsClipboardContext":true,"supportsSteppingGranularity":true,"supportsInstructionBreakpoints":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":2,"type":"request","command":"attach","arguments":{"name":"Attach to remote process","type":"go","debugAdapter":"dlv-dap","request":"attach","mode":"remote","program":"/usr/local/trpc/bin/api-svr","processId":14,"host":"127.0.0.1","port":11458,"__configurationTarget":6,"packagePathToGoModPathMap":{"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446":"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446","/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930":"/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930","/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740":"/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740"},"showRegisters":false,"showGlobalVariables":false,"substitutePath":[],"showLog":false,"logOutput":"debugger","dlvFlags":[],"hideSystemGoroutines":false,"dlvToolPath":"/data/workspace/go-env/bin/dlv","env":{"GOPATH":"/data/workspace/go-env/"},"__sessionId":"f2b8819c-65f7-4128-a239-ee57c9b57e9a"}}
2024-05-29T18:27:17+08:00 debug layer=dap parsed launch config: {
"mode": "remote",
"processId": 14,
"backend": "default",
"stackTraceDepth": 50
}
2024-05-29T18:27:17+08:00 debug layer=dap Failed to attach: no debugger found
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":2,"success":false,"command":"attach","message":"Failed to attach","body":{"error":{"id":3001,"format":"Failed to attach: no debugger found","showUser":true}}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":3,"type":"request","command":"disconnect","arguments":{}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":3,"success":true,"command":"disconnect"}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"event","event":"terminated","body":{}}
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopping...
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopped
</code>
root:bin$ dlv dap --listen=:11458 --log --log-output=dap
DAP server listening at: [::]:11458
2024-05-29T18:26:56+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2024-05-29T18:26:56+08:00 debug layer=dap DAP server pid = 17968
2024-05-29T18:27:17+08:00 debug layer=dap DAP connection 1 started
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":1,"type":"request","command":"initialize","arguments":{"clientID":"vscode","clientName":"Visual Studio Code","adapterID":"go","locale":"en","linesStartAt1":true,"columnsStartAt1":true,"pathFormat":"path","supportsVariableType":true,"supportsVariablePaging":true,"supportsRunInTerminalRequest":true,"supportsMemoryReferences":true,"supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryEvent":true,"supportsArgsCanBeInterpretedByShell":true,"supportsStartDebuggingRequest":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":1,"success":true,"command":"initialize","body":{"supportsConfigurationDoneRequest":true,"supportsFunctionBreakpoints":true,"supportsConditionalBreakpoints":true,"supportsEvaluateForHovers":true,"supportsSetVariable":true,"supportsExceptionInfoRequest":true,"supportsDelayedStackTraceLoading":true,"supportsLogPoints":true,"supportsDisassembleRequest":true,"supportsClipboardContext":true,"supportsSteppingGranularity":true,"supportsInstructionBreakpoints":true}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":2,"type":"request","command":"attach","arguments":{"name":"Attach to remote process","type":"go","debugAdapter":"dlv-dap","request":"attach","mode":"remote","program":"/usr/local/trpc/bin/api-svr","processId":14,"host":"127.0.0.1","port":11458,"__configurationTarget":6,"packagePathToGoModPathMap":{"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446":"/data/workspace/repos/protocol/csproto/api/tmp-1716954334266622446","/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930":"/data/workspace/repos/protocol/service/game-service/tmp-1716954459120449930","/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740":"/data/workspace/repos/protocol/service/game-service/tmp-1716954556533632740"},"showRegisters":false,"showGlobalVariables":false,"substitutePath":[],"showLog":false,"logOutput":"debugger","dlvFlags":[],"hideSystemGoroutines":false,"dlvToolPath":"/data/workspace/go-env/bin/dlv","env":{"GOPATH":"/data/workspace/go-env/"},"__sessionId":"f2b8819c-65f7-4128-a239-ee57c9b57e9a"}}
2024-05-29T18:27:17+08:00 debug layer=dap parsed launch config: {
"mode": "remote",
"processId": 14,
"backend": "default",
"stackTraceDepth": 50
}
2024-05-29T18:27:17+08:00 debug layer=dap Failed to attach: no debugger found
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":2,"success":false,"command":"attach","message":"Failed to attach","body":{"error":{"id":3001,"format":"Failed to attach: no debugger found","showUser":true}}}
2024-05-29T18:27:17+08:00 debug layer=dap [<- from client]{"seq":3,"type":"request","command":"disconnect","arguments":{}}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"response","request_seq":3,"success":true,"command":"disconnect"}
2024-05-29T18:27:17+08:00 debug layer=dap [-> to client]{"seq":0,"type":"event","event":"terminated","body":{}}
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopping...
2024-05-29T18:27:17+08:00 debug layer=dap DAP server stopped
My VSCode launch.json configuration is as follows:
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “Attach to remote process”,
“type”: “go”,
“debugAdapter”: “dlv-dap”,
“request”: “attach”,
“mode”: “remote”,
“program”: “/usr/local/trpc/bin/api-svr”,
“processId”: 14,
“host”: “127.0.0.1”,
“port”: 11458,
}
]
}
vscode error