How do I print the full value of a string variable in delve?
Asked Answered
A

4

38

I'm using the delve go debugger to debug some code. When I try to print a string variable, it gives me an abbreviated version.

(dlv) print myString
"my string...+539 more"

How do I get it to print the full string?

Antenna answered 20/9, 2018 at 1:30 Comment(0)
A
39

The ability to configure the length of printed strings was recently added to delve. To see the full list of configuration options, run config -list;

(dlv) config -list
aliases            map[]
substitute-path    []
max-string-len     <not defined>
max-array-values   <not defined>
show-location-expr false

The one we're interested in here is called max-string-len, which you can see is currently <not defined>. To increase the length to e.g. 1000, run

(dlv) config max-string-len 1000

Now running print myString should print the whole string.

Antenna answered 20/9, 2018 at 1:30 Comment(2)
1024 is more idiomatic ;)Kirstenkirsteni
Tried this in the VSCode debug console but Unable to evaluate expression: 1:8: expected 'EOF', found max.Andresandresen
M
23

Just to add to your answer, if you're using VS Code debugging feature, add the following config to your settings.json:

    "go.delveConfig": {
        "dlvLoadConfig": {
            "maxStringLen": 1024,
        },
        "apiVersion": 2,
    },
Morez answered 4/7, 2019 at 13:59 Comment(0)
H
13

Adding to the above answers, to have these configs applied each time you run dlv, you should be able to find the config file in (see the sourcecode):

  • $HOME/.dlv/config.yml by default on MacOS
  • $HOME/.dlv/config.yml by default on Linux. If $XDG_CONFIG_HOME is set then it should be in $XDG_CONFIG_HOME/dlv/config.yml

For example, the relevant region in the config.yml file:

...
# Maximum number of elements loaded from an array.
max-array-values: 1000

# Maximum loaded string length.
max-string-len: 1000
...
Type 'help' for list of commands.
(dlv) config -list
...
max-string-len         1000
max-array-values       1000
...
Hurds answered 4/11, 2019 at 16:53 Comment(1)
you can also run config -save from inside a running dlv instance (to save config values once you get them how you like them)Kilohertz
G
1

VSCode's Go Debugger

I need to view large strings. How can I do that if dlvLoadConfig with maxStringLen is deprecated?

The legacy adapter used dlvLoadConfig as one-time session-wide setting to override dlv's conservative default variable loading limits, intended to protect tool's performance. The new debug adapter is taking a different approach with on-demand loading of composite data and updated string limits, relaxed when interacting with individual strings. In particular, if the new default limit of 512, applied to all string values in the variables pane, is not sufficient, you can take advantage of a larger limit of 4096 with one of the following:

  • Hover over the variable in the source code
  • Copy as Expression to query the string via REPL in the DEBUG CONSOLE panel
  • Copy Value to clipboard

Please open an issue if this is not sufficient for your use case or if you have any additional feedback.

Source

Glacial answered 25/11, 2022 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.