golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.07k stars 17.68k forks source link

cmd/compile: Adding `-gcflags "all=-N -l"` does not add debugging information. #64082

Closed aimuz closed 1 year ago

aimuz commented 1 year ago

What version of Go are you using (go version)?

$ go version
go version devel go1.22-f7b4f02ba0 Fri Aug 4 15:41:19 2023 +0000 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/aimuz/Library/Caches/go-build'
GOENV='/Users/aimuz/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=' -mod=mod'
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/aimuz/go/pkg/mod'
GONOPROXY='gitee.com/wisecloud,gitee.com/wisecloud-archive'
GONOSUMDB='gitee.com/wisecloud,gitee.com/wisecloud-archive'
GOOS='darwin'
GOPATH='/Users/aimuz/go'
GOPRIVATE='gitee.com/wisecloud,gitee.com/wisecloud-archive'
GOPROXY='https://goproxy.dev,https://goproxy.io,https://goproxy.cn,direct'
GOROOT='/Users/aimuz/workspace/go'
GOSUMDB='sum.golang.google.cn'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/aimuz/workspace/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='devel go1.22-f7b4f02ba0 Fri Aug 4 15:41:19 2023 +0000'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/aimuz/workspace/go/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/pt/qtn3qrvs6n9b_944ggd01nj00000gn/T/go-build4175838830=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Master branch compilations cannot be debugged using dlv.

Programs built with the -gcflags "all=-N -l" parameter cannot be debugged.

After investigation, the problem was caused by changes to https://go-review.googlesource.com/c/go/+/461697

When I change back to the historical version of the code, I can debug it properly

func DefaultPIE(goos, goarch string, isRace bool) bool {
    switch goos {
    case "android", "ios":
        return true
    case "windows":
        if isRace {
            // PIE is not supported with -race on windows;
            // see https://go.dev/cl/416174.
            return false
        }
        return true
    case "darwin":
        return goarch == "arm64"
    }
    return false
}
ld -v
@(#)PROGRAM:ld  PROJECT:dyld-1015.7
BUILD 16:59:22 Oct  1 2023
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
will use ld-classic for: armv6 armv7 armv7s arm64_32 i386 armv6m armv7k armv7m armv7em
LTO support using: LLVM version 15.0.0 (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 15.0.0 (tapi-1500.0.12.3)
Library search paths:
Framework search paths:

What did you expect to see?

Be able to use dlv to debug programs properly

What did you see instead?

cannot be debugged

aimuz commented 1 year ago

When I add the -ldflags "-buildmode=exe" parameter, he works fine!

aarzilli commented 1 year ago

Debug info is always included in the executable, -N -l just disables optimizations. Your problem is different: probably an just old version of delve, but it's hard to tell for sure because you haven't really described what's happening.

aimuz commented 1 year ago

You are right, when I use the latest version of dlv he works fine, maybe older versions of dlv don't support PIE format executables?

aarzilli commented 1 year ago

It was probably this https://github.com/go-delve/delve/pull/3467

aimuz commented 1 year ago

thank you