Skip to main content

Your first debug session

In this tutorial, you attach Delve to a Go service in your cluster and stop at a breakpoint.

Before you start​

  • DebugHero is installed.
  • Your cluster and your binary meet the cluster requirements.
  • kubectl get pods -n <namespace> works in your shell and shows the target pod. DebugHero uses the same kubeconfig as kubectl: KUBECONFIG, else ~/.kube/config. See Which cluster DebugHero uses.
  • You have the source code of the service on your machine, at the same version as the image.

You need these values for the target pod:

ValueExampleNotes
Namespacepayments
Pod labelsapp=checkoutUse labels that select exactly one pod. DebugHero uses the first pod in the list.
Container nameappOptional. The default is the first container in the pod.
Process namecheckoutOptional. The default is PID 1 in the container.

To check that the labels select one pod, run:

shell
kubectl get pods -n payments -l app=checkout

1. Start debugd​

Open a terminal and start the daemon:

shell
debugd

debugd listens on port 58090 and writes its logs to ~/.debughero/logs/debugd.log. Keep it running while you debug.

To check that it runs, open another terminal and run:

shell
curl http://localhost:58090/health/check

2. Log in​

shell
debugctl login

Your browser opens the DebugHero sign-in page. After you sign in, debugctl stores your credentials in the OS keychain.

You can also sign in from the IDE: in VS Code, select Sign In in the DebugHero view; in GoLand, run the action DebugHero: Login.

3. Start a session​

Select one of the three methods.

VS Code​

  1. Open the folder of the service as the workspace. DebugHero maps the workspace root to the build directory of the binary, so the workspace root must be the root of the Go module that was built.
  2. Select the DebugHero icon in the Activity Bar. The Debug Sessions view opens.
  3. Enter the Pod labels and the Namespace. Optionally, enter the Container name and the Match process name.
  4. Select Start Debug Session.
  5. When the session is ready, select Connect. VS Code starts a debug session that is attached to Delve.

GoLand​

  1. Open the project of the service.
  2. Open the DebugHero tool window.
  3. Enter the Pod labels and the Namespace. Optionally, enter the container name and the process name.
  4. Select Start Debug Session.
  5. When the session is ready, select Connect. GoLand starts a Go Remote debug configuration that is attached to Delve.

CLI​

  1. Start the session:

    shell
    debugctl debug --namespace payments --labels app=checkout
    Output
    debug session requested — run `debugctl get sessions` to check status
  2. Wait until the status is READY:

    shell
    debugctl get sessions

    Note the values in the DEBUGGER_PORT and PATH columns.

  3. Connect your IDE to 127.0.0.1:<DEBUGGER_PORT>. For VS Code, add this configuration to .vscode/launch.json:

    .vscode/launch.json
    {
    "version": "0.2.0",
    "configurations": [
    {
    "name": "DebugHero",
    "type": "go",
    "request": "attach",
    "mode": "remote",
    "host": "127.0.0.1",
    "port": 40123,
    "substitutePath": [
    {"from": "${workspaceFolder}", "to": "/src"}
    ]
    }
    ]
    }

    Replace 40123 with the DEBUGGER_PORT value and /src with the PATH value.

4. Stop at a breakpoint​

  1. Set a breakpoint in a handler of the service.
  2. Send a request that runs this handler. If the service is not exposed, forward its port. With debugctl debug, use --port <application-port>. In the IDE form, enter the App port. DebugHero forwards it to the same port number on localhost.
  3. The IDE stops at the breakpoint. You can now examine variables, goroutines, and the call stack.
  4. Continue the process quickly. While it is stopped, it does not answer requests or health checks.

5. End the session​

There are two different actions:

  • Disconnect closes the connection of the IDE to Delve. The session continues, and Delve stays attached to the process. You can connect again.
  • Stop Session ends the session. Delve removes its breakpoints and detaches. The process continues to run. The debug container exits, and the session status changes to COMPLETED. You cannot connect to this session again.

To stop the session, do one of these steps:

  • In the IDE, select Stop Session on the session. In VS Code, the session is in the Debug Sessions view. In GoLand, it is in the DebugHero tool window.

  • In a terminal, run:

    shell
    debugctl stop <session-id>

    The output shows the result:

    output
    session 3: COMPLETED
    debugger detached: true
    container terminated: true

If you only disconnect, the session stops automatically after 1 hour.

tip

Stop the session when you are done. Then Delve does not stay attached to a production process.

If something does not work​

SymptomWhat to do
failed to start debug session - make sure debugd is runningStart debugd. If you changed its port, use the same port in the IDE settings.
The session status is FAILEDRun debugctl get sessions and read the LAST_ERRORS column.
The session is READY, but the IDE does not connectRun debugctl logs <session-id> to see the Delve log.
Version of Delve is too old for Go version in the Delve logThe binary was built with Go 1.26 or later. See Go version.
Breakpoints are not hit, or show as unverifiedThe source path mapping is wrong. Open the module root as the workspace, and check that your source has the same version as the image.
LAST_ERRORS starts with missing kubernetes permissions or blocked byRBAC or an admission policy rejected the debug container. See Cluster requirements and Admission policy exemptions.

debugd writes more details to ~/.debughero/logs/debugd.log.