Debugging & breakpoints in Go compiler source code

334 views
Skip to first unread message

Mohit Verma

unread,
Mar 8, 2019, 7:24:07 PM3/8/19
to golan...@googlegroups.com
Hi All,

I was looking to see how the golang compiler works, and wanted to step in to the compilation process to see what happens. I am looking at the standard Go compiler in src/cmd/compile from https://github.com/golang/go

When I try to run the compilation process with gdb, I see I can put breakpoints for the code when the "go tool" is triggered (code in src/cmd/go), but not in the code inside cmd/compile/internal. 
I can also put breakpoints for the code in runtime package, and I see runtime package has a special "runtime-gdb.py" file.

Can anyone point me to what is going on here? And how can I step-in to the compiler code?

Thanks!
Mohit

Ian Lance Taylor

unread,
Mar 8, 2019, 7:29:58 PM3/8/19
to Mohit Verma, golang-nuts
The go tool invokes cmd/compile as a subprocess. To see exactly what
the go tool does, run

go tool -x -work

That will show you an invocation of cmd/compile that you can run under
the debugger. Using -work ensures that the temporary files passed to
the compiler will be available; you will have to replace $WORK in the
-x output with the temporary directory printed in the first line of
the cmd/go output.

Note that in general compilation are cached, so to get a useful
cmd/compile execution you may have to first run "go clean -cache".

Ian

Mohit Verma

unread,
Mar 11, 2019, 7:01:04 PM3/11/19
to Ian Lance Taylor, golang-nuts
Thanks Ian. This helped me to identify what processes are launched when go build is called.
I then ran gdb just for the process that compiled the file for which I wanted to debug the compilation: 

gdb --args /home/administrator/Work/go/src/go/pkg/tool/linux_amd64/compile -o /tmp/go-build037119929/b001/_pkg_.a -trimpath /tmp/go-build037119929/b001 -p main -complete -buildid Y52EEDh5fr669VPNVIvF/Y52EEDh5fr669VPNVIvF -D _/home/administrator/Work/go/src/example_test -importcfg /tmp/go-build037119929/b001/importcfg -pack -c=4 ./example.go

Although I get this error when setting any breakpoint:
Dwarf Error: Cannot find DIE at 0x182106 referenced from DIE at 0x1892a7

Do I need to build the Go compiler differently to be able to debug it? Currently, I am just building it through make.bash from the src/ directory.

Thanks!
Mohit

Ian Lance Taylor

unread,
Mar 11, 2019, 7:04:16 PM3/11/19
to Mohit Verma, golang-nuts
On Mon, Mar 11, 2019 at 4:00 PM Mohit Verma <vmoh...@gmail.com> wrote:
>
> Thanks Ian. This helped me to identify what processes are launched when go build is called.
> I then ran gdb just for the process that compiled the file for which I wanted to debug the compilation:
>
> gdb --args /home/administrator/Work/go/src/go/pkg/tool/linux_amd64/compile -o /tmp/go-build037119929/b001/_pkg_.a -trimpath /tmp/go-build037119929/b001 -p main -complete -buildid Y52EEDh5fr669VPNVIvF/Y52EEDh5fr669VPNVIvF -D _/home/administrator/Work/go/src/example_test -importcfg /tmp/go-build037119929/b001/importcfg -pack -c=4 ./example.go
>
> Although I get this error when setting any breakpoint:
> Dwarf Error: Cannot find DIE at 0x182106 referenced from DIE at 0x1892a7
>
> Do I need to build the Go compiler differently to be able to debug it? Currently, I am just building it through make.bash from the src/ directory.

You do not need to build the Go compiler differently in order to debug it.

Unfortunately I do not know what this problem is. I haven't seen this
problem. You could try running Delve instead, though I don't know
whether it would help.

Ian

David Chase

unread,
Mar 12, 2019, 1:48:07 PM3/12/19
to golang-nuts
Wouldn't mind knowing the version of that Go compiler.
What you see there is a bug in generated debugging information (DWARF).
You might be better off using Delve; parts of the compiler are multithreaded (with goroutines, not necessarily threads that gdb understands) and that is not best case for gdb.
Reply all
Reply to author
Forward
0 new messages