Long
Long

Reputation: 69

How to reload source files in golang dlv

I am using dlv to debug golang source code. Is there any command that reloads a program after modifying the source code? I have searched online and the dlv documentation but did not found anything on how to do that.

Any help will be appreciated.

environment : centos

Upvotes: 2

Views: 3210

Answers (3)

Izana
Izana

Reputation: 3135

you can use rebuild to recompile the source and restart the process.

Breakpoint 7 (enabled) at 0x0 for main.main.func3() ./main.go:25 (0)
Breakpoint 8 (enabled) at 0x0 for main.main.func3() ./main.go:28 (0)

If you set breakpoint by location, and modify the source code, the breakpoint might be invalidated. See addr 0x0. From my experience, If I delete the nearby code, the breakpoint is gone.

Upvotes: 0

Jerry Seutter
Jerry Seutter

Reputation: 72

Delve currently does not have a way to reload the compiled code you are running while preserving your state like breakpoints and traces. Probably the best thing you can do is use the "source" command in Delve. For example, if you put a file called debug.txt in the local directory, you can add:

break foo.go:171
on 1 print myVar

..and back in dlv debug:

$ dlv debug
Type 'help' for list of commands.
(dlv) source debug.txt
Breakpoint 1 set at 0x1b0f5e5 for /path/to/foo.MyFunction() ./foo.go:171
(dlv)

your configured breakpoints are loaded.

This isn't a perfect solution and I miss the gdb behavior, so I created a GitHub issue to start a discussion: https://github.com/go-delve/delve/issues/1551

Upvotes: 4

whitespace
whitespace

Reputation: 821

~~ guess you are looking for live code reloading like this ~~

~~ https://github.com/codegangsta/gin ~~

  • 1 if you are looking to debug without exiting vim then: :new | 0read ! dlv debug # will do.

  • 2 if you are looking to load the dlv mode constantly you can probably use tmux with split-pane. With one pane having vim and your file open and the other running dlv. When you change your file in vim(in a pane) delve(in the companion pane) reloads this file for debugging.

  • 3 if you just want to run debug at the time of saving the file, prefix the first command with autocmd BufWritePre Filetype go and add that to your virmc.

If you are looking for some command that rebuilds your executable each time you make a change, I think you are in the wrong place, debugging is not meant to rebuild the executable. After you are done debugging, you can check your application and run the command(some variation of go build or some wrapper that calls it). To build your executable.

Upvotes: -1

Related Questions