Debugging with Delve: execute function - debugging

I setup a breakpoint in my Go code with runtime.Breakpoint(), save the file (my editor, Atom with go-plus installed, runs go install . on save). Then I run Delve in terminal with dlv debug, and type continue after it starts.
When the breakpoint kicks in, I want to test a couple of things (basically to print reader's data via a bytes.Buffer). But, I get the following error
buf := new(bytes.Buffer): "1:5: expected 'EOF', found ':='"
and in general cannot do much more than print values.
Is it really not possible to do this sort of thing? I am used to Python's pdb where setting variables or calling functions is not a problem and I would expect Delve is capable of the same.
So, what am I doing wrong?

Not possible yet. Right now (2018-NOV) work is in progress on Delve, but unfinished.
Go runtime was changed recently to allow this kind of call. Delve have a Github issue tracking the progress of such feature, but is still experimental and incomplete.

Related

AOSP lldbclient.py missing?

I'm building AOSP from source and have created a small C++ program that prints some messages to logcat when started. Now I wanted to debug the program according to https://source.android.com/devices/tech/debug/gdb
In the reference you're encouraged to use lldb in favor of gdb and there is also a short section on using vs code as debugger.
However, I cannot find the mentioned script lldbclient anywhere in my source nor in Android Code Search, only gdbclient.py seems to be present.
Q1: Where can I find lldbclient script?
When running the gdbclient.py script the option --setup-forwarding vscode seems to be ignored and gdb is always started.
Q2: If there isn't a lldbclient script, what options do I have to pass to gdbclient.py to enable debugging with lldb and vs code?
What did I do so far?
gdbclient.py -r /data/mysample_bin --setup-forwarding vscode
Starts my native program with attached gdb and allows me to step through my program.
Though I do not know how to code python, I was able to track down a call sequence in the script to method generate_setup_script, which is called with parameter debugger=gdb. Therefore no lldb configuration for vs code is created. Passing --no-gdb or --lldb to the script doesn't change this behavior.
Q1: You can find lldbclient.py script in the repository https://android.googlesource.com/platform/development, branch android-s-beta-2 (or another android-s branch).
Q2: Android also provides some tutorial debugging with Vscode: https://source.android.com/devices/tech/debug/gdb#vscode.

What does <autogenerated>:1 mean when debugging Go package with delve?

I was trying to debug a Go package with lots of dependencies, trying to trace function calls across dependencies to get to the bottom of a bug.
Delve – via both CLI and VSCode GUI debugger – followed function calls to a certain depth, but then suddenly refused to go any deeper and instead of allowing me to access the local variables in that function call, produced a cryptic <autogenerated>:1 (PC: 0x141e850).
On VSCode I get this error instead: go list failed on .: can't load package: package .: no Go files in C:\Program Files\Microsoft VS Code
I know this is kind of vague, but can anybody point me to what this might mean?
This might answer your doubt: Github: go-delve/delve #1908
Read the full thread.
For anyone who runs into this in the future:
The solution is to set breakpoints IN the functions that are getting called as well. I tried this, and it does in fact follow the function calls properly.

How can I diagnose, debug MinGW-get GUI (guimain.exe) crashing?

Initially, I wanted to install the updated MinGW packages using the MinGW-get GUI, but it crashes when I choose "Mark All Upgrades" from the 'Installation' drop-down menu (whether I update the catalogue or not).
Now, I want to figure out why guimain.exe is crashing.
How can I diagnose and debug this persistent problem? I am vaguely familiar with GDB, but I've never used it. I am not committed to using the Visual Studio debugger.
I assume I need to use some ancillary binaries or debug libraries in the latest MinGW installer branch here: https://sourceforge.net/projects/mingw/files/Installer/mingw-get/mingw-get-0.6.2-beta-20131004-1/
Can anyone please guide me? Any assistance or suggested reading is appreciated.
For those who are experiencing the crash on attempting to upgrade packages.. try running it in recursive mode..
mingw-get upgrade --recursive
that appeared to force an upgrade for me..
I happen to have the same issue. The GDB way to debug this:
gdb mingw-get.exe
r upgrade (run with argument upgrade)
GDB will automatically stop at a SIGSEGV (Segmentation fault) signal, that's what happens for me.
bt (get a backtrace)
For me this resulted in a huge backtrace, filled with calls to:
#5013 0x6f9cdda4 in mingw-get-0!_ZN14pkgXmlDocument19ResolveDependenciesEP10pkgXmlNodeP13pkgActionItem () from c:\MinGW\libexec\mingw-get\mingw-get-0.dll
It looks like a recursion problem that quickly filled up the call stack.
Note that function name is mangled, and we don't know the line number. This is the best you can do without debugging symbols. If you have the debugging symbols, GDBs output becomes more useful. For most Linux-style packages, you can get the debugging symbols in a -dbg package named similiarly to the stripped binary package. However I don't see a debug package for mingw32-mingw-get in the listing.

pyCharm Debugging: skip framework code

Is there a way to tell pyCharm that it should skip framework code? The debugger should skip all lines which are not from me.
In my case it is very easy to tell which code I want to debug and which not:
Code in virtualenv lib should be skipped
Code in virtualenv src should be debugged.
If I press F7 (Step Into) it should skip all lines which are not from my code base.
[Update May 2015: introduced in PyCharm 4.5]
There are two new features now, one of which is the one you asked for, but I mention the other one as well because it is topically very close.
From the 4.5 release notes:
Step into My Code
Stay focused on your code by telling the debugger to step only through your project code, as opposed to stepping through the library sources.
[...]
Ignore Library Files
The debugger is improved with the new 'Ignore library files' option. Use it to have the debugger stop inside your code in case the exception is raised in a library module, instead of stopping inside the library code.
[Update after learning about blackboxing libraries in debugging]
In this answer it is mentioned that you can add the modules to ignore into "the dict DONT_TRACE in /helpers/pydev/pydevd.py"
And there is an open issue on the issue tracker.
[original answer]
It is not possible to skip code like that, but you can flexibly switch between walking through the code line by line and making bigger jumps in a running debug session by simply adding another breakpoint (while debugging - break points can be changed in a running debug session) at the position after the library code you want to skip and press 'Resume Program' in the Debugger. The library code is skipped and you are back in your code.
You might also want to use conditional breakpoints to make sure that the program breaks into the debugger exactly when the program is in the state that you desire: right click on a breakpoint and enter a condition that has to evaluate to True in the context of that line.

Does Go provide REPL?

The interactive environment is VERY helpful for a programmer. However, it seems Go does not provide it. Is my understanding correct?
No, Go does not provide a REPL(read–eval–print loop).
However, as already mentioned, Go Playground is very handy. The Go Authors are also thinking about adding a feature-rich editor to it.
If you want something local, consider installing hsandbox. Running it simply with hsandbox go will split your terminal screen (with screen) where you can write code at the top and see its execution output at the bottom on every save.
There was a gotry among standard Go commands, which used to evaluate expressions (with an optional package name), and could be run like gotry 1+2 and gotry fmt 'Println("hello")' from shell. It is no longer available because not many people actually used it.
I have also seen third party projects for building a REPL for Go, but now I can only find links to two of them: igo and go-repl. How well do they work I don't know.
My two cents: Speed of compilation makes writing a REPL possible for Go, as it has also helped building the tools mentioned here, but the same speed makes REPL less necessary. Every time I want to test something in Go that I can't run in Playground I open a simple .go file and start coding and simply run the code. This will be even easier when the go command in Go 1 makes one-command build process possible and way easier.
UPDATE: Latest weekly release of Go added go command which can be used to very easily build a file: write your prog.go file and run go build prog.go && ./prog
UPDATE 2: With Go 1 you can directly run go programs with go run filename.go
UPDATE 3: gore is a new project which seems interesting.
Try motemen/gore
Yet another Go REPL that works nicely. Featured with line editing,
code completion, and more.
https://github.com/motemen/gore
You also have a recent (March 2013) project called gore from Sriram Srinivasan, which can be useful:
gore is a command-line evaluator for golang code -- a REPL without a loop, if you will.
It is a replacement for the go playground, while making it much easier to interactively try out bits of code: gore automatically supplies boiler-plate code such as import and package declarations and a main function wrapper.
Also, since it runs on your own computer, no code is rejected on security grounds (unlike go playground's safe sandbox mode).
If you're a Vim user, the vim-go plugin (https://github.com/fatih/vim-go) provides a command (GoRun) to run and print the output of the current buffer. You still have to include all the boilerplate code of a main Go file, but it still provides a convenient way to quickly test code snippets in your local environment.
Have you tried the Go Playground?
About the Go Playground
The Go Playground is a web service that runs on golang.org's servers.
The service receives a Go program, compiles, links, and runs the
program inside a sandbox, then returns the output.
The GoSpeccy project includes a builtin REPL of a restricted subset of the Go language. The implementation is using goeval.
No, but you can exploit the speed of compilation (as mentioned in other answers).
Have a look at rango that uses a generate-compile-run loop to mimic a REPL. You can also start it with imports and statements to begin an interactive session.
Gosh is the interactive Golang shell. The goal is to provide an easy-to-use interactive execution environment.
https://github.com/mkouhei/gosh
I've had some luck with the VSCode debugger, but it's fairly limited in so far as you cannot invoke function calls from the debug console Debug: Function Calls not supported #2225.
Basically you set a breakpoint after properly configuring your launch.json file. Then you can drill down on the left in the variables side bar and enter variable expressions an the debug console.
You may also like to try https://github.com/haya14busa/goplay
This enables you to run go code files from your terminal directly to the Go Playground
Please also check www.gorepl.com for go REPL and other REPLs
Go code can be run in a REPL-like way in Visual Studio Code with the Go extension and Code Runner extension. Click the Run triangle ▶ which is marked by the mouse cursor in the below screenshot to run the code and show the results in the Output pane at the bottom of Visual Studio Code.
When programming with Go Visual Studio Code will suggest additional Go extensions that can be installed to extend Visual Studio Code's functionality.

Resources