...
1
2
3
4
5 package debug_test
6
7 import (
8 "bytes"
9 "fmt"
10 "internal/testenv"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "runtime"
15 . "runtime/debug"
16 "strings"
17 "testing"
18 )
19
20 func TestMain(m *testing.M) {
21 if os.Getenv("GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT") != "" {
22 fmt.Println(runtime.GOROOT())
23 os.Exit(0)
24 }
25 os.Exit(m.Run())
26 }
27
28 type T int
29
30 func (t *T) ptrmethod() []byte {
31 return Stack()
32 }
33 func (t T) method() []byte {
34 return t.ptrmethod()
35 }
36
37
55 func TestStack(t *testing.T) {
56 b := T(0).method()
57 lines := strings.Split(string(b), "\n")
58 if len(lines) < 6 {
59 t.Fatal("too few lines")
60 }
61
62
63
64
65
66 fileGoroot := ""
67 if envGoroot := os.Getenv("GOROOT"); envGoroot != "" {
68
69
70
71
72
73 t.Logf("found GOROOT %q from environment; checking embedded GOROOT value", envGoroot)
74 testenv.MustHaveExec(t)
75 exe, err := os.Executable()
76 if err != nil {
77 t.Fatal(err)
78 }
79 cmd := exec.Command(exe)
80 cmd.Env = append(os.Environ(), "GOROOT=", "GO_RUNTIME_DEBUG_TEST_DUMP_GOROOT=1")
81 out, err := cmd.Output()
82 if err != nil {
83 t.Fatal(err)
84 }
85 fileGoroot = string(bytes.TrimSpace(out))
86 } else {
87
88
89 fileGoroot = runtime.GOROOT()
90 }
91 filePrefix := ""
92 if fileGoroot != "" {
93 filePrefix = filepath.ToSlash(fileGoroot) + "/src/"
94 }
95
96 n := 0
97 frame := func(file, code string) {
98 t.Helper()
99
100 line := lines[n]
101 if !strings.Contains(line, code) {
102 t.Errorf("expected %q in %q", code, line)
103 }
104 n++
105
106 line = lines[n]
107
108 wantPrefix := "\t" + filePrefix + file
109 if !strings.HasPrefix(line, wantPrefix) {
110 t.Errorf("in line %q, expected prefix %q", line, wantPrefix)
111 }
112 n++
113 }
114 n++
115
116 frame("runtime/debug/stack.go", "runtime/debug.Stack")
117 frame("runtime/debug/stack_test.go", "runtime/debug_test.(*T).ptrmethod")
118 frame("runtime/debug/stack_test.go", "runtime/debug_test.T.method")
119 frame("runtime/debug/stack_test.go", "runtime/debug_test.TestStack")
120 frame("testing/testing.go", "")
121 }
122
View as plain text