Debian Codegraph @

src/runtime/traceback_test.go

raw
1 // Copyright 2021 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 "bytes"
9 "fmt"
10 "internal/abi"
11 "internal/testenv"
12 "regexp"
13 "runtime"
14 "runtime/debug"
15 "strconv"
16 "strings"
17 "sync"
18 "testing"
19 _ "unsafe"
20 )
21
22 // Test traceback printing of inlined frames.
23 func TestTracebackInlined(t *testing.T) {
24 testenv.SkipIfOptimizationOff(t) // This test requires inlining
25 check := func(t *testing.T, r *ttiResult, funcs ...string) {
26 t.Helper()
27
28 // Check the printed traceback.
29 frames := parseTraceback1(t, r.printed).frames
30 t.Log(r.printed)
31 // Find ttiLeaf
32 for len(frames) > 0 && frames[0].funcName != "runtime_test.ttiLeaf" {
33 frames = frames[1:]
34 }
35 if len(frames) == 0 {
36 t.Errorf("missing runtime_test.ttiLeaf")
37 return
38 }
39 frames = frames[1:]
40 // Check the function sequence.
41 for i, want := range funcs {
42 got := "<end>"
43 if i < len(frames) {
44 got = frames[i].funcName
45 if strings.HasSuffix(want, ")") {
46 got += "(" + frames[i].args + ")"
47 }
48 }
49 if got != want {
50 t.Errorf("got %s, want %s", got, want)
51 return
52 }
53 }
54 }
55
56 t.Run("simple", func(t *testing.T) {
57 // Check a simple case of inlining
58 r := ttiSimple1()
59 check(t, r, "runtime_test.ttiSimple3(...)", "runtime_test.ttiSimple2(...)", "runtime_test.ttiSimple1()")
60 })
61
62 t.Run("sigpanic", func(t *testing.T) {
63 // Check that sigpanic from an inlined function prints correctly
64 r := ttiSigpanic1()
65 check(t, r, "runtime_test.ttiSigpanic1.func1()", "panic", "runtime_test.ttiSigpanic3(...)", "runtime_test.ttiSigpanic2(...)", "runtime_test.ttiSigpanic1()")
66 })
67
68 t.Run("wrapper", func(t *testing.T) {
69 // Check that a method inlined into a wrapper prints correctly
70 r := ttiWrapper1()
71 check(t, r, "runtime_test.ttiWrapper.m1(...)", "runtime_test.ttiWrapper1()")
72 })
73
74 t.Run("excluded", func(t *testing.T) {
75 // Check that when F -> G is inlined and F is excluded from stack
76 // traces, G still appears.
77 r := ttiExcluded1()
78 check(t, r, "runtime_test.ttiExcluded3(...)", "runtime_test.ttiExcluded1()")
79 })
80 }
81
82 type ttiResult struct {
83 printed string
84 }
85
86 //go:noinline
87 func ttiLeaf() *ttiResult {
88 // Get a printed stack trace.
89 printed := string(debug.Stack())
90 return &ttiResult{printed}
91 }
92
93 //go:noinline
94 func ttiSimple1() *ttiResult {
95 return ttiSimple2()
96 }
97 func ttiSimple2() *ttiResult {
98 return ttiSimple3()
99 }
100 func ttiSimple3() *ttiResult {
101 return ttiLeaf()
102 }
103
104 //go:noinline
105 func ttiSigpanic1() (res *ttiResult) {
106 defer func() {
107 res = ttiLeaf()
108 recover()
109 }()
110 ttiSigpanic2()
111 // without condition below the inliner might decide to de-prioritize
112 // the callsite above (since it would be on an "always leads to panic"
113 // path).
114 if alwaysTrue {
115 panic("did not panic")
116 }
117 return nil
118 }
119 func ttiSigpanic2() {
120 ttiSigpanic3()
121 }
122 func ttiSigpanic3() {
123 var p *int
124 *p = 3
125 }
126
127 var alwaysTrue = true
128
129 //go:noinline
130 func ttiWrapper1() *ttiResult {
131 var w ttiWrapper
132 m := (*ttiWrapper).m1
133 return m(&w)
134 }
135
136 type ttiWrapper struct{}
137
138 func (w ttiWrapper) m1() *ttiResult {
139 return ttiLeaf()
140 }
141
142 //go:noinline
143 func ttiExcluded1() *ttiResult {
144 return ttiExcluded2()
145 }
146
147 // ttiExcluded2 should be excluded from tracebacks. There are
148 // various ways this could come up. Linking it to a "runtime." name is
149 // rather synthetic, but it's easy and reliable. See issue #42754 for
150 // one way this happened in real code.
151 //
152 //go:linkname ttiExcluded2 runtime.ttiExcluded2
153 //go:noinline
154 func ttiExcluded2() *ttiResult {
155 return ttiExcluded3()
156 }
157 func ttiExcluded3() *ttiResult {
158 return ttiLeaf()
159 }
160
161 var testTracebackArgsBuf [1000]byte
162
163 func TestTracebackElision(t *testing.T) {
164 // Test printing exactly the maximum number of frames to make sure we don't
165 // print any "elided" message, eliding exactly 1 so we have to pick back up
166 // in the paused physical frame, and eliding 10 so we have to advance the
167 // physical frame forward.
168 for _, elided := range []int{0, 1, 10} {
169 t.Run(fmt.Sprintf("elided=%d", elided), func(t *testing.T) {
170 n := elided + runtime.TracebackInnerFrames + runtime.TracebackOuterFrames
171
172 // Start a new goroutine so we have control over the whole stack.
173 stackChan := make(chan string)
174 go tteStack(n, stackChan)
175 stack := <-stackChan
176 tb := parseTraceback1(t, stack)
177
178 // Check the traceback.
179 i := 0
180 for i < n {
181 if len(tb.frames) == 0 {
182 t.Errorf("traceback ended early")
183 break
184 }
185 fr := tb.frames[0]
186 if i == runtime.TracebackInnerFrames && elided > 0 {
187 // This should be an "elided" frame.
188 if fr.elided != elided {
189 t.Errorf("want %d frames elided", elided)
190 break
191 }
192 i += fr.elided
193 } else {
194 want := fmt.Sprintf("runtime_test.tte%d", (i+1)%5)
195 if i == 0 {
196 want = "runtime/debug.Stack"
197 } else if i == n-1 {
198 want = "runtime_test.tteStack"
199 }
200 if fr.funcName != want {
201 t.Errorf("want %s, got %s", want, fr.funcName)
202 break
203 }
204 i++
205 }
206 tb.frames = tb.frames[1:]
207 }
208 if !t.Failed() && len(tb.frames) > 0 {
209 t.Errorf("got %d more frames than expected", len(tb.frames))
210 }
211 if t.Failed() {
212 t.Logf("traceback diverged at frame %d", i)
213 off := len(stack)
214 if len(tb.frames) > 0 {
215 off = tb.frames[0].off
216 }
217 t.Logf("traceback before error:\n%s", stack[:off])
218 t.Logf("traceback after error:\n%s", stack[off:])
219 }
220 })
221 }
222 }
223
224 // tteStack creates a stack of n logical frames and sends the traceback to
225 // stack. It cycles through 5 logical frames per physical frame to make it
226 // unlikely that any part of the traceback will end on a physical boundary.
227 func tteStack(n int, stack chan<- string) {
228 n-- // Account for this frame
229 // This is basically a Duff's device for starting the inline stack in the
230 // right place so we wind up at tteN when n%5=N.
231 switch n % 5 {
232 case 0:
233 stack <- tte0(n)
234 case 1:
235 stack <- tte1(n)
236 case 2:
237 stack <- tte2(n)
238 case 3:
239 stack <- tte3(n)
240 case 4:
241 stack <- tte4(n)
242 default:
243 panic("unreachable")
244 }
245 }
246 func tte0(n int) string {
247 return tte4(n - 1)
248 }
249 func tte1(n int) string {
250 return tte0(n - 1)
251 }
252 func tte2(n int) string {
253 // tte2 opens n%5 == 2 frames. It's also the base case of the recursion,
254 // since we can open no fewer than two frames to call debug.Stack().
255 if n < 2 {
256 panic("bad n")
257 }
258 if n == 2 {
259 return string(debug.Stack())
260 }
261 return tte1(n - 1)
262 }
263 func tte3(n int) string {
264 return tte2(n - 1)
265 }
266 func tte4(n int) string {
267 return tte3(n - 1)
268 }
269
270 func TestTracebackArgs(t *testing.T) {
271 if *flagQuick {
272 t.Skip("-quick")
273 }
274 optimized := !testenv.OptimizationOff()
275 abiSel := func(x, y string) string {
276 // select expected output based on ABI
277 // In noopt build we always spill arguments so the output is the same as stack ABI.
278 if optimized && abi.IntArgRegs > 0 {
279 return x
280 }
281 return y
282 }
283
284 tests := []struct {
285 fn func() int
286 expect string
287 }{
288 // simple ints
289 {
290 func() int { return testTracebackArgs1(1, 2, 3, 4, 5) },
291 "testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5)",
292 },
293 // some aggregates
294 {
295 func() int {
296 return testTracebackArgs2(false, struct {
297 a, b, c int
298 x [2]int
299 }{1, 2, 3, [2]int{4, 5}}, [0]int{}, [3]byte{6, 7, 8})
300 },
301 "testTracebackArgs2(0x0, {0x1, 0x2, 0x3, {0x4, 0x5}}, {}, {0x6, 0x7, 0x8})",
302 },
303 {
304 func() int { return testTracebackArgs3([3]byte{1, 2, 3}, 4, 5, 6, [3]byte{7, 8, 9}) },
305 "testTracebackArgs3({0x1, 0x2, 0x3}, 0x4, 0x5, 0x6, {0x7, 0x8, 0x9})",
306 },
307 // too deeply nested type
308 {
309 func() int { return testTracebackArgs4(false, [1][1][1][1][1][1][1][1][1][1]int{}) },
310 "testTracebackArgs4(0x0, {{{{{...}}}}})",
311 },
312 // a lot of zero-sized type
313 {
314 func() int {
315 z := [0]int{}
316 return testTracebackArgs5(false, struct {
317 x int
318 y [0]int
319 z [2][0]int
320 }{1, z, [2][0]int{}}, z, z, z, z, z, z, z, z, z, z, z, z)
321 },
322 "testTracebackArgs5(0x0, {0x1, {}, {{}, {}}}, {}, {}, {}, {}, {}, ...)",
323 },
324
325 // edge cases for ...
326 // no ... for 10 args
327 {
328 func() int { return testTracebackArgs6a(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) },
329 "testTracebackArgs6a(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa)",
330 },
331 // has ... for 11 args
332 {
333 func() int { return testTracebackArgs6b(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) },
334 "testTracebackArgs6b(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...)",
335 },
336 // no ... for aggregates with 10 words
337 {
338 func() int { return testTracebackArgs7a([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) },
339 "testTracebackArgs7a({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa})",
340 },
341 // has ... for aggregates with 11 words
342 {
343 func() int { return testTracebackArgs7b([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}) },
344 "testTracebackArgs7b({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...})",
345 },
346 // no ... for aggregates, but with more args
347 {
348 func() int { return testTracebackArgs7c([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 11) },
349 "testTracebackArgs7c({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa}, ...)",
350 },
351 // has ... for aggregates and also for more args
352 {
353 func() int { return testTracebackArgs7d([11]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 12) },
354 "testTracebackArgs7d({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...}, ...)",
355 },
356 // nested aggregates, no ...
357 {
358 func() int { return testTracebackArgs8a(testArgsType8a{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}}) },
359 "testTracebackArgs8a({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa}})",
360 },
361 // nested aggregates, ... in inner but not outer
362 {
363 func() int { return testTracebackArgs8b(testArgsType8b{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}}) },
364 "testTracebackArgs8b({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa, ...}})",
365 },
366 // nested aggregates, ... in outer but not inner
367 {
368 func() int { return testTracebackArgs8c(testArgsType8c{1, 2, 3, 4, 5, 6, 7, 8, [2]int{9, 10}, 11}) },
369 "testTracebackArgs8c({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa}, ...})",
370 },
371 // nested aggregates, ... in both inner and outer
372 {
373 func() int { return testTracebackArgs8d(testArgsType8d{1, 2, 3, 4, 5, 6, 7, 8, [3]int{9, 10, 11}, 12}) },
374 "testTracebackArgs8d({0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, {0x9, 0xa, ...}, ...})",
375 },
376
377 // Register argument liveness.
378 // 1, 3 are used and live, 2, 4 are dead (in register ABI).
379 // Address-taken (7) and stack ({5, 6}) args are always live.
380 {
381 func() int {
382 poisonStack() // poison arg area to make output deterministic
383 return testTracebackArgs9(1, 2, 3, 4, [2]int{5, 6}, 7)
384 },
385 abiSel(
386 "testTracebackArgs9(0x1, 0xffffffff?, 0x3, 0xff?, {0x5, 0x6}, 0x7)",
387 "testTracebackArgs9(0x1, 0x2, 0x3, 0x4, {0x5, 0x6}, 0x7)"),
388 },
389 // No live.
390 // (Note: this assume at least 5 int registers if register ABI is used.)
391 {
392 func() int {
393 poisonStack() // poison arg area to make output deterministic
394 return testTracebackArgs10(1, 2, 3, 4, 5)
395 },
396 abiSel(
397 "testTracebackArgs10(0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?, 0xffffffff?)",
398 "testTracebackArgs10(0x1, 0x2, 0x3, 0x4, 0x5)"),
399 },
400 // Conditional spills.
401 // Spill in conditional, not executed.
402 {
403 func() int {
404 poisonStack() // poison arg area to make output deterministic
405 return testTracebackArgs11a(1, 2, 3)
406 },
407 abiSel(
408 "testTracebackArgs11a(0xffffffff?, 0xffffffff?, 0xffffffff?)",
409 "testTracebackArgs11a(0x1, 0x2, 0x3)"),
410 },
411 // 2 spills in conditional, not executed; 3 spills in conditional, executed, but not statically known.
412 // So print 0x3?.
413 {
414 func() int {
415 poisonStack() // poison arg area to make output deterministic
416 return testTracebackArgs11b(1, 2, 3, 4)
417 },
418 abiSel(
419 "testTracebackArgs11b(0xffffffff?, 0xffffffff?, 0x3?, 0x4)",
420 "testTracebackArgs11b(0x1, 0x2, 0x3, 0x4)"),
421 },
422 // Make sure spilled slice data pointers are spilled to the right location
423 // to ensure we see it listed without a ?.
424 // See issue 64414.
425 {
426 func() int {
427 poisonStack()
428 return testTracebackArgsSlice(testTracebackArgsSliceBackingStore[:])
429 },
430 // Note: capacity of the slice might be junk, as it is not used.
431 fmt.Sprintf("testTracebackArgsSlice({%p, 0x2, ", &testTracebackArgsSliceBackingStore[0]),
432 },
433 }
434 for _, test := range tests {
435 n := test.fn()
436 got := testTracebackArgsBuf[:n]
437 if !bytes.Contains(got, []byte(test.expect)) {
438 t.Errorf("traceback does not contain expected string: want %q, got\n%s", test.expect, got)
439 }
440 }
441 }
442
443 //go:noinline
444 func testTracebackArgs1(a, b, c, d, e int) int {
445 n := runtime.Stack(testTracebackArgsBuf[:], false)
446 if a < 0 {
447 // use in-reg args to keep them alive
448 return a + b + c + d + e
449 }
450 return n
451 }
452
453 //go:noinline
454 func testTracebackArgs2(a bool, b struct {
455 a, b, c int
456 x [2]int
457 }, _ [0]int, d [3]byte) int {
458 n := runtime.Stack(testTracebackArgsBuf[:], false)
459 if a {
460 // use in-reg args to keep them alive
461 return b.a + b.b + b.c + b.x[0] + b.x[1] + int(d[0]) + int(d[1]) + int(d[2])
462 }
463 return n
464 }
465
466 //go:noinline
467 //go:registerparams
468 func testTracebackArgs3(x [3]byte, a, b, c int, y [3]byte) int {
469 n := runtime.Stack(testTracebackArgsBuf[:], false)
470 if a < 0 {
471 // use in-reg args to keep them alive
472 return int(x[0]) + int(x[1]) + int(x[2]) + a + b + c + int(y[0]) + int(y[1]) + int(y[2])
473 }
474 return n
475 }
476
477 //go:noinline
478 func testTracebackArgs4(a bool, x [1][1][1][1][1][1][1][1][1][1]int) int {
479 n := runtime.Stack(testTracebackArgsBuf[:], false)
480 if a {
481 panic(x) // use args to keep them alive
482 }
483 return n
484 }
485
486 //go:noinline
487 func testTracebackArgs5(a bool, x struct {
488 x int
489 y [0]int
490 z [2][0]int
491 }, _, _, _, _, _, _, _, _, _, _, _, _ [0]int) int {
492 n := runtime.Stack(testTracebackArgsBuf[:], false)
493 if a {
494 panic(x) // use args to keep them alive
495 }
496 return n
497 }
498
499 //go:noinline
500 func testTracebackArgs6a(a, b, c, d, e, f, g, h, i, j int) int {
501 n := runtime.Stack(testTracebackArgsBuf[:], false)
502 if a < 0 {
503 // use in-reg args to keep them alive
504 return a + b + c + d + e + f + g + h + i + j
505 }
506 return n
507 }
508
509 //go:noinline
510 func testTracebackArgs6b(a, b, c, d, e, f, g, h, i, j, k int) int {
511 n := runtime.Stack(testTracebackArgsBuf[:], false)
512 if a < 0 {
513 // use in-reg args to keep them alive
514 return a + b + c + d + e + f + g + h + i + j + k
515 }
516 return n
517 }
518
519 //go:noinline
520 func testTracebackArgs7a(a [10]int) int {
521 n := runtime.Stack(testTracebackArgsBuf[:], false)
522 if a[0] < 0 {
523 // use in-reg args to keep them alive
524 return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9]
525 }
526 return n
527 }
528
529 //go:noinline
530 func testTracebackArgs7b(a [11]int) int {
531 n := runtime.Stack(testTracebackArgsBuf[:], false)
532 if a[0] < 0 {
533 // use in-reg args to keep them alive
534 return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + a[10]
535 }
536 return n
537 }
538
539 //go:noinline
540 func testTracebackArgs7c(a [10]int, b int) int {
541 n := runtime.Stack(testTracebackArgsBuf[:], false)
542 if a[0] < 0 {
543 // use in-reg args to keep them alive
544 return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + b
545 }
546 return n
547 }
548
549 //go:noinline
550 func testTracebackArgs7d(a [11]int, b int) int {
551 n := runtime.Stack(testTracebackArgsBuf[:], false)
552 if a[0] < 0 {
553 // use in-reg args to keep them alive
554 return a[1] + a[2] + a[3] + a[4] + a[5] + a[6] + a[7] + a[8] + a[9] + a[10] + b
555 }
556 return n
557 }
558
559 type testArgsType8a struct {
560 a, b, c, d, e, f, g, h int
561 i [2]int
562 }
563 type testArgsType8b struct {
564 a, b, c, d, e, f, g, h int
565 i [3]int
566 }
567 type testArgsType8c struct {
568 a, b, c, d, e, f, g, h int
569 i [2]int
570 j int
571 }
572 type testArgsType8d struct {
573 a, b, c, d, e, f, g, h int
574 i [3]int
575 j int
576 }
577
578 //go:noinline
579 func testTracebackArgs8a(a testArgsType8a) int {
580 n := runtime.Stack(testTracebackArgsBuf[:], false)
581 if a.a < 0 {
582 // use in-reg args to keep them alive
583 return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1]
584 }
585 return n
586 }
587
588 //go:noinline
589 func testTracebackArgs8b(a testArgsType8b) int {
590 n := runtime.Stack(testTracebackArgsBuf[:], false)
591 if a.a < 0 {
592 // use in-reg args to keep them alive
593 return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1] + a.i[2]
594 }
595 return n
596 }
597
598 //go:noinline
599 func testTracebackArgs8c(a testArgsType8c) int {
600 n := runtime.Stack(testTracebackArgsBuf[:], false)
601 if a.a < 0 {
602 // use in-reg args to keep them alive
603 return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1] + a.j
604 }
605 return n
606 }
607
608 //go:noinline
609 func testTracebackArgs8d(a testArgsType8d) int {
610 n := runtime.Stack(testTracebackArgsBuf[:], false)
611 if a.a < 0 {
612 // use in-reg args to keep them alive
613 return a.b + a.c + a.d + a.e + a.f + a.g + a.h + a.i[0] + a.i[1] + a.i[2] + a.j
614 }
615 return n
616 }
617
618 // nosplit to avoid preemption or morestack spilling registers.
619 //
620 //go:nosplit
621 //go:noinline
622 func testTracebackArgs9(a int64, b int32, c int16, d int8, x [2]int, y int) int {
623 if a < 0 {
624 println(&y) // take address, make y live, even if no longer used at traceback
625 }
626 n := runtime.Stack(testTracebackArgsBuf[:], false)
627 if a < 0 {
628 // use half of in-reg args to keep them alive, the other half are dead
629 return int(a) + int(c)
630 }
631 return n
632 }
633
634 // nosplit to avoid preemption or morestack spilling registers.
635 //
636 //go:nosplit
637 //go:noinline
638 func testTracebackArgs10(a, b, c, d, e int32) int {
639 // no use of any args
640 return runtime.Stack(testTracebackArgsBuf[:], false)
641 }
642
643 // norace to avoid race instrumentation changing spill locations.
644 // nosplit to avoid preemption or morestack spilling registers.
645 //
646 //go:norace
647 //go:nosplit
648 //go:noinline
649 func testTracebackArgs11a(a, b, c int32) int {
650 if a < 0 {
651 println(a, b, c) // spill in a conditional, may not execute
652 }
653 if b < 0 {
654 return int(a + b + c)
655 }
656 return runtime.Stack(testTracebackArgsBuf[:], false)
657 }
658
659 // norace to avoid race instrumentation changing spill locations.
660 // nosplit to avoid preemption or morestack spilling registers.
661 //
662 //go:norace
663 //go:nosplit
664 //go:noinline
665 func testTracebackArgs11b(a, b, c, d int32) int {
666 var x int32
667 if a < 0 {
668 print() // spill b in a conditional
669 x = b
670 } else {
671 print() // spill c in a conditional
672 x = c
673 }
674 if d < 0 { // d is always needed
675 return int(x + d)
676 }
677 return runtime.Stack(testTracebackArgsBuf[:], false)
678 }
679
680 // norace to avoid race instrumentation changing spill locations.
681 // nosplit to avoid preemption or morestack spilling registers.
682 //
683 //go:norace
684 //go:nosplit
685 //go:noinline
686 func testTracebackArgsSlice(a []int) int {
687 n := runtime.Stack(testTracebackArgsBuf[:], false)
688 return a[1] + n
689 }
690
691 var testTracebackArgsSliceBackingStore [2]int
692
693 // Poison the arg area with deterministic values.
694 //
695 //go:noinline
696 func poisonStack() [20]int {
697 return [20]int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
698 }
699
700 func TestTracebackParentChildGoroutines(t *testing.T) {
701 parent := fmt.Sprintf("goroutine %d", runtime.Goid())
702 var wg sync.WaitGroup
703 wg.Add(1)
704 go func() {
705 defer wg.Done()
706 buf := make([]byte, 1<<10)
707 // We collect the stack only for this goroutine (by passing
708 // false to runtime.Stack). We expect to see the current
709 // goroutine ID, and the parent goroutine ID in a message like
710 // "created by ... in goroutine N".
711 stack := string(buf[:runtime.Stack(buf, false)])
712 child := fmt.Sprintf("goroutine %d", runtime.Goid())
713 if !strings.Contains(stack, parent) || !strings.Contains(stack, child) {
714 t.Errorf("did not see parent (%s) and child (%s) IDs in stack, got %s", parent, child, stack)
715 }
716 }()
717 wg.Wait()
718 }
719
720 type traceback struct {
721 frames []*tbFrame
722 createdBy *tbFrame // no args
723 }
724
725 type tbFrame struct {
726 funcName string
727 args string
728 inlined bool
729
730 // elided is set to the number of frames elided, and the other fields are
731 // set to the zero value.
732 elided int
733
734 off int // byte offset in the traceback text of this frame
735 }
736
737 // parseTraceback parses a printed traceback to make it easier for tests to
738 // check the result.
739 func parseTraceback(t *testing.T, tb string) []*traceback {
740 //lines := strings.Split(tb, "\n")
741 //nLines := len(lines)
742 off := 0
743 lineNo := 0
744 fatal := func(f string, args ...any) {
745 msg := fmt.Sprintf(f, args...)
746 t.Fatalf("%s (line %d):\n%s", msg, lineNo, tb)
747 }
748 parseFrame := func(funcName, args string) *tbFrame {
749 // Consume file/line/etc
750 if !strings.HasPrefix(tb, "\t") {
751 fatal("missing source line")
752 }
753 _, tb, _ = strings.Cut(tb, "\n")
754 lineNo++
755 inlined := args == "..."
756 return &tbFrame{funcName: funcName, args: args, inlined: inlined, off: off}
757 }
758 var elidedRe = regexp.MustCompile(`^\.\.\.([0-9]+) frames elided\.\.\.$`)
759 var tbs []*traceback
760 var cur *traceback
761 tbLen := len(tb)
762 for len(tb) > 0 {
763 var line string
764 off = tbLen - len(tb)
765 line, tb, _ = strings.Cut(tb, "\n")
766 lineNo++
767 switch {
768 case strings.HasPrefix(line, "goroutine "):
769 cur = &traceback{}
770 tbs = append(tbs, cur)
771 case line == "":
772 // Separator between goroutines
773 cur = nil
774 case line[0] == '\t':
775 fatal("unexpected indent")
776 case strings.HasPrefix(line, "created by "):
777 funcName := line[len("created by "):]
778 cur.createdBy = parseFrame(funcName, "")
779 case strings.HasSuffix(line, ")"):
780 line = line[:len(line)-1] // Trim trailing ")"
781 funcName, args, found := strings.Cut(line, "(")
782 if !found {
783 fatal("missing (")
784 }
785 frame := parseFrame(funcName, args)
786 cur.frames = append(cur.frames, frame)
787 case elidedRe.MatchString(line):
788 // "...N frames elided..."
789 nStr := elidedRe.FindStringSubmatch(line)
790 n, _ := strconv.Atoi(nStr[1])
791 frame := &tbFrame{elided: n}
792 cur.frames = append(cur.frames, frame)
793 }
794 }
795 return tbs
796 }
797
798 // parseTraceback1 is like parseTraceback, but expects tb to contain exactly one
799 // goroutine.
800 func parseTraceback1(t *testing.T, tb string) *traceback {
801 tbs := parseTraceback(t, tb)
802 if len(tbs) != 1 {
803 t.Fatalf("want 1 goroutine, got %d:\n%s", len(tbs), tb)
804 }
805 return tbs[0]
806 }
807
808 //go:noinline
809 func testTracebackGenericFn[T any](buf []byte) int {
810 return runtime.Stack(buf[:], false)
811 }
812
813 func testTracebackGenericFnInlined[T any](buf []byte) int {
814 return runtime.Stack(buf[:], false)
815 }
816
817 type testTracebackGenericTyp[P any] struct{ x P }
818
819 //go:noinline
820 func (t testTracebackGenericTyp[P]) M(buf []byte) int {
821 return runtime.Stack(buf[:], false)
822 }
823
824 func (t testTracebackGenericTyp[P]) Inlined(buf []byte) int {
825 return runtime.Stack(buf[:], false)
826 }
827
828 func TestTracebackGeneric(t *testing.T) {
829 if *flagQuick {
830 t.Skip("-quick")
831 }
832 var x testTracebackGenericTyp[int]
833 tests := []struct {
834 fn func([]byte) int
835 expect string
836 }{
837 // function, not inlined
838 {
839 testTracebackGenericFn[int],
840 "testTracebackGenericFn[...](",
841 },
842 // function, inlined
843 {
844 func(buf []byte) int { return testTracebackGenericFnInlined[int](buf) },
845 "testTracebackGenericFnInlined[...](",
846 },
847 // method, not inlined
848 {
849 x.M,
850 "testTracebackGenericTyp[...].M(",
851 },
852 // method, inlined
853 {
854 func(buf []byte) int { return x.Inlined(buf) },
855 "testTracebackGenericTyp[...].Inlined(",
856 },
857 }
858 var buf [1000]byte
859 for _, test := range tests {
860 n := test.fn(buf[:])
861 got := buf[:n]
862 if !bytes.Contains(got, []byte(test.expect)) {
863 t.Errorf("traceback does not contain expected string: want %q, got\n%s", test.expect, got)
864 }
865 if bytes.Contains(got, []byte("shape")) { // should not contain shape name
866 t.Errorf("traceback contains shape name: got\n%s", got)
867 }
868 }
869 }