使用 VSCode 对 Linux 虚拟机进行远程调试给出:远程调试:关闭 dlv 连接

问题描述 投票:0回答:1

我尝试从 Windows vscode 到 virtualbox simple go 应用程序进行简单的远程调试。 所有端口均在 nat 上的端口转发 vbox 配置中打开。

从 vccode 来看,它的配置如下:

 {
    // 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
        }
    ]
}

在Linux服务器中是这样的:

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"}

为什么无法连接? 更新 当使用 dlv cli 连接时,我可以成功连接到服务器并设置断点等

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)

这是在远程服务器上:

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)}

所以我猜它在 VSCODE 配置中的某个地方..但是在哪里?

go visual-studio-code virtualbox remote-debugging dlv
1个回答
0
投票

这正在按预期工作。

你没有在vs-code中设置断点,所以被调试者运行到最后退出,然后dlv退出并关闭连接。

您需要做的是在开始调试之前在 vs-code 中设置断点。

其他注意事项:

  1. 根据 vscode-go 调试
    remotePath
    已弃用。请使用
    substitutePath
    来代替。
  2. 建议使用 APIv2,因为新功能只能通过版本 2 提供。要使用 APIv2,请使用
    --api-version=2
    标志启动 dlv,并从
    "apiVersion":1
    中删除
    launch.json

参考资料:

  1. JSON-RPC 接口
  2. https://stackoverflow.com/a/76469636/1369400
© www.soinside.com 2019 - 2024. All rights reserved.