Source file
src/cmd/addr2line/addr2line_test.go
1
2
3
4
5 package main
6
7 import (
8 "bufio"
9 "bytes"
10 "internal/testenv"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "runtime"
15 "strings"
16 "testing"
17 )
18
19 func loadSyms(t *testing.T) map[string]string {
20 cmd := exec.Command(testenv.GoToolPath(t), "tool", "nm", os.Args[0])
21 out, err := cmd.CombinedOutput()
22 if err != nil {
23 t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
24 }
25 syms := make(map[string]string)
26 scanner := bufio.NewScanner(bytes.NewReader(out))
27 for scanner.Scan() {
28 f := strings.Fields(scanner.Text())
29 if len(f) < 3 {
30 continue
31 }
32 syms[f[2]] = f[0]
33 }
34 if err := scanner.Err(); err != nil {
35 t.Fatalf("error reading symbols: %v", err)
36 }
37 return syms
38 }
39
40 func runAddr2Line(t *testing.T, exepath, addr string) (funcname, path, lineno string) {
41 cmd := exec.Command(exepath, os.Args[0])
42 cmd.Stdin = strings.NewReader(addr)
43 out, err := cmd.CombinedOutput()
44 if err != nil {
45 t.Fatalf("go tool addr2line %v: %v\n%s", os.Args[0], err, string(out))
46 }
47 f := strings.Split(string(out), "\n")
48 if len(f) < 3 && f[2] == "" {
49 t.Fatal("addr2line output must have 2 lines")
50 }
51 funcname = f[0]
52 pathAndLineNo := f[1]
53 f = strings.Split(pathAndLineNo, ":")
54 if runtime.GOOS == "windows" && len(f) == 3 {
55
56 f = []string{f[0] + ":" + f[1], f[2]}
57 }
58 if len(f) != 2 {
59 t.Fatalf("no line number found in %q", pathAndLineNo)
60 }
61 return funcname, f[0], f[1]
62 }
63
64 const symName = "cmd/addr2line.TestAddr2Line"
65
66 func testAddr2Line(t *testing.T, exepath, addr string) {
67 funcName, srcPath, srcLineNo := runAddr2Line(t, exepath, addr)
68 if symName != funcName {
69 t.Fatalf("expected function name %v; got %v", symName, funcName)
70 }
71 fi1, err := os.Stat("addr2line_test.go")
72 if err != nil {
73 t.Fatalf("Stat failed: %v", err)
74 }
75
76
77 srcPath = filepath.FromSlash(srcPath)
78 fi2, err := os.Stat(srcPath)
79
80
81
82
83 if gorootFinal := os.Getenv("GOROOT_FINAL"); gorootFinal != "" &&
84 (os.IsNotExist(err) || (err == nil && !os.SameFile(fi1, fi2))) {
85
86
87 gorootFinal = filepath.Clean(gorootFinal)
88
89 if strings.HasPrefix(srcPath, gorootFinal) {
90 fi2, err = os.Stat(runtime.GOROOT() + strings.TrimPrefix(srcPath, gorootFinal))
91 }
92 }
93
94 if err != nil {
95 t.Fatalf("Stat failed: %v", err)
96 }
97 if !os.SameFile(fi1, fi2) {
98 t.Fatalf("addr2line_test.go and %s are not same file", srcPath)
99 }
100 if srcLineNo != "106" {
101 t.Fatalf("line number = %v; want 106", srcLineNo)
102 }
103 }
104
105
106 func TestAddr2Line(t *testing.T) {
107 testenv.MustHaveGoBuild(t)
108
109 tmpDir, err := os.MkdirTemp("", "TestAddr2Line")
110 if err != nil {
111 t.Fatal("TempDir failed: ", err)
112 }
113 defer os.RemoveAll(tmpDir)
114
115
116
117 exepath := filepath.Join(tmpDir, "testaddr2line_test.exe")
118 out, err := exec.Command(testenv.GoToolPath(t), "test", "-c", "-o", exepath, "cmd/addr2line").CombinedOutput()
119 if err != nil {
120 t.Fatalf("go test -c -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
121 }
122 os.Args[0] = exepath
123
124 syms := loadSyms(t)
125
126 exepath = filepath.Join(tmpDir, "testaddr2line.exe")
127 out, err = exec.Command(testenv.GoToolPath(t), "build", "-o", exepath, "cmd/addr2line").CombinedOutput()
128 if err != nil {
129 t.Fatalf("go build -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
130 }
131
132 testAddr2Line(t, exepath, syms[symName])
133 testAddr2Line(t, exepath, "0x"+syms[symName])
134 }
135
View as plain text