Skip to content

go1.16 runtime.Callers,the pc[1] is point at Callers itself, but go other version is point at the caller of Callers. #49941

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
ermeiyao opened this issue Dec 3, 2021 · 1 comment

Comments

@ermeiyao
Copy link

ermeiyao commented Dec 3, 2021

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

$ go version
go version go1.16 linux/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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=

What did you do?

package main

import (
	"fmt"
	"runtime"
)

func printMyName() string {
	pc, _, _, _ := runtime.Caller(1)
	return runtime.FuncForPC(pc).Name()
}
func printCallerName() string {
	pc, _, _, _ := runtime.Caller(2)
	return runtime.FuncForPC(pc).Name()
}
func Bar() {
	fmt.Printf("I am %s, %s  call me!\n", printMyName(), printCallerName())
	trace()
}
func trace() {
	fmt.Printf(""I am %s, %s call me!\n", printMyName(), printCallerName())
	pc := make([]uintptr, 10) // at least 1 entry needed
	n := runtime.Callers(0, pc)
	for i := 0; i < n; i++ {
		f := runtime.FuncForPC(pc[i])
		file, line := f.FileLine(pc[i])
		fmt.Printf("%s:%d %s\n", file, line, f.Name())
	}
}

func main() {
	Bar()
}

What did you expect to see?

output info by go1.10:

I am main.Bar, main.main call me!
I am main.trace, main.Bar call me!
/usr/lib/go-1.10/src/runtime/extern.go:212 runtime.Callers
/data/163851302034758975.go:24 main.trace
/data/163851302034758975.go:19 main.Bar
/data/163851302034758975.go:46 main.main
/usr/lib/go-1.10/src/runtime/proc.go:207 runtime.main
/usr/lib/go-1.10/src/runtime/asm_amd64.s:2362 runtime.goexit

What did you see instead?

output info by go1.16:

I am main.Bar, main.main call me!
I am main.trace, main.Bar call me!
/usr/local/go/src/runtime/extern.go:229 runtime.Callers
/usr/local/go/src/runtime/extern.go:229 runtime.Callers ---> here is where i think it should be main.trace
/data/code/learn_go/func_name.go:19 main.Bar
/data/code/learn_go/func_name.go:46 main.main
/usr/local/go/src/runtime/proc.go:234 runtime.main
/usr/local/go/src/runtime/asm_amd64.s:1372 runtime.goexit

@ermeiyao ermeiyao closed this as completed Dec 3, 2021
@golang golang locked and limited conversation to collaborators Dec 3, 2022
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

2 participants