Skip to content

Latest commit

 

History

History
97 lines (79 loc) · 2.95 KB

03 - debug.md

File metadata and controls

97 lines (79 loc) · 2.95 KB

Debug

We can use Delve for Go debugging.

Install Delve

  • Follow the General install instructions.

    Make sure PATH containers $GOPATH/bin which will allow you to run Go binary executables without specifying the absolute path.

  • If you are using macOS, you can install via HomeBrew with the following command :
    brew install delve
  • Check your installation
    dlv version

Start a delve debug server

You can start a delve debug server for running and debugging specific package or envtest.

Debug specific package

make run-delve GO_PACKAGE=./cmd/manager/main.go

Pass arguments

 make run-delve GO_PACKAGE=./cmd/cli/main.go ARGUMENTS='cluster create test-cluster --termination-policy=Halt'

Debug envtest

make test-delve TEST_PACKAGES=./controllers/apps/...

Unlike go test supports multiple packages, Delve needs a single executable to work, it only supports single package.

Change debug server port

You can change debug server port for make run-delve and make test-delve.

make run-delve DEBUG_PORT=2347

After bug server started, it will show the listen address:

API server listening at: [::]:2347

Connect the debug server with a frontend client

Delve support lots of code editors (editor plugins for Delve), you can choose your favorite editor to connect with the listen address for debugging.

This section introduces how to start debugging with the Delve CLI, GoLand and VSCode. Please refer to the Delve or editors documentation for more details.

Delve CLI

$ dlv connect 127.0.0.1:2345
Type 'help' for list of commands.
(dlv) break cluster_controller.go:303
(dlv) continue
(dlv) next
(dlv) ......

JetBrains GoLand / IntelliJ IDEA with go plugin

Click Run — Edit configurations, add new debug configuration, select "Go Remote": goland add go remote

Add debug server listen address to Host and Port goland configure go remote

And then set breakpoints, and run debug. goland debug result

VSCode

Create/modify .vscode/launch.json file for connecting debug server:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug envtest",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "",
            "port": 2346,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "log",
            "logOutput": "rpc"
        }
    ]
}

Set breakpoints and run Start Debugging. vscode debug result