remote debug golang dlv wait for a client to connect before continuing the code (visual studio code client)

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

remote debug golang dlv wait for a client to connect before continuing the code (visual studio code client)

问题

我喜欢能够在等待客户端连接到dlv调试器之前执行简单的操作,但没有成功。我有一个简单的Go服务器:

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(w, "hello\n")
}

func headers(w http.ResponseWriter, req *http.Request) {
    for name, headers := range req.Header {
        for _, h := range headers {
            fmt.Fprintf(w, "%v: %v\n", name, h)
        }
    }
}

func main() {
    fmt.Println("server started now!")
    http.HandleFunc("/hello", hello)
    http.HandleFunc("/headers", headers)

    http.ListenAndServe(":8090", nil)
}

我在Linux机器上这样启动服务器:

vagrant@vagrant:~/go_dev/very_simple_server_dir$ dlv debug /home/vagrant/go_dev/very_simple_server_dir/very_simple_server.go --headless --listen=:3000  --log
API server listening at: [::]:3000
2022-10-31T06:18:47Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2022-10-31T06:18:47Z info layer=debugger launching process with args: [/home/vagrant/go_dev/very_simple_server_dir/__debug_bin]
2022-10-31T06:18:47Z warning layer=debugger can't find build-id note on binary

在Visual Studio Code中,launch.json文件如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/home/vagrant/go_dev/very_simple_server_dir/",
            "cwd": "${workspaceFolder}",
            "port": 3000,
            "host": "127.0.0.1",
            "trace": "verbose",
            "asRoot": true
        }
    ]
}

它可以成功连接到远程服务器,问题是我希望dlv调试服务器在执行Go代码之前等待客户端连接。我想要能够在func main()的第二行设置断点。请问我该如何做到这一点?

英文:

I like to be able do very simple thing wait for the client to connect to dlv debugger before continue the code without success .
i have simple go server :

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {

    fmt.Fprintf(w, "hello\n")
}

func headers(w http.ResponseWriter, req *http.Request) {

    for name, headers := range req.Header {
        for _, h := range headers {
            fmt.Fprintf(w, "%v: %v\n", name, h)
        }
    }
}

func main() {
    fmt.Println("server started now!")
    http.HandleFunc("/hello", hello)
    http.HandleFunc("/headers", headers)

    http.ListenAndServe(":8090", nil)
}

i start the server on the linux mechine like this:

vagrant@vagrant:~/go_dev/very_simple_server_dir$ dlv debug /home/vagrant/go_dev/very_simple_server_dir/very_simple_server.go --headless --listen=:3000  --log
API server listening at: [::]:3000
2022-10-31T06:18:47Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
2022-10-31T06:18:47Z info layer=debugger launching process with args: [/home/vagrant/go_dev/very_simple_server_dir/__debug_bin]
2022-10-31T06:18:47Z warning layer=debugger can't find build-id note on binary

in visual studio code the launch.json looks like this :

"version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to server",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "/home/vagrant/go_dev/very_simple_server_dir/",
            "cwd" : "${workspaceFolder}",
            "port": 3000,
            "host": "127.0.0.1",
            "trace": "verbose",
            "asRoot": true,
        }
    ]

it connectes just fine to the remote server the problem is that i like the dlv debug server wait to the client to connect before it execute the go code .
i want to be able to set break point in func main() second line

how can i do that ?

答案1

得分: 1

你的main.go中的第二行代码只是注册了你的处理函数(在这个例子中是hello函数)。这发生在你使用ListenAndServe()启动应用程序之前。

我猜你想在应用程序启动后进行调试。你可能想在hello函数的第一行(也是唯一一行)设置一个断点。

英文:

The second line in your main.go does no more than register your handler function (hello in this case). This happens before your app is started with ListenAndServe().

I assume you want to debug your app after it is started. You may want to set a breakpoint at the first (and the only) line of your hello function.

huangapple
  • 本文由 发表于 2022年10月31日 14:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/74259427.html
匿名

发表评论

匿名网友

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

确定