Closed
Description
What did you do?
https://play.golang.org/p/ZJ4aswqjbQ
package main
import "runtime/debug"
func main() {
// correct
println(trace().stack)
// also correct
t := trace()
println(t.getStack())
// inlined getStack method appears in stack trace
// as the caller of func trace()
println(trace().getStack())
}
func trace() callStack {
return callStack{
stack: string(debug.Stack()),
}
}
type callStack struct {
stack string
}
func (c callStack) getStack() string {
return c.stack
}
https://play.golang.org/p/wVO2lrLiAk
package main
import (
"runtime"
)
func main() {
// prints "main.main" as expected
println(caller().frame.Function)
// prints main.call.name with default compile, but prints "main.main" if
// compiled with -gcflags="-l"
println(caller().name())
}
func caller() call {
var pcs [3]uintptr
n := runtime.Callers(1, pcs[:])
frames := runtime.CallersFrames(pcs[:n])
frame, _ := frames.Next()
frame, _ = frames.Next()
return call{frame: frame}
}
type call struct {
frame runtime.Frame
}
func (c call) name() string {
return c.frame.Function
}
What did you expect to see?
- Methods called after a stack trace has been captured should not appear in the stack trace.
What did you see instead?
- The
getStack
andname
methods logically called after a stack trace had been captured appear in the stack trace as the caller of a function they don't call.