Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active September 13, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/b7e8b3529871b18c1adb69ae40ccb118 to your computer and use it in GitHub Desktop.
Save Integralist/b7e8b3529871b18c1adb69ae40ccb118 to your computer and use it in GitHub Desktop.
[Golang Debugging with Delve] #go #golang #delve #gui #debug #dlv #gdlv
  • git clone git@github.com:fastly/cli.git
  • cd cli
  • go install github.com/go-delve/delve/cmd/dlv@latest (install the debugger)
  • dlv debug ./cmd/fastly/main.go -- compute deploy (start up the debugger)
  • break ./pkg/commands/compute/deploy.go:89 (add break point within the 'deploy' code file)
  • cond <breakpoint_name_or_id> <boolean expression> (e.g. cond 1 commandName == "sso")
  • continue (this would be typed into the debugger prompt and would cause the code to run until the breakpoint)

From there you can use n (next) to go from line-to-line or s (step-into) to jump into any function calls, along with print to see what the code is doing.

To exercise the test code:

  • dlv test ./pkg/example/... -- -test.v -test.run TestExample/specific_test_case
  • break ./pkg/example/example_test.go:123

NOTE: If you see the error "cannot use -c flag with multiple packages" then cd into the package directory (e.g. cd ./pkg/example && dlv test).

Examples

Fastly Terraform Tests:

cd ./fastly
TF_ACC=true dlv test -- -test.v -test.run TestAccFastlyServiceVCL_syslog_useTLS
break block_fastly_service_logging_syslog_test.go:253 // break inside the test code
break block_fastly_service_logging_syslog.go:342      // break inside the execute terraform code (trigged by the test)

Fastly CLI Tests:

cd ./pkg/commands/compute
TEST_COMPUTE_BUILD=1 dlv test -- -test.v -test.run TestBuildAssemblyScript/successful_build
break build_test.go:328
# https://blog.gopheracademy.com/advent-2015/debugging-with-delve/
#
# connect - connect to headless debug server.
# debug - run in same directory as `go build`, builds binary with extra debug info (e.g. dlv debug .)
# run - similar to debug but no build step.
# exec - run and attach to an existing binary.
# test - useful for debugging a test suite (or you have no main function, e.g. it's a library package).
# attach - attach to a running process.
# trace - prints information whenever a tracepoint is hit, but not full debug session (only on tui version, not gui `dlv trace [regexp]`).
# note: the install steps presume you're using go 1.16 or newer.
go install github.com/go-delve/delve/cmd/dlv@latest
# dependencies required:
#
# xcode-select --install
# sudo /usr/sbin/DevToolsSecurity -enable
#
# see https://github.com/go-delve/delve/tree/master/Documentation/installation for details
go install github.com/aarzilli/gdlv@latest
# now replace:
# go run <program>
#
# with:
# gdlv run <program>
#
# Press "Ctrl+" to increase font-size.
# Type "help" to see all available commands.
# in order to use `dlv exec` you need to compile the binary like so:
go build -gcflags="all=-N -l" -o "debug" ./cmd/yourapp
# to set breakpoints:
# https://github.com/go-delve/delve/tree/master/Documentation/cli#break
#
# <filename>:<line> Specifies the line in filename. filename can be the partial path to a file or even just the base name as long as the expression remains unambiguous.
#
# Example (break on line 22 of the file):
break main.go:22
# <function>[:<line>] Specifies the line inside function. The full syntax for function is <package>.(*<receiver type>).<function name> however the only required element is the function name, everything else can be omitted as long as the expression remains unambiguous. For setting a breakpoint on an init function (ex: main.init), the <filename>:<line> syntax should be used to break in the correct init function at the correct location.
#
# Example (break on the first line within the function `main`):
break main.main:1
break compute.(*ServeCommand).Exec:20 # NOTE: The line number is relative to the function!
# debugging tests is a lot more confusing...
# https://stackoverflow.com/questions/43380530/debugging-tests-with-delve
dlv test ./pkg/example/...
dlv test ./pkg/example/... -- -test.v -test.run TestExample/specific_test_case
break someRandomNameToDescribeMyBreakpoint pkg/example/example_test.go:123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment