Does any golang interactive debugger exist?

39,414

Solution 1

Update: Personally, while GDB works I'm not a fan of using it in Go and it will make you spit some blood. Check out some of the other answers for good alternatives.


Yes, of course :)

Go has a debugger (GDB)

Here is the official tutorial on how to use it.

If you'd like 'graphical debugging' (that is, setting breakpoints in the editor) some IDEs let you do that (with GDB in the background).

In specific, Eclipse, LiteIDE and Zeus all let you set breakpoints and debug from your coding environment (source). Here is a video on how to do it with Zeus.

Solution 2

GDB support for go has lots of issues that won't be fixed by the go team.

For more information, read the post by Rob Pike:

Although we will endeavor to keep basic gdb functionality (stack traces, printing values) working on supported platforms, the ability to use the debugger to understand a Go program's full environment will likely never work, and improving gdb support is not a priority for the team.

They are looking for other debugging options but have no concrete plans by now. The documentation is outdated and the runtime-gdb.pyscript coming with go 1.2 does not work for a GDB that was compiled with python3 support (current Ubuntu for example).

Solution 3

Update 2017: the godebug project mentions below is now official replaced by derekparker/delve.


Original answer:

You now (March 2015) have another approach, based on instrumenting the code.

mailgun/godebug:

godebug uses source code generation to instrument your program with debugging calls.
go tool cover takes a similar approach to code coverage.

  • When you run godebug, it parses your program, instruments function calls, variable declarations, and statement lines, and outputs the resulting code somewhere (currently either stdout or in place over your original files).
  • When you run this modified code, assuming you put a breakpoint somewhere, you can step through it and inspect variables.

Coming later: evaluate arbitrary Go expressions and write to variables.


Update June 2015:

While it might not be as interactive as "some" might hope, it is still appreciated (and has "step into" feature).
See "Go has a debugger—and it's awesome!" (Cloudfare)

here's the cool bit: instead of wrestling with half a dozen different ptrace interfaces that would not be portable, godebug rewrites your source code and injects function calls like godebug.Line on every line, godebug.Declare at every variable declaration, and godebug.SetTrace for breakpoints (i.e. wherever you type _ = "breakpoint").

I find this solution brilliant.
What you get out of it is a (possibly cross-compiled) debug-enabled binary that you can drop on a staging server just like you would with a regular binary.

When a breakpoint is reached, the program will stop inline and wait for you on stdin.

It's the single-binary, zero-dependencies philosophy of Go that we love applied to debugging. Builds everywhere, runs everywhere, with no need for tools or permissions on the server.

ifdef GODEBUG  
    GOPATH="${PWD}" go install github.com/mailgun/godebug
    GOPATH="${PWD}" ./bin/godebug build -instrument "${GODEBUG}" -o bin/rrdns rrdns

Debugging is just a make bin/rrdns GODEBUG=rrdns/... away.

Solution 4

UPDATE:

I have checked it out and am happy to report that Version: 2016.1.3, Build: 145.1617.8, Released: June 5, 2016 works with Delve! You can download it here: https://www.jetbrains.com/idea/download/. Also follow the Delve install instructions here: https://github.com/derekparker/delve/tree/master/Documentation/installation

It's a bit flaky. Just after I got the OSX login prompt the interactive debugging started working. Sometimes, I have to debug a simple .go program to kick start it. But it does work and is the best interactive debugging experience for Go that I've seen.

ORIGINAL POST:

Does any golang interactive debugger exist? Yes.

Does any golang interactive debugger, that is worth using, exist? No.

Configuring GDB on the mac is tedious, but doable.

However, once you start using it you'll soon realize that you just wasted your time installing it.

You can even configure IntelliJ to use it.

The only value that IntelliJ, LiteIDE, CGDB, etc. seem to provide is that you can more quickly ascertain that GDB debugging support for Go is extremely poor.

You can use it to step through some Go code, but try to print the value of anything other than very simple variable values and you'll be wasting your time wishing for a decent debugger.

Here's an example of what happens when you try to print the value of a map[string]string data structure using CGDB:

(gdb) print params
$1 = (github.com/go-martini/martini.Params) 0x15582 <runtime.reentersyscall+450>

...which is completely useless.

Next, try this:

(gdb) print params["UserID"]

...and you'll get "Bus error".

Delve (https://github.com/derekparker/delve) looks promising, since it is written in Go, but you drive it using the console, not via an IDE.

I would gladly pay for the enterprise version of IntelliJ (or any other IDE) that did a decent job supporting interactive debugging in Go.

As of now, fmt.Printf("%v", variable) is about as good as it gets.

Solution 5

EDIT

The GO debugger is now a simple plugin in IntelliJ or PyCharm, no need to install anything else. Just look for the Go plugin in the plugin preferences.

Original answer (Nov 2015)

for those looking for the latest as of end of Nov 2015:

get delve

https://github.com/derekparker/delve

and follow the build / setup instructions:

https://github.com/derekparker/delve/wiki/Building

Get the latest IntelliJ (15) or PyCharm (5)

https://www.jetbrains.com/idea/download/

and get the go-lang-plugin in the IDE of your choice:

Pycharm -> Preference -> plugins -> search for go
current version is Version: 0.10.749
IntelliJ -> Preference -> plugins -> search for go
current version is Version: 0.10.749
  1. Setup a new Go project, or Import a project.
  2. Setup you Go SDK as prompted
  3. Setup your Go library (usually in GOROOT/src
  4. Setup a Run Application configuration top right corner by the 'play' icon:
  5. Edit Configurations -> click + -> Go Application

Define you package or file to run.

Once done with the setup, the Play icon and Debug icon should now be active, and you can set breakpoint, watches etc... as usual.

Cheers

Share:
39,414

Related videos on Youtube

Daniel Williams
Author by

Daniel Williams

Working software professional specializing in highly scalable applications. You can reach me at enknamel AT gmail DOT com SOreadytohelp

Updated on July 08, 2022

Comments

  • Daniel Williams
    Daniel Williams almost 2 years

    The title pretty much sums it up. I am trying out Go and I really miss being able to set breakpoints and step in/over/out as much as I want within an interactive environment. I know I can use gdb to debug Go but that is quite annoying compared to using an IDE that can plug into gdb for breakpointing.

    I tried searching for one and could only find plugins or small IDEs that have syntax highlighting but no debugging.

    • Leigh
      Leigh over 7 years
      To anyone else who lands on this question, make sure you check out the other answers, not just the (currently accepted) one about GDB
  • Daniel Williams
    Daniel Williams almost 11 years
    As a follow up I am on a mac and I've found the IntelliJ go plugin to make development very nice and easy but the LiteIDE is really great at debugging (though not perfect it works very well). I did have to install an updated GDB to make it work at all which was kind of painful.
  • metakeule
    metakeule almost 10 years
    It has lots of issues and will not be supported by the go team in the future (apart from basic functionality), see my answer below
  • Valerio
    Valerio almost 10 years
    that's not reassuring for a language that is starting being adopted now
  • metakeule
    metakeule almost 10 years
    @ValerioColtrè I never needed a debugger in go. unit tests and Printf("%#v", ...) served me well so far...
  • gotgenes
    gotgenes over 9 years
    @metakeule Nobody needs a debugger, however, a good debugger can prove incredibly helpful and more efficient in debugging - especially in debugging failing tests. Plus, debuggers do not alter your code, while print statements do.
  • masukomi
    masukomi over 9 years
    From the GDB docs: "GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger, even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations, it is not a reliable debugger for Go programs, particularly heavily concurrent ones"
  • Ivan Chau
    Ivan Chau almost 9 years
    How does Go work with debuggers? "We’ve talked for a while about a custom Go debugger, but there isn’t one yet." by Russ Cox
  • Michele Giuseppe Fadda
    Michele Giuseppe Fadda over 8 years
    Not anymore! outdated answer.
  • l3x
    l3x over 8 years
    mailgun/godebug is a vast improvement; It is worth using. However, what would be very much worth using is an IDE that had an integrated, interactive debugger. Debugging from the console is reminiscent of debugging in the 80's. Adding instrumentation to my Go code works, but is a bit intrusive. I'd rather just (un)click on a row of code to (un)set a breakpoint.
  • Michele Giuseppe Fadda
    Michele Giuseppe Fadda over 8 years
    I will restate... dvelve debugger + intelliJ idea IDE.!
  • Michele Giuseppe Fadda
    Michele Giuseppe Fadda over 8 years
    GDB on go broke ages ago and is no longer functional. Accepted answer is outright WRONG.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 8 years
    @user4839438 uhh, the first two lines are literally "don't use gdb on go". I personally used godebug which was pretty nice last time I go'd.
  • Michele Giuseppe Fadda
    Michele Giuseppe Fadda over 8 years
    You are right, but the title says "Go has a debugger (gdb)", which actually isn't true anymore and is currently a disservice. GDB used to work with old versions of golang, trying to make it work was a big pita, and in the end, it did not work and can't possibly work. I'm an happy puppy with Dwelve, I'll try godebug as well.
  • Benjamin Gruenbaum
    Benjamin Gruenbaum over 8 years
    Feel free to post an answer - this answer literally starts with "Personally, while GDB works I'm not a fan of using it in Go and it will make you spit some blood. Check out some of the other answers for good alternatives." :)
  • l3x
    l3x over 8 years
    Hey user4839438, I just noticed your post. Will try it out. If that works, it would certainly be worth paying any JetBrains upgrade fees. And not that it matters to me, but I did hear that the Delve debugger supposedly works in MS Visual Studio. See marketplace.visualstudio.com/items/lukehoban.Go
  • MrE
    MrE over 8 years
    Can you explain how you set it up with IntelliJ? The debugger must run as an HTTP endpoint, doesn't it?
  • MrE
    MrE over 8 years
    nevermind... found the docs etc... but apparently requires the latest version of PyCharm or IntelliJ to work out of the box.
  • Michele Giuseppe Fadda
    Michele Giuseppe Fadda over 8 years
    I did nothing special, I just installed Dvelve and intelliJ 14, It still works correctly with IntelliJ 15. About previous versions: I don't know.
  • Stefan Wuthrich - Altafino
    Stefan Wuthrich - Altafino over 8 years
    evaluate/watch dont work for me: could not find symbol value for... Could you resolve this for you ?
  • MrE
    MrE over 8 years
    watch and evaluate seem to work fine for me, in PyCharm 5.0.3 CE as well as IntelliJ IDEA 15.0.2
  • dlsniper
    dlsniper over 8 years
    You actually don't need to build delve, the plugin comes with it bundled automatically.
  • MrE
    MrE over 8 years
    @dlsniper is this recent? When I tried, i had had the go plugin install for a while, i upgraded but it didn't work until I built delve.
  • dlsniper
    dlsniper over 8 years
    @MrE delve has been shipped with the plugin since it was integrated, which is about 3 months ago. But it works only on Linux & Mac OS X both 64bit.
  • l3x
    l3x almost 8 years
    I have checked it out and am happy to report that Version: 2016.1.3, Build: 145.1617.8, Released: June 5, 2016 works with Delve! You can download it here: jetbrains.com/idea/download. Also follow the Delve install instructions here: github.com/derekparker/delve/tree/master/Documentation/…
  • Ostati
    Ostati over 7 years
    Delve is lagging behind though but still better than others IMO.
  • Daniel Williams
    Daniel Williams about 7 years
    Very nice! LiteIDE has been a bit ... too light. I love IntelliJ and will check this out.
  • gideon
    gideon over 6 years
    This IMO is the best answer. Option #3 it's also a multi platform solution. Delve has some limitations but basic visual debugging works rather well.
  • Alexandro de Oliveira
    Alexandro de Oliveira over 6 years
    Godebug is now deprecated and they recommend to use Delve instead
  • VonC
    VonC over 6 years
    @AlexandrodeOliveira Thank you. I have updated the answer accordingly.
  • Edwin O.
    Edwin O. over 6 years
    that is a very UGLY thing how Rob Pike puts it
  • Gerry Gleason
    Gerry Gleason over 5 years
    Trying out delve, seems to work pretty well. Seems to me this answer of a decent open source tool makes the question less "off topic". I get not wanting to rate closed tools. Maybe the question needs to be refrased, but I got hear searching for alternatives to gdb after finding it (gdb) isn't really recommended for go.
  • Vipin Mohan
    Vipin Mohan over 2 years
    anyone heard of or used delve? github.com/go-delve/delve