Delve (dlv) is the canonical Go debugger — it understands Go’s calling convention, knows about goroutines, and can attach to a running process or a core dump. This article shows how Stack Harbor uses Delve in local development, in a container, and remotely against staging — and which switches you must NOT leave on in production.
How to verify
Install Delve and confirm it can run a binary:
go install github.com/go-delve/delve/cmd/dlv@latest
dlv version
dlv debug ./cmd/api -- --config=local.yaml
dlv exec ./bin/api -- --config=local.yaml
dlv attach $(pgrep -f bin/api)
dlv debug builds and runs from source. dlv exec runs an existing binary (it must be built with debug info — no -ldflags="-s -w"). dlv attach attaches to a running PID via ptrace.
What’s happening
Delve uses ptrace (Linux) or the equivalent OS facility (mach on macOS, debug API on Windows) to stop and inspect a target process. It maps the process memory back to Go source using DWARF debug info embedded in the binary. If you stripped DWARF with -ldflags="-s -w", Delve can attach but cannot show source lines or local variables.
Delve speaks several protocols. The native dlv CLI runs over its own RPC. The --headless mode exposes a TCP server speaking either the dlv RPC or DAP (Debug Adapter Protocol — the standard editors use). Most editor integrations launch dlv dap and connect to it. For remote attaches to staging, you run dlv --headless --listen=:40000 attach <PID> on the remote host and tunnel the port to your laptop.
A debug session is heavyweight. Delve stops the target process while you step. Latency-sensitive services should not run under Delve in production — use logs, traces, and pprof instead. Delve is for staging, local repro, and core dumps.
The procedure
-
Build a binary that Delve can read. The default
go buildkeeps DWARF. Do NOT strip it for the debug binary:go build -gcflags=all="-N -l" -o bin/api ./cmd/api-Ndisables optimizations and-ldisables inlining so the source-line mapping stays accurate. Without these flags, single-stepping skips lines because inlined code has been hoisted. -
Local single-binary debug session.
dlv exec ./bin/api -- --config=local.yaml (dlv) break main.main (dlv) continue (dlv) goroutines -t (dlv) goroutine 17 (dlv) bt (dlv) print req.URL.Path (dlv) stepgoroutines -tlists every goroutine with its current call site.goroutine Nswitches the focus.btis the backtrace.printevaluates a Go expression against the current frame. -
Headless mode for an editor. Launch Delve as a server:
dlv dap --listen=127.0.0.1:40000 --logThen in VSCode,
launch.json:{ "name": "Attach to API", "type": "go", "request": "attach", "mode": "remote", "host": "127.0.0.1", "port": 40000 }Goland exposes the same via
Run -> Edit Configurations -> Go Remote. -
Remote debug a staging container. SSH-tunnel the port and attach:
kubectl port-forward pod/api-7f8c-9q4r 40000:40000 # In another shell on the pod (via kubectl exec): dlv --headless --listen=:40000 --api-version=2 --accept-multiclient attach 1The container image must include
dlv(multi-stage build with a debug variant), andSYS_PTRACEcapability must be granted to the pod’s security context. In Kubernetes:securityContext: capabilities: add: ["SYS_PTRACE"] -
Debug a core dump. When a service crashes with a panic and you have a core file:
GOTRACEBACK=crash ./bin/api # generates core on panic dlv core ./bin/api ./core (dlv) goroutines -t (dlv) btcoremode is read-only — you cannot resume or step, only inspect. -
Conditional breakpoints and hitcounts. Pause on a specific condition:
(dlv) break handler.go:42 (dlv) condition 1 req.Method == "POST" (dlv) cond -hitcount 1 > 100The first form fires only on
POSTrequests; the second only after 100 hits. -
dlv tracefor non-stop function tracing. Sometimes you want to log every entry into a function without halting:dlv trace ./bin/api 'main.handleCharge'Prints each invocation with arguments. Lower overhead than stepping but still 5-10x slower than running raw.
Common pitfalls
- Production binaries stripped of DWARF cannot be debugged. Ship a separate debug-symbols build to staging if you need to attach in non-prod environments.
SYS_PTRACEcapability is required to attach in containers. Without it,dlv attachfails withoperation not permitted. Default Kubernetes pods do not have it.--accept-multiclientkeeps Delve running after the client disconnects. Otherwise the headless server exits and you have to restart it for each session.- Optimizations defeat the line debugger.
-gcflags=all="-N -l"is mandatory for accurate single-stepping. Production binaries are optimized and lines will jump. - Delve over the internet is a backdoor. The Delve protocol has no authentication. Tunnel it through SSH or kubectl port-forward — never bind it to a public interface.
Stack Harbor keeps a debug-image variant for every Go service we operate so remote attaches to staging are a kubectl-exec away when a behavior cannot be reproduced locally. See how that fits into our managed operations practice when a production reproduction is needed safely.