Remote debug with VSCode to VirtualBox Linux gives: Remote Debugging: close dlv connection

174 views Asked by At

I am trying to do simple remote debug from Windows VSCode to VirtualBox with a simple Go app. All ports are open in the port forward VBox config on nat.

enter image description here

From the VCCode it is configured like this:

 {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/home/xxx/go_test/",
            "cwd" : "${workspaceFolder}",
            "port": 3000,
            "host": "127.0.0.1",
            "trace": "verbose",
            "asRoot": true,
            "apiVersion":1
        }
    ]
}

And in the Linux server like this:

xxx@xxx:~/go_test$ dlv debug /home/xxx/go_test/main.go --headless --listen=:3000  --log
API server listening at: [::]:3000
2023-11-14T16:09:40Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2023-11-14T16:09:40Z info layer=debugger launching process with args: [./__debug_bin3166801926]
2023-11-14T16:09:40Z debug layer=debugger Adding target 1336 "/home/xxx/go_test/__debug_bin3166801926"
2023-11-14T16:10:55Z debug layer=debugger continuing
2023-11-14T16:10:55Z debug layer=debugger ContinueOnce
2023-11-14T16:10:55Z debug layer=debugger continuing
2023-11-14T16:10:55Z debug layer=debugger detaching

vscode-go-debug.txt :

