Skip to content

Commit 354fa9a

Browse files
committed
runtime: simplify stack walk in panicwrap
panicwrap currently uses runtime.Callers and runtime.CallersFrames to find the name of its caller. Simplify this by using getcallerpc. This will be important for #16723, since to fix that we're going to make CallersFrames skip the wrapper method, which is exactly what panicwrap needs to see. Change-Id: Icb0776d399966e31595f3ee44f980290827e32a6 Reviewed-on: https://go-review.googlesource.com/45411 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 229aaac commit 354fa9a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/runtime/error.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,31 @@ func printany(i interface{}) {
126126
//go:linkname stringsIndexByte strings.IndexByte
127127
func stringsIndexByte(s string, c byte) int
128128

129-
// called from generated code
129+
// panicwrap generates a panic for a call to a wrapped value method
130+
// with a nil pointer receiver.
131+
//
132+
// It is called from the generated wrapper code.
130133
func panicwrap() {
131-
pc := make([]uintptr, 1)
132-
n := Callers(2, pc)
133-
if n == 0 {
134-
throw("panicwrap: Callers failed")
135-
}
136-
frames := CallersFrames(pc)
137-
frame, _ := frames.Next()
138-
name := frame.Function
134+
pc := getcallerpc()
135+
name := funcname(findfunc(pc))
139136
// name is something like "main.(*T).F".
140137
// We want to extract pkg ("main"), typ ("T"), and meth ("F").
141138
// Do it by finding the parens.
142139
i := stringsIndexByte(name, '(')
143140
if i < 0 {
144-
throw("panicwrap: no ( in " + frame.Function)
141+
throw("panicwrap: no ( in " + name)
145142
}
146143
pkg := name[:i-1]
147144
if i+2 >= len(name) || name[i-1:i+2] != ".(*" {
148-
throw("panicwrap: unexpected string after package name: " + frame.Function)
145+
throw("panicwrap: unexpected string after package name: " + name)
149146
}
150147
name = name[i+2:]
151148
i = stringsIndexByte(name, ')')
152149
if i < 0 {
153-
throw("panicwrap: no ) in " + frame.Function)
150+
throw("panicwrap: no ) in " + name)
154151
}
155152
if i+2 >= len(name) || name[i:i+2] != ")." {
156-
throw("panicwrap: unexpected string after type name: " + frame.Function)
153+
throw("panicwrap: unexpected string after type name: " + name)
157154
}
158155
typ := name[:i]
159156
meth := name[i+2:]

0 commit comments

Comments
 (0)