Tags: #go #debugging
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
).
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