Skip to content

runtime.Callers does not include all callers. #29526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
uberbo opened this issue Jan 3, 2019 · 4 comments
Closed

runtime.Callers does not include all callers. #29526

uberbo opened this issue Jan 3, 2019 · 4 comments

Comments

@uberbo
Copy link

uberbo commented Jan 3, 2019

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

go version go1.12beta1 darwin/amd64

Does this issue reproduce with the latest release?

It is not on the latest non-beta release.

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/bos/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bos/gocode"
GOPROXY=""
GORACE=""
GOROOT="/Users/bos/sdk/go1.12beta1"
GOTMPDIR=""
GOTOOLDIR="/Users/bos/sdk/go1.12beta1/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zx/8zmf02c16knfz261k7w0p12m0000gn/T/go-build733608340=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/dduJxdG9L1U

What did you expect to see?

0: runtime.Callers
1: main.getFunctionNames
2: main.functionCallingGetFunctionNames
3: main.main
4: runtime.main
5: runtime.goexit

What did you see instead?

0: runtime.Callers
1: main.getFunctionNames
2: main.main
3: runtime.main
4: runtime.goexit
@Neverik
Copy link
Contributor

Neverik commented Jan 3, 2019

Maybe it's just improved inlining (i.e. because functionCallingGetFunctionNames was short, its body got inserted into the call site)?

See https://github.com/golang/go/wiki/CompilerOptimizations#function-inlining for more info.

You can check whether it was actually inlined using -gcflags -m when compiling with go build.

@uberbo
Copy link
Author

uberbo commented Jan 3, 2019

Maybe it's just improved inlining (i.e. because functionCallingGetFunctionNames was short, its body got inserted into the call site)?

See https://github.com/golang/go/wiki/CompilerOptimizations#function-inlining for more info.

You can check whether it was actually inlined using -gcflags -m when compiling with go build.

That sounds very likely, it will make stack traces from https://github.com/pkg/errors look weird though. E.g:

main.getFunctionNames
	/gocode/src/github.com/pkg/errors/errors.go:104
runtime.skipPleaseUseCallersFrames
	/sdk/go1.12beta1/src/runtime/asm.s:40
main.main
	/gocode/src/playground/go12beta/main.go:22
runtime.main
	/sdk/go1.12beta1/src/runtime/proc.go:200
runtime.goexit
	/sdk/go1.12beta1/src/runtime/asm_amd64.s:1337

Whereas go 1.11.4 produces:

main.getFunctionNames
	/gocode/src/playground/go12beta/main.go:44
main.functionCallingGetFunctionNames
	/gocode/src/playground/go12beta/main.go:31
main.main
	/gocode/src/playground/go12beta/main.go:15
runtime.main
	/usr/local/Cellar/go/1.11.4/libexec/src/runtime/proc.go:201
runtime.goexit
	/usr/local/Cellar/go/1.11.4/libexec/src/runtime/asm_amd64.s:1333

@uberbo
Copy link
Author

uberbo commented Jan 3, 2019

Even if I add logic to the dummy function that prevents inlining, the stack tracing looks incorrect.
https://play.golang.org/p/EdwAz1v3a1Y

main.getFunctionNames
	/gocode/src/github.com/pkg/errors/errors.go:104
runtime.skipPleaseUseCallersFrames
	/sdk/go1.12beta1/src/runtime/asm.s:40
main.functionCallingGetFunctionNames
	/gocode/src/playground/go12beta/main.go:32
main.main
	/gocode/src/playground/go12beta/main.go:16
runtime.main
	/sdk/go1.12beta1/src/runtime/proc.go:200
runtime.goexit
	/sdk/go1.12beta1/src/runtime/asm_amd64.s:1337

It still contains skipPleaseUseCallersFrames and it uses the wrong file for main.getFunctionNames.

@cherrymui
Copy link
Member

It still contains skipPleaseUseCallersFrames

As this name suggests, as well as documented in https://golang.org/pkg/runtime/#Callers, please use runtime.CallersFrames (https://golang.org/pkg/runtime/#CallersFrames) instead of FuncForPC over the PCs.

@golang golang locked and limited conversation to collaborators Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants