Debian Codegraph @

libgo/go/runtime/callers_test.go

raw
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime_test
6
7 import (
8 "reflect"
9 "runtime"
10 "strings"
11 "testing"
12 )
13
14 func f1(pan bool) []uintptr {
15 return f2(pan) // line 15
16 }
17
18 func f2(pan bool) []uintptr {
19 return f3(pan) // line 19
20 }
21
22 func f3(pan bool) []uintptr {
23 if pan {
24 panic("f3") // line 24
25 }
26 ret := make([]uintptr, 20)
27 return ret[:runtime.Callers(0, ret)] // line 27
28 }
29
30 func testCallers(t *testing.T, pcs []uintptr, pan bool) {
31 m := make(map[string]int, len(pcs))
32 frames := runtime.CallersFrames(pcs)
33 for {
34 frame, more := frames.Next()
35 if frame.Function != "" {
36 m[frame.Function] = frame.Line
37 }
38 if !more {
39 break
40 }
41 }
42
43 var seen []string
44 for k := range m {
45 seen = append(seen, k)
46 }
47 t.Logf("functions seen: %s", strings.Join(seen, " "))
48
49 var f3Line int
50 if pan {
51 f3Line = 24
52 } else {
53 f3Line = 27
54 }
55 want := []struct {
56 name string
57 line int
58 }{
59 {"f1", 15},
60 {"f2", 19},
61 {"f3", f3Line},
62 }
63 for _, w := range want {
64 if got := m["runtime_test."+w.name]; got != w.line {
65 t.Errorf("%s is line %d, want %d", w.name, got, w.line)
66 }
67 }
68 }
69
70 func testCallersEqual(t *testing.T, pcs []uintptr, want []string, ignore map[string]struct{}) {
71 t.Helper()
72
73 got := make([]string, 0, len(want))
74
75 frames := runtime.CallersFrames(pcs)
76 for {
77 frame, more := frames.Next()
78 if !more || len(got) >= len(want) {
79 break
80 }
81 if _, ok := ignore[frame.Function]; !ok {
82 got = append(got, frame.Function)
83 }
84 }
85 if !reflect.DeepEqual(want, got) {
86 t.Fatalf("wanted %v, got %v", want, got)
87 }
88 }
89
90 func TestCallers(t *testing.T) {
91 testCallers(t, f1(false), false)
92 }
93
94 func TestCallersPanic(t *testing.T) {
95 // Make sure we don't have any extra frames on the stack (due to
96 // open-coded defer processing)
97 want := []string{"runtime.Callers", "runtime_test.TestCallersPanic.func1",
98 "runtime.gopanic", "runtime_test.f3", "runtime_test.f2", "runtime_test.f1",
99 "runtime_test.TestCallersPanic"}
100 if runtime.Compiler == "gccgo" {
101 want = []string{"runtime.Callers", "runtime_test.TestCallersPanic..func1",
102 "runtime.gopanic", "runtime_test.f3", "runtime_test.f2", "runtime_test.f1",
103 "runtime_test.TestCallersPanic"}
104 }
105
106 defer func() {
107 if r := recover(); r == nil {
108 t.Fatal("did not panic")
109 }
110 pcs := make([]uintptr, 20)
111 pcs = pcs[:runtime.Callers(0, pcs)]
112 testCallers(t, pcs, true)
113 testCallersEqual(t, pcs, want, nil)
114 }()
115 f1(true)
116 }
117
118 func TestCallersDoublePanic(t *testing.T) {
119 // Make sure we don't have any extra frames on the stack (due to
120 // open-coded defer processing)
121 want := []string{"runtime.Callers", "runtime_test.TestCallersDoublePanic.func1.1",
122 "runtime.gopanic", "runtime_test.TestCallersDoublePanic.func1", "runtime.gopanic", "runtime_test.TestCallersDoublePanic"}
123 if runtime.Compiler == "gccgo" {
124 want = []string{"runtime.Callers", "runtime_test.TestCallersDoublePanic..func2",
125 "runtime.gopanic", "runtime_test.TestCallersDoublePanic..func1", "runtime.gopanic", "runtime_test.TestCallersDoublePanic"}
126 }
127
128 defer func() {
129 defer func() {
130 pcs := make([]uintptr, 20)
131 pcs = pcs[:runtime.Callers(0, pcs)]
132 if recover() == nil {
133 t.Fatal("did not panic")
134 }
135 testCallersEqual(t, pcs, want, nil)
136 }()
137 if recover() == nil {
138 t.Fatal("did not panic")
139 }
140 panic(2)
141 }()
142 panic(1)
143 }
144
145 // Test that a defer after a successful recovery looks like it is called directly
146 // from the function with the defers.
147 func TestCallersAfterRecovery(t *testing.T) {
148 want := []string{"runtime.Callers", "runtime_test.TestCallersAfterRecovery.func1", "runtime_test.TestCallersAfterRecovery"}
149 if runtime.Compiler == "gccgo" {
150 want = []string{"runtime.Callers", "runtime_test.TestCallersAfterRecovery..func1", "runtime_test.TestCallersAfterRecovery"}
151 }
152
153 defer func() {
154 pcs := make([]uintptr, 20)
155 pcs = pcs[:runtime.Callers(0, pcs)]
156 testCallersEqual(t, pcs, want, nil)
157 }()
158 defer func() {
159 if recover() == nil {
160 t.Fatal("did not recover from panic")
161 }
162 }()
163 panic(1)
164 }
165
166 func TestCallersAbortedPanic(t *testing.T) {
167 want := []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic.func2", "runtime_test.TestCallersAbortedPanic"}
168 if runtime.Compiler == "gccgo" {
169 want = []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic..func2", "runtime_test.TestCallersAbortedPanic"}
170 }
171
172 defer func() {
173 r := recover()
174 if r != nil {
175 t.Fatalf("should be no panic remaining to recover")
176 }
177 }()
178
179 defer func() {
180 // panic2 was aborted/replaced by panic1, so when panic2 was
181 // recovered, there is no remaining panic on the stack.
182 pcs := make([]uintptr, 20)
183 pcs = pcs[:runtime.Callers(0, pcs)]
184 testCallersEqual(t, pcs, want, nil)
185 }()
186 defer func() {
187 r := recover()
188 if r != "panic2" {
189 t.Fatalf("got %v, wanted %v", r, "panic2")
190 }
191 }()
192 defer func() {
193 // panic2 aborts/replaces panic1, because it is a recursive panic
194 // that is not recovered within the defer function called by
195 // panic1 panicking sequence
196 panic("panic2")
197 }()
198 panic("panic1")
199 }
200
201 func TestCallersAbortedPanic2(t *testing.T) {
202 want := []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic2.func2", "runtime_test.TestCallersAbortedPanic2"}
203 if runtime.Compiler == "gccgo" {
204 want = []string{"runtime.Callers", "runtime_test.TestCallersAbortedPanic2..func2", "runtime_test.TestCallersAbortedPanic2"}
205 }
206 defer func() {
207 r := recover()
208 if r != nil {
209 t.Fatalf("should be no panic remaining to recover")
210 }
211 }()
212 defer func() {
213 pcs := make([]uintptr, 20)
214 pcs = pcs[:runtime.Callers(0, pcs)]
215 testCallersEqual(t, pcs, want, nil)
216 }()
217 func() {
218 defer func() {
219 r := recover()
220 if r != "panic2" {
221 t.Fatalf("got %v, wanted %v", r, "panic2")
222 }
223 }()
224 func() {
225 defer func() {
226 // Again, panic2 aborts/replaces panic1
227 panic("panic2")
228 }()
229 panic("panic1")
230 }()
231 }()
232 }
233
234 func TestCallersNilPointerPanic(t *testing.T) {
235 // Make sure we don't have any extra frames on the stack (due to
236 // open-coded defer processing)
237 want := []string{"runtime.Callers", "runtime_test.TestCallersNilPointerPanic.func1",
238 "runtime.gopanic", "runtime.panicmem", "runtime.sigpanic",
239 "runtime_test.TestCallersNilPointerPanic"}
240 ign := make(map[string]struct{})
241 if runtime.Compiler == "gccgo" {
242 // The expected results of gollvm and gccgo are slightly different, the result
243 // of gccgo does not contain tRunner, and the result of gollvm does not contain
244 // sigpanic. Make these two elementes optional to pass both of gollvm and gccgo.
245 want = []string{"runtime.Callers", "runtime_test.TestCallersNilPointerPanic..func1",
246 "runtime.gopanic", "runtime.panicmem",
247 "runtime_test.TestCallersNilPointerPanic"}
248 ign["runtime.sigpanic"] = struct{}{}
249 ign["testing.tRunner"] = struct{}{}
250 }
251
252 defer func() {
253 if r := recover(); r == nil {
254 t.Fatal("did not panic")
255 }
256 pcs := make([]uintptr, 20)
257 pcs = pcs[:runtime.Callers(0, pcs)]
258 testCallersEqual(t, pcs, want, ign)
259 }()
260 var p *int
261 if *p == 3 {
262 t.Fatal("did not see nil pointer panic")
263 }
264 }
265
266 func TestCallersDivZeroPanic(t *testing.T) {
267 // Make sure we don't have any extra frames on the stack (due to
268 // open-coded defer processing)
269 want := []string{"runtime.Callers", "runtime_test.TestCallersDivZeroPanic.func1",
270 "runtime.gopanic", "runtime.panicdivide",
271 "runtime_test.TestCallersDivZeroPanic"}
272 if runtime.Compiler == "gccgo" {
273 want = []string{"runtime.Callers", "runtime_test.TestCallersDivZeroPanic..func1",
274 "runtime.gopanic", "runtime.panicdivide",
275 "runtime_test.TestCallersDivZeroPanic"}
276 }
277
278 defer func() {
279 if r := recover(); r == nil {
280 t.Fatal("did not panic")
281 }
282 pcs := make([]uintptr, 20)
283 pcs = pcs[:runtime.Callers(0, pcs)]
284 testCallersEqual(t, pcs, want, nil)
285 }()
286 var n int
287 if 5/n == 1 {
288 t.Fatal("did not see divide-by-sizer panic")
289 }
290 }
291
292 func TestCallersDeferNilFuncPanic(t *testing.T) {
293 // Make sure we don't have any extra frames on the stack. We cut off the check
294 // at runtime.sigpanic, because non-open-coded defers (which may be used in
295 // non-opt or race checker mode) include an extra 'deferreturn' frame (which is
296 // where the nil pointer deref happens).
297 state := 1
298 want := []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanic.func1",
299 "runtime.gopanic", "runtime.panicmem", "runtime.sigpanic"}
300 if runtime.Compiler == "gccgo" {
301 want = []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanic..func1",
302 "runtime.gopanic", "runtime.panicmem", "runtime.sigpanic"}
303 }
304
305 defer func() {
306 if r := recover(); r == nil {
307 t.Fatal("did not panic")
308 }
309 pcs := make([]uintptr, 20)
310 pcs = pcs[:runtime.Callers(0, pcs)]
311 testCallersEqual(t, pcs, want, nil)
312 if state == 1 {
313 t.Fatal("nil defer func panicked at defer time rather than function exit time")
314 }
315
316 }()
317 var f func()
318 defer f()
319 // Use the value of 'state' to make sure nil defer func f causes panic at
320 // function exit, rather than at the defer statement.
321 state = 2
322 }
323
324 // Same test, but forcing non-open-coded defer by putting the defer in a loop. See
325 // issue #36050
326 func TestCallersDeferNilFuncPanicWithLoop(t *testing.T) {
327 state := 1
328 want := []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanicWithLoop.func1",
329 "runtime.gopanic", "runtime.panicmem", "runtime.sigpanic", "runtime.deferreturn", "runtime_test.TestCallersDeferNilFuncPanicWithLoop"}
330 if runtime.Compiler == "gccgo" {
331 want = []string{"runtime.Callers", "runtime_test.TestCallersDeferNilFuncPanicWithLoop..func1",
332 "runtime.gopanic", "runtime.panicmem", "runtime.sigpanic", "runtime_test.TestCallersDeferNilFuncPanicWithLoop"}
333 }
334
335 defer func() {
336 if r := recover(); r == nil {
337 t.Fatal("did not panic")
338 }
339 pcs := make([]uintptr, 20)
340 pcs = pcs[:runtime.Callers(0, pcs)]
341 testCallersEqual(t, pcs, want, nil)
342 if state == 1 {
343 t.Fatal("nil defer func panicked at defer time rather than function exit time")
344 }
345
346 }()
347
348 for i := 0; i < 1; i++ {
349 var f func()
350 defer f()
351 }
352 // Use the value of 'state' to make sure nil defer func f causes panic at
353 // function exit, rather than at the defer statement.
354 state = 2
355 }