Golang delve: Difference between revisions

From wikinotes
(Created page with "delve is a go debugger.<br> It is more knowledgeable about the go runtime than golang gdb . = Documentation = <blockquote> {| class="wikitable" |- | github || https://github.com/go-delve/delve |- |} </blockquote><!-- Documentation --> = Install = <blockquote> <syntaxhighlight lang="bash"> go install github.com/go-delve/delve/cmd/dlv@latest </syntaxhighlight> </blockquote><!-- Install --> = Usage = <blockquote> <syntaxhighlight lang="bash"> dlv debug dlv debug exam...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
delve is a go debugger.<br>
delve is a go debugger.<br>
It is more knowledgeable about the go runtime than [[golang gdb]] .
It is more knowledgeable about the go runtime than [[golang gdb]] .<br>
There are also editor integrations.


= Documentation =
= Documentation =
Line 7: Line 8:
|-
|-
| github || https://github.com/go-delve/delve
| github || https://github.com/go-delve/delve
|-
| command docs || https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md
|-
| editor plugins || https://github.com/go-delve/delve/blob/master/Documentation/EditorIntegration.md
|-
|-
|}
|}
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->
= Locations =
<blockquote>
{| class="wikitable"
|-
| <code>~/.config/dlv/config.yml</code> || config options
|-
|}
</blockquote><!-- Locations -->


= Install =
= Install =
Line 17: Line 31:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Install -->
</blockquote><!-- Install -->
= Configuration =
<blockquote>
== Persistent ==
<blockquote>
<syntaxhighlight lang="yaml">
# ~/.config/dlv/config.yml
max-string-len: 1000
max-array-values: 1000
</syntaxhighlight>
</blockquote><!-- Persistent -->
== Dynamic ==
<blockquote>
Within a delve session, you can modify debugger config values.
<syntaxhighlight lang="bash">
config max-string-len 1000
</syntaxhighlight>
</blockquote><!-- Dynamic -->
</blockquote><!-- Configuration -->


= Usage =
= Usage =
<blockquote>
<blockquote>
Delve builds your executable automatically with flags required for it to debug your program.
== Start Delve ==
<blockquote>
<syntaxhighlight lang="bash">
# start debugger
dlv debug [example.com/x/foo/package] [-- [--your-params]]          # run program
dlv test                                  # run tests in PWD
dlv test ./internal/foo                  # run local tests in dir
dlv test example.com/x/foo/package        # run package on GOPATH
dlv test -- -test.run ^TestSanitizePath$  # run matching tests in PWD
# useful test params:
-test.run  # regex test to match
-test.v    # verbose (print tests as run)
</syntaxhighlight>
</blockquote><!-- Start Delve -->
== Breakpoint ==
<blockquote>
You can add breakpoints in src, or interactively
<syntaxhighlight lang="go">
runtime.Breakpoint() // delve breakpoint
</syntaxhighlight>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
dlv debug
b main.main  # add breakpoint to 'main.go's 'main' method
dlv debug example.com/x/foo/package
b main.go:10  # add breakpoint to 'main.go's line 10
</syntaxhighlight>
</blockquote><!-- Breakpoint -->
 
== Session ==
<blockquote>
Add your breakpoints, then continue to execute your program
<syntaxhighlight lang="go">
c            # continue (start executing)
</syntaxhighlight>
 
Inspect your program, learn
<syntaxhighlight lang="go">
p yourVar  # print var
l          # print location
n          # next line (enters loop, not skip to end)
s          # step into line
<enter>    # repeat last command
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Session -->
</blockquote><!-- Usage -->
</blockquote><!-- Usage -->

Latest revision as of 01:15, 17 July 2022

delve is a go debugger.
It is more knowledgeable about the go runtime than golang gdb .
There are also editor integrations.

Documentation

github https://github.com/go-delve/delve
command docs https://github.com/go-delve/delve/blob/master/Documentation/cli/README.md
editor plugins https://github.com/go-delve/delve/blob/master/Documentation/EditorIntegration.md

Locations

~/.config/dlv/config.yml config options

Install

go install github.com/go-delve/delve/cmd/dlv@latest

Configuration

Persistent

# ~/.config/dlv/config.yml

max-string-len: 1000
max-array-values: 1000

Dynamic

Within a delve session, you can modify debugger config values.

config max-string-len 1000

Usage

Delve builds your executable automatically with flags required for it to debug your program.

Start Delve

# start debugger
dlv debug [example.com/x/foo/package] [-- [--your-params]]          # run program

dlv test                                  # run tests in PWD
dlv test ./internal/foo                   # run local tests in dir
dlv test example.com/x/foo/package        # run package on GOPATH
dlv test -- -test.run ^TestSanitizePath$  # run matching tests in PWD

# useful test params:
 -test.run  # regex test to match
 -test.v    # verbose (print tests as run)

Breakpoint

You can add breakpoints in src, or interactively

runtime.Breakpoint() // delve breakpoint
b main.main   # add breakpoint to 'main.go's 'main' method
b main.go:10  # add breakpoint to 'main.go's line 10

Session

Add your breakpoints, then continue to execute your program

c             # continue (start executing)

Inspect your program, learn

p yourVar  # print var
l          # print location
n          # next line (enters loop, not skip to end)
s          # step into line
<enter>    # repeat last command