Source file
src/runtime/traceback_test.go
1
2
3
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
23 func TestTracebackInlined(t *testing.T) {
24 testenv.SkipIfOptimizationOff(t)
25 check := func(t *testing.T, r *ttiResult, funcs ...string) {
26 t.Helper()
27
28
29 frames := parseTraceback1(t, r.printed).frames
30 t.Log(r.printed)
31
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
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
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
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
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
76
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
87 func ttiLeaf() *ttiResult {
88
89 printed := string(debug.Stack())
90 return &ttiResult{printed}
91 }
92
93
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
105 func ttiSigpanic1() (res *ttiResult) {
106 defer func() {
107 res = ttiLeaf()
108 recover()
109 }()
110 ttiSigpanic2()
111
112
113
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
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
143 func ttiExcluded1() *ttiResult {
144 return ttiExcluded2()
145 }
146
147
148
149
150
151
152
153
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
165
166
167
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
173 stackChan := make(chan string)
174 go tteStack(n, stackChan)
175 stack := <-stackChan
176 tb := parseTraceback1(t, stack)
177
178
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
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
225
226
227 func tteStack(n int, stack chan<- string) {
228 n--
229
230
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
254
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
277
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
289 {
290 func() int { return testTracebackArgs1(1, 2, 3, 4, 5) },
291 "testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5)",
292 },
293
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
308 {
309 func() int { return testTracebackArgs4(false, [1][1][1][1][1][1][1][1][1][1]int{}) },
310 "testTracebackArgs4(0x0, {{{{{...}}}}})",
311 },
312
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
326
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
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
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
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
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
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
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
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
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
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
378
379
380 {
381 func() int {
382 poisonStack()
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
390
391 {
392 func() int {
393 poisonStack()
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
401
402 {
403 func() int {
404 poisonStack()
405 return testTracebackArgs11a(1, 2, 3)
406 },
407 abiSel(
408 "testTracebackArgs11a(0xffffffff?, 0xffffffff?, 0xffffffff?)",
409 "testTracebackArgs11a(0x1, 0x2, 0x3)"),
410 },
411
412
413 {
414 func() int {
415 poisonStack()
416 return testTracebackArgs11b(1, 2, 3, 4)
417 },
418 abiSel(
419 "testTracebackArgs11b(0xffffffff?, 0xffffffff?, 0x3?, 0x4)",
420 "testTracebackArgs11b(0x1, 0x2, 0x3, 0x4)"),
421 },
422
423
424
425 {
426 func() int {
427 poisonStack()
428 return testTracebackArgsSlice(testTracebackArgsSliceBackingStore[:])
429 },
430
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
444 func testTracebackArgs1(a, b, c, d, e int) int {
445 n := runtime.Stack(testTracebackArgsBuf[:], false)
446 if a < 0 {
447
448 return a + b + c + d + e
449 }
450 return n
451 }
452
453
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
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
467
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
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
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)
482 }
483 return n
484 }
485
486
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)
495 }
496 return n
497 }
498
499
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
504 return a + b + c + d + e + f + g + h + i + j
505 }
506 return n
507 }
508
509
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
514 return a + b + c + d + e + f + g + h + i + j + k
515 }
516 return n
517 }
518
519
520 func testTracebackArgs7a(a [10]int) int {
521 n := runtime.Stack(testTracebackArgsBuf[:], false)
522 if a[0] < 0 {
523
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
530 func testTracebackArgs7b(a [11]int) int {
531 n := runtime.Stack(testTracebackArgsBuf[:], false)
532 if a[0] < 0 {
533
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
540 func testTracebackArgs7c(a [10]int, b int) int {
541 n := runtime.Stack(testTracebackArgsBuf[:], false)
542 if a[0] < 0 {
543
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
550 func testTracebackArgs7d(a [11]int, b int) int {
551 n := runtime.Stack(testTracebackArgsBuf[:], false)
552 if a[0] < 0 {
553
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
579 func testTracebackArgs8a(a testArgsType8a) int {
580 n := runtime.Stack(testTracebackArgsBuf[:], false)
581 if a.a < 0 {
582
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
589 func testTracebackArgs8b(a testArgsType8b) int {
590 n := runtime.Stack(testTracebackArgsBuf[:], false)
591 if a.a < 0 {
592
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
599 func testTracebackArgs8c(a testArgsType8c) int {
600 n := runtime.Stack(testTracebackArgsBuf[:], false)
601 if a.a < 0 {
602
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
609 func testTracebackArgs8d(a testArgsType8d) int {
610 n := runtime.Stack(testTracebackArgsBuf[:], false)
611 if a.a < 0 {
612
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
619
620
621
622 func testTracebackArgs9(a int64, b int32, c int16, d int8, x [2]int, y int) int {
623 if a < 0 {
624 println(&y)
625 }
626 n := runtime.Stack(testTracebackArgsBuf[:], false)
627 if a < 0 {
628
629 return int(a) + int(c)
630 }
631 return n
632 }
633
634
635
636
637
638 func testTracebackArgs10(a, b, c, d, e int32) int {
639
640 return runtime.Stack(testTracebackArgsBuf[:], false)
641 }
642
643
644
645
646
647
648
649 func testTracebackArgs11a(a, b, c int32) int {
650 if a < 0 {
651 println(a, b, c)
652 }
653 if b < 0 {
654 return int(a + b + c)
655 }
656 return runtime.Stack(testTracebackArgsBuf[:], false)
657 }
658
659
660
661
662
663
664
665 func testTracebackArgs11b(a, b, c, d int32) int {
666 var x int32
667 if a < 0 {
668 print()
669 x = b
670 } else {
671 print()
672 x = c
673 }
674 if d < 0 {
675 return int(x + d)
676 }
677 return runtime.Stack(testTracebackArgsBuf[:], false)
678 }
679
680
681
682
683
684
685
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
694
695
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
708
709
710
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
723 }
724
725 type tbFrame struct {
726 funcName string
727 args string
728 inlined bool
729
730
731
732 elided int
733
734 off int
735 }
736
737
738
739 func parseTraceback(t *testing.T, tb string) []*traceback {
740
741
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
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
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]
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
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
799
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
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
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
838 {
839 testTracebackGenericFn[int],
840 "testTracebackGenericFn[...](",
841 },
842
843 {
844 func(buf []byte) int { return testTracebackGenericFnInlined[int](buf) },
845 "testTracebackGenericFnInlined[...](",
846 },
847
848 {
849 x.M,
850 "testTracebackGenericTyp[...].M(",
851 },
852
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")) {
866 t.Errorf("traceback contains shape name: got\n%s", got)
867 }
868 }
869 }
870
View as plain text