-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
runtime: skip logical frames in runtime.Caller
This rewrites runtime.Caller in terms of stackExpander, which already handles inlined frames and partially skipped frames. This also has the effect of making runtime.Caller understand cgo frames if there is a cgo symbolizer. Updates #19348. Change-Id: Icdf4df921aab5aa394d4d92e3becc4dd169c9a6e Reviewed-on: https://go-review.googlesource.com/40270 Run-TryBot: David Lazar <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
- Loading branch information
1 parent
5e15497
commit 0ea120a
Showing
2 changed files
with
92 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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 ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
type frame struct { | ||
pc uintptr | ||
file string | ||
line int | ||
ok bool | ||
} | ||
|
||
var ( | ||
skip int | ||
globalFrame frame | ||
) | ||
|
||
func f() { | ||
g() // line 27 | ||
} | ||
|
||
func g() { | ||
h() // line 31 | ||
} | ||
|
||
func h() { | ||
x := &globalFrame | ||
x.pc, x.file, x.line, x.ok = runtime.Caller(skip) // line 36 | ||
} | ||
|
||
//go:noinline | ||
func testCaller(skp int) frame { | ||
skip = skp | ||
f() // line 42 | ||
frame := globalFrame | ||
if !frame.ok { | ||
panic(fmt.Sprintf("skip=%d runtime.Caller failed", skp)) | ||
} | ||
return frame | ||
} | ||
|
||
type wantFrame struct { | ||
funcName string | ||
line int | ||
} | ||
|
||
// -1 means don't care | ||
var expected = []wantFrame{ | ||
0: {"main.testCaller", 36}, | ||
1: {"main.testCaller", 31}, | ||
2: {"main.testCaller", 27}, | ||
3: {"main.testCaller", 42}, | ||
4: {"main.main", 68}, | ||
5: {"runtime.main", -1}, | ||
6: {"runtime.goexit", -1}, | ||
} | ||
|
||
func main() { | ||
for i := 0; i <= 6; i++ { | ||
frame := testCaller(i) // line 68 | ||
fn := runtime.FuncForPC(frame.pc) | ||
if expected[i].line >= 0 && frame.line != expected[i].line { | ||
panic(fmt.Sprintf("skip=%d expected line %d, got line %d", i, expected[i].line, frame.line)) | ||
} | ||
if fn.Name() != expected[i].funcName { | ||
panic(fmt.Sprintf("skip=%d expected function %s, got %s", i, expected[i].funcName, fn.Name())) | ||
} | ||
} | ||
} |