[SOLVED] How to debug golang code through vsode?

  • Clearly explain what you’re trying to do
    I want to learn an open source project[1] through debugging (basically to check some values during runtime) its code. But I encounter a problem to setup debugging env in vscode. I suppose I am not familiar with both vscode and golang, so I appreciate any suggestions.

  • State the problem and report exact error messages or program behavior
    There is no problem to git clone the project and to setup up the break points and so on. However, that project is a client/ server setting. So I am not sure if the way I setup to debug is correct or not.

    1. Click Debug and Run button which creates launch.json file with content
    {
        // 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": "Launch Package",
                "type": "go",
                "request": "launch",
                "mode": "auto",
                "program": "${fileDirname}"
            }
        ]
    }
    
    1. vscode’s DEBUG CONSOLE prints
    Starting: /home/jason/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:41767 --log-dest=3 from /home/jason/workspace/github.com/chlin501/serf/.vscode
    DAP server listening at: 127.0.0.1:41767
    
  • Problems
    I do not know how to trigger the entire flow so that I can observe the vlaues during runtime execution. Typically the procedure of this project is to launch a server process by e.g. ./bin/serf agent -node=foo -bind=127.0.0.1:5000 -rpc-addr=127.0.0.1:7373; then to issue a client command such as ./bin/serf join 127.0.0.1:5001. Now if I run this in vscode TERMINAL, nothing happens in debug env like simple tutorial where I can observe variables and call stack values at runtime. How should I configure to achieve this goal? Thanks

[1]. GitHub - hashicorp/serf: Service orchestration and management tool.

It looks like Attach to Process should be used instead.

Steps:

  1. Build binary artifact
  2. Launch server process e.g. ./bin/serf agent ...
  3. Click Run and Debug, and choose Attach to Process with following launch.json content
    {
        // 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": "Attach to Process",
                "type": "go",
                "request": "attach",
                "mode": "local",
                "processId": 47854 # this value can be found by ps -ef | grep serf
            }
        ]
    }
    
  4. Click Attach (triangle) button (or F5) in Run and Debug section at the left hand side panel. When the process is attached, variables and call stack info will be shown at the left hand side panel
  5. Execute ./bin/serf join ... command in vscode TERMINAL. Then now the runtime info will be updated at variables, call stack panel at the left hand side.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.