runtime: handle inlined calls in runtime.Callers
The `skip` argument passed to runtime.Caller and runtime.Callers should
be interpreted as the number of logical calls to skip (rather than the
number of physical stack frames to skip). This changes runtime.Callers
to skip inlined calls in addition to physical stack frames.
The result value of runtime.Callers is a slice of program counters
([]uintptr) representing physical stack frames. If the `skip` parameter
to runtime.Callers skips part-way into a physical frame, there is no
convenient way to encode that in the resulting slice. To avoid changing
the API in an incompatible way, our solution is to store the number of
skipped logical calls of the first frame in the _second_ uintptr
returned by runtime.Callers. Since this number is a small integer, we
encode it as a valid PC value into a small symbol called:
runtime.skipPleaseUseCallersFrames
For example, if f() calls g(), g() calls `runtime.Callers(2, pcs)`, and
g() is inlined into f, then the frame for f will be partially skipped,
resulting in the following slice:
pcs = []uintptr{pc_in_f, runtime.skipPleaseUseCallersFrames+1, ...}
We store the skip PC in pcs[1] instead of pcs[0] so that `pcs[i:]` will
truncate the captured stack trace rather than grow it for all i.
Updates #19348.
Change-Id: I1c56f89ac48c29e6f52a5d085567c6d77d499cf1
Reviewed-on: https://go-review.googlesource.com/37854
Run-TryBot: David Lazar <lazard@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-03-06 12:48:36 -07:00
|
|
|
// run -gcflags -l=4
|
|
|
|
|
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var skip int
|
|
|
|
var npcs int
|
|
|
|
var pcs = make([]uintptr, 32)
|
|
|
|
|
|
|
|
func f() {
|
|
|
|
g()
|
|
|
|
}
|
|
|
|
|
|
|
|
func g() {
|
|
|
|
h()
|
|
|
|
}
|
|
|
|
|
|
|
|
func h() {
|
|
|
|
npcs = runtime.Callers(skip, pcs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCallers(skp int) (frames []string) {
|
|
|
|
skip = skp
|
|
|
|
f()
|
|
|
|
for i := 0; i < npcs; i++ {
|
|
|
|
fn := runtime.FuncForPC(pcs[i])
|
|
|
|
frames = append(frames, fn.Name())
|
|
|
|
if fn.Name() == "main.main" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:14:12 -07:00
|
|
|
func testCallersFrames(skp int) (frames []string) {
|
|
|
|
skip = skp
|
|
|
|
f()
|
|
|
|
callers := pcs[:npcs]
|
|
|
|
ci := runtime.CallersFrames(callers)
|
|
|
|
for {
|
|
|
|
frame, more := ci.Next()
|
|
|
|
frames = append(frames, frame.Function)
|
|
|
|
if !more || frame.Function == "main.main" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
runtime: handle inlined calls in runtime.Callers
The `skip` argument passed to runtime.Caller and runtime.Callers should
be interpreted as the number of logical calls to skip (rather than the
number of physical stack frames to skip). This changes runtime.Callers
to skip inlined calls in addition to physical stack frames.
The result value of runtime.Callers is a slice of program counters
([]uintptr) representing physical stack frames. If the `skip` parameter
to runtime.Callers skips part-way into a physical frame, there is no
convenient way to encode that in the resulting slice. To avoid changing
the API in an incompatible way, our solution is to store the number of
skipped logical calls of the first frame in the _second_ uintptr
returned by runtime.Callers. Since this number is a small integer, we
encode it as a valid PC value into a small symbol called:
runtime.skipPleaseUseCallersFrames
For example, if f() calls g(), g() calls `runtime.Callers(2, pcs)`, and
g() is inlined into f, then the frame for f will be partially skipped,
resulting in the following slice:
pcs = []uintptr{pc_in_f, runtime.skipPleaseUseCallersFrames+1, ...}
We store the skip PC in pcs[1] instead of pcs[0] so that `pcs[i:]` will
truncate the captured stack trace rather than grow it for all i.
Updates #19348.
Change-Id: I1c56f89ac48c29e6f52a5d085567c6d77d499cf1
Reviewed-on: https://go-review.googlesource.com/37854
Run-TryBot: David Lazar <lazard@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-03-06 12:48:36 -07:00
|
|
|
var expectedFrames [][]string = [][]string{
|
|
|
|
0: {"runtime.Callers", "main.testCallers", "main.main"},
|
|
|
|
1: {"main.testCallers", "main.main"},
|
|
|
|
2: {"main.testCallers", "runtime.skipPleaseUseCallersFrames", "main.main"},
|
|
|
|
3: {"main.testCallers", "runtime.skipPleaseUseCallersFrames", "main.main"},
|
|
|
|
4: {"main.testCallers", "runtime.skipPleaseUseCallersFrames", "main.main"},
|
|
|
|
5: {"main.main"},
|
|
|
|
}
|
|
|
|
|
2017-03-07 19:14:12 -07:00
|
|
|
var allFrames = []string{"runtime.Callers", "main.h", "main.g", "main.f", "main.testCallersFrames", "main.main"}
|
|
|
|
|
runtime: handle inlined calls in runtime.Callers
The `skip` argument passed to runtime.Caller and runtime.Callers should
be interpreted as the number of logical calls to skip (rather than the
number of physical stack frames to skip). This changes runtime.Callers
to skip inlined calls in addition to physical stack frames.
The result value of runtime.Callers is a slice of program counters
([]uintptr) representing physical stack frames. If the `skip` parameter
to runtime.Callers skips part-way into a physical frame, there is no
convenient way to encode that in the resulting slice. To avoid changing
the API in an incompatible way, our solution is to store the number of
skipped logical calls of the first frame in the _second_ uintptr
returned by runtime.Callers. Since this number is a small integer, we
encode it as a valid PC value into a small symbol called:
runtime.skipPleaseUseCallersFrames
For example, if f() calls g(), g() calls `runtime.Callers(2, pcs)`, and
g() is inlined into f, then the frame for f will be partially skipped,
resulting in the following slice:
pcs = []uintptr{pc_in_f, runtime.skipPleaseUseCallersFrames+1, ...}
We store the skip PC in pcs[1] instead of pcs[0] so that `pcs[i:]` will
truncate the captured stack trace rather than grow it for all i.
Updates #19348.
Change-Id: I1c56f89ac48c29e6f52a5d085567c6d77d499cf1
Reviewed-on: https://go-review.googlesource.com/37854
Run-TryBot: David Lazar <lazard@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-03-06 12:48:36 -07:00
|
|
|
func same(xs, ys []string) bool {
|
|
|
|
if len(xs) != len(ys) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range xs {
|
|
|
|
if xs[i] != ys[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
for i := 0; i <= 5; i++ {
|
|
|
|
frames := testCallers(i)
|
|
|
|
expected := expectedFrames[i]
|
|
|
|
if !same(frames, expected) {
|
|
|
|
log.Fatalf("testCallers(%d):\n got %v\n want %v", i, frames, expected)
|
|
|
|
}
|
2017-03-07 19:14:12 -07:00
|
|
|
|
|
|
|
frames = testCallersFrames(i)
|
|
|
|
expected = allFrames[i:]
|
|
|
|
if !same(frames, expected) {
|
|
|
|
log.Fatalf("testCallersFrames(%d):\n got %v\n want %v", i, frames, expected)
|
|
|
|
}
|
runtime: handle inlined calls in runtime.Callers
The `skip` argument passed to runtime.Caller and runtime.Callers should
be interpreted as the number of logical calls to skip (rather than the
number of physical stack frames to skip). This changes runtime.Callers
to skip inlined calls in addition to physical stack frames.
The result value of runtime.Callers is a slice of program counters
([]uintptr) representing physical stack frames. If the `skip` parameter
to runtime.Callers skips part-way into a physical frame, there is no
convenient way to encode that in the resulting slice. To avoid changing
the API in an incompatible way, our solution is to store the number of
skipped logical calls of the first frame in the _second_ uintptr
returned by runtime.Callers. Since this number is a small integer, we
encode it as a valid PC value into a small symbol called:
runtime.skipPleaseUseCallersFrames
For example, if f() calls g(), g() calls `runtime.Callers(2, pcs)`, and
g() is inlined into f, then the frame for f will be partially skipped,
resulting in the following slice:
pcs = []uintptr{pc_in_f, runtime.skipPleaseUseCallersFrames+1, ...}
We store the skip PC in pcs[1] instead of pcs[0] so that `pcs[i:]` will
truncate the captured stack trace rather than grow it for all i.
Updates #19348.
Change-Id: I1c56f89ac48c29e6f52a5d085567c6d77d499cf1
Reviewed-on: https://go-review.googlesource.com/37854
Run-TryBot: David Lazar <lazard@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
2017-03-06 12:48:36 -07:00
|
|
|
}
|
|
|
|
}
|