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

Closed
@ermeiyao

Description

@ermeiyao

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions