build unoptimized std lib for debugging

314 views
Skip to first unread message

arthurwil...@gmail.com

unread,
Apr 3, 2022, 9:21:00 PM4/3/22
to golang-nuts
I'm trying to build an unoptimized version of the standard library for debugging.
I tried using GO_GCFLAGS='-N -l' but dlv is telling me the code is optimized. 
This is what I did (Notice the "Warning debugging optimized function" coming from delve)

/Volumes/git/goroot/src (master)$ git log --oneline -1
ba6df85c7c (HEAD -> master, origin/master, origin/HEAD) cmd/compile: add MOVBEWstore support for GOAMD64>=3

/Volumes/git/goroot/src (master)$ time GO_GCFLAGS='-N -l' ./make.bash
Building Go cmd/dist using /Users/billmorgan/sdk/go1.18. (go1.18 darwin/amd64)
Building Go toolchain1 using /Users/billmorgan/sdk/go1.18.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for darwin/amd64.
---
Installed Go for darwin/amd64 in /Volumes/git/goroot
Installed commands in /Volumes/git/goroot/bin

real    2m9.101s
user    5m40.243s
sys     0m36.602s
/Volumes/git/goroot/src (master)$ dlv exec ../bin/go -- help
Type 'help' for list of commands.
(dlv) b runtime.main
Breakpoint 1 set at 0x103c68a for runtime.main() ./runtime/proc.go:145
(dlv) c
> runtime.main() ./runtime/proc.go:145 (hits goroutine(1):1 total:1) (PC: 0x103c68a)
Warning: debugging optimized function
   140:
   141: // Value to use for signal mask for newly created M's.
   142: var initSigmask sigset
   143:
   144: // The main goroutine.
=> 145: func main() {
   146:         g := getg()
   147:
   148:         // Racectx of m0->g0 is used only as the parent of the main goroutine.
   149:         // It must not be used for anything else.
   150:         g.m.g0.racectx = 0
(dlv) q

Ian Lance Taylor

unread,
Apr 3, 2022, 11:35:04 PM4/3/22
to arthurwil...@gmail.com, golang-nuts
On Sun, Apr 3, 2022 at 6:21 PM arthurwil...@gmail.com
<arthurwil...@gmail.com> wrote:
>
> I'm trying to build an unoptimized version of the standard library for debugging.

You can just use

go build -gcflags=all="-N -l"

Ian

arthurwil...@gmail.com

unread,
Apr 4, 2022, 6:49:17 PM4/4/22
to golang-nuts
That gives this error:
/Volumes/git/goroot/src (master)$ go build -gcflags=all="-N -l"
no Go files in /Volumes/git/goroot/src
 

Ian Lance Taylor

unread,
Apr 4, 2022, 7:14:30 PM4/4/22
to arthurwil...@gmail.com, golang-nuts
You have to run this wherever you are building your actual Go program.

What I mean is, if you want to "go build x.go" and you want to build
that with a standard library built for maximal debugging, then rn "go
build -gcflags=all="-N -l" x.go". Any flags specified by
-gcflags=all="FLAGS" will be used to build the standard library as
well. You don't need to build the standard library separately.

Ian

arthurwil...@gmail.com

unread,
Apr 4, 2022, 8:00:49 PM4/4/22
to golang-nuts
Thanks Ian. I cleaned the cache and mod cache and passed -x to see all the commands run and noticed go build is not propagating the -N -l flags to the compiler for a lot of the files. 

Also noticed some extra compiler flags (-std, -+) that are not listed in the compile command documentation

What do these -std and -+ compiler flags do? 

Why are some compilation commands missing -N -l?



 

arthurwil...@gmail.com

unread,
Apr 4, 2022, 8:16:29 PM4/4/22
to golang-nuts
I found some clues: 

    CompilingRuntime bool "flag:\"+\" help:\"compiling runtime\""

and:

if Flag.CompilingRuntime && Flag.N != 0 {
log.Fatal("cannot disable optimizations while compiling runtime")
}
 

 

Ian Lance Taylor

unread,
Apr 4, 2022, 8:51:58 PM4/4/22
to arthurwil...@gmail.com, golang-nuts
On Mon, Apr 4, 2022 at 5:16 PM arthurwil...@gmail.com
Yeah.

The -std flag is passed when compiling packages in the standard
library. Certain undocumented compiler pragmas are only permitted for
standard library code.

Ian
Reply all
Reply to author
Forward
0 new messages