2023-11-14, 16:08:35.879 UTC
[16:08:35.879 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":"en","supportsProgressReporting":true,"supportsInvalidatedEvent":true,"supportsMemoryReferences":true,"supportsArgsCanBeInterpretedByShell":true,"supportsMemoryEvent":true,"supportsStartDebuggingRequest":true})
[16:08:35.879 UTC] InitializeRequest
[16:08:35.879 UTC] To client: {"seq":0,"type":"response","request_seq":1,"command":"initialize","success":true,"body":{"supportsConditionalBreakpoints":true,"supportsConfigurationDoneRequest":true,"supportsSetVariable":true}}
[16:08:35.880 UTC] InitializeResponse
[16:08:35.880 UTC] From client: attach({"name":"Connect to server","type":"go","request":"attach","mode":"remote","remotePath":"/home/xxx/go_test/","cwd":"C:\\dev\\my\\go_apps\\go_remote_debug_test","port":3000,"host":"127.0.0.1","trace":"verbose","asRoot":true,"apiVersion":1,"__configurationTarget":6,"packagePathToGoModPathMap":{"c:\\dev\\my\\go_apps\\go_remote_debug_test":"c:\\dev\\my\\go_apps\\go_remote_debug_test"},"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},"dlvToolPath":"})
[16:08:35.880 UTC] AttachRequest
[16:08:35.880 UTC] Start remote debugging: connecting 127.0.0.1:3000
[16:08:36.213 UTC] To client: {"seq":0,"type":"event","event":"initialized"}
[16:08:36.214 UTC] InitializeEvent
[16:08:36.214 UTC] To client: {"seq":0,"type":"response","request_seq":2,"command":"attach","success":true}
[16:08:36.218 UTC] From client: configurationDone(undefined)
[16:08:36.218 UTC] ConfigurationDoneRequest
[16:08:36.221 UTC] Changing DebugState from Halted to Running
[16:08:36.222 UTC] To client: {"seq":0,"type":"response","request_seq":3,"command":"configurationDone","success":true}
[16:08:36.222 UTC] ConfigurationDoneResponse {"seq":21,"type":"response","request_seq":3,"command":"configurationDone","success":true}
[16:08:36.229 UTC] continue state {"Pid":1336,"TargetCommandLine":"","Running":false,"Recording":false,"CoreDumping":false,"Threads":null,"NextInProgress":false,"WatchOutOfScope":null,"exited":true,"exitStatus":0,"When":""}
[16:08:36.230 UTC] handleReenterDebug(breakpoint).
[16:08:36.230 UTC] To client: {"seq":0,"type":"event","event":"terminated"}
[16:08:36.230 UTC] TerminatedEvent
[16:08:36.248 UTC] From client: threads(undefined)
[16:08:36.248 UTC] To client: {"seq":0,"type":"response","request_seq":4,"command":"threads","success":true,"body":{"threads":[]}}
[16:08:36.250 UTC] From client: disconnect({"restart":false})
[16:08:36.250 UTC] DisconnectRequest
[16:08:36.250 UTC] Issuing a continue command before closing Delve's connection as the debuggee is not running.
[16:08:36.250 UTC] Closing Delve.
[16:08:36.250 UTC] Remote Debugging: close dlv connection.
[16:08:36.251 UTC] DisconnectRequest to parent to shut down protocol server.
[16:08:36.251 UTC] To client: {"seq":0,"type":"response","request_seq":5,"command":"disconnect","success":true}
[16:08:36.251 UTC] DisconnectResponse
[16:08:36.252 UTC] continue state {"Pid":1336,"TargetCommandLine":"","Running":false,"Recording":false,"CoreDumping":false,"Threads":null,"NextInProgress":false,"WatchOutOfScope":null,"exited":true,"exitStatus":0,"When":""}
[16:08:36.252 UTC] handleReenterDebug(breakpoint).
[16:08:36.252 UTC] To client: {"seq":0,"type":"event","event":"terminated"}
[16:08:36.252 UTC] TerminatedEvent
[16:08:36.252 UTC] [Error] Socket connection to remote was closed
[16:08:36.253 UTC] Sending TerminatedEvent as delve is closed
[16:08:36.253 UTC] To client: {"seq":0,"type":"event","event":"terminated"}

Why can't it connect?

Update

When connecting with dlv cli I can successfully connect to the server and set breakpoint and such

dlv connect 127.0.0.1:3000 --log --log-output=debugger,rpc,dap,stack
Type 'help' for list of commands.

(dlv) break main.go:8
Breakpoint 1 set at 0x78b0aa for main.main.func1() ./go_test/main.go:8
(dlv)

And this is on the remote server:

dlv debug /home/xxx/go_test/main.go --headless --listen=:3000  --log
API server listening at: [::]:3000
2023-11-15T05:57:02Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2023-11-15T05:57:02Z info layer=debugger launching process with args: [./__debug_bin3481357464]
2023-11-15T05:57:03Z debug layer=debugger Adding target 1776 "/home/umen/go_test/__debug_bin3481357464"
2023-11-15T05:58:21Z info layer=debugger created breakpoint: &api.Breakpoint{ID:1, Name:"", Addr:0x78b0aa, Addrs:[]uint64{0x78b0aa}, AddrPid:[]int{1776}, File:"/home/umen/go_test/main.go", Line:8, FunctionName:"main.main.func1", ExprString:"", Cond:"", HitCond:"", HitCondPerG:false, Tracepoint:false, TraceReturn:false, Goroutine:false, Stacktrace:0, Variables:[]string(nil), LoadArgs:(*api.LoadConfig)(nil), LoadLocals:(*api.LoadConfig)(nil), WatchExpr:"", WatchType:0x0, VerboseDescr:[]string(nil), HitCount:map[string]uint64{}, TotalHitCount:0x0, Disabled:false, UserData:interface {}(nil)}

So I guess it is somewhere in the VSCODE config.. but where ?

1

There are 1 answers

0
Zeke Lu On

This is working as expected.

You haven't set breakpoints in vs-code, so the debugee runs to the end and exits, and then dlv exits and the connection is closed.

What you need to do is to set a breakpoint in vs-code before starting debugging.

Other things to note:

  1. According to vscode-go debugging, remotePath is deprecated. Use substitutePath instead.
  2. It's recommended to use the APIv2 as new features will only be made available through version 2. To use APIv2, start dlv with the --api-version=2 flag and remove "apiVersion":1 from launch.json.

References:

  1. JSON-RPC interface
  2. https://stackoverflow.com/a/76469636/1369400