1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func loadSyms(t *testing.T) map[string]string {
cmd := exec.Command(testenv.GoToolPath(t), "tool", "nm", os.Args[0])
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
}
syms := make(map[string]string)
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
f := strings.Fields(scanner.Text())
if len(f) < 3 {
continue
}
syms[f[2]] = f[0]
}
if err := scanner.Err(); err != nil {
t.Fatalf("error reading symbols: %v", err)
}
return syms
}
func runAddr2Line(t *testing.T, exepath, addr string) (funcname, path, lineno string) {
cmd := exec.Command(exepath, os.Args[0])
cmd.Stdin = strings.NewReader(addr)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool addr2line %v: %v\n%s", os.Args[0], err, string(out))
}
f := strings.Split(string(out), "\n")
if len(f) < 3 && f[2] == "" {
t.Fatal("addr2line output must have 2 lines")
}
funcname = f[0]
pathAndLineNo := f[1]
f = strings.Split(pathAndLineNo, ":")
if runtime.GOOS == "windows" && len(f) == 3 {
// Reattach drive letter.
f = []string{f[0] + ":" + f[1], f[2]}
}
if len(f) != 2 {
t.Fatalf("no line number found in %q", pathAndLineNo)
}
return funcname, f[0], f[1]
}
const symName = "cmd/addr2line.TestAddr2Line"
func testAddr2Line(t *testing.T, exepath, addr string) {
funcName, srcPath, srcLineNo := runAddr2Line(t, exepath, addr)
if symName != funcName {
t.Fatalf("expected function name %v; got %v", symName, funcName)
}
fi1, err := os.Stat("addr2line_test.go")
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
// Debug paths are stored slash-separated, so convert to system-native.
srcPath = filepath.FromSlash(srcPath)
fi2, err := os.Stat(srcPath)
if gorootFinal := os.Getenv("GOROOT_FINAL"); gorootFinal != "" && strings.HasPrefix(srcPath, gorootFinal) {
if os.IsNotExist(err) || (err == nil && !os.SameFile(fi1, fi2)) {
// srcPath has had GOROOT_FINAL substituted for GOROOT, and it doesn't
// match the actual file. GOROOT probably hasn't been moved to its final
// location yet, so try the original location instead.
fi2, err = os.Stat(runtime.GOROOT() + strings.TrimPrefix(srcPath, gorootFinal))
}
}
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
if !os.SameFile(fi1, fi2) {
t.Fatalf("addr2line_test.go and %s are not same file", srcPath)
}
if srcLineNo != "99" {
t.Fatalf("line number = %v; want 99", srcLineNo)
}
}
// This is line 98. The test depends on that.
func TestAddr2Line(t *testing.T) {
testenv.MustHaveGoBuild(t)
tmpDir, err := ioutil.TempDir("", "TestAddr2Line")
if err != nil {
t.Fatal("TempDir failed: ", err)
}
defer os.RemoveAll(tmpDir)
// Build copy of test binary with debug symbols,
// since the one running now may not have them.
exepath := filepath.Join(tmpDir, "testaddr2line_test.exe")
out, err := exec.Command(testenv.GoToolPath(t), "test", "-c", "-o", exepath, "cmd/addr2line").CombinedOutput()
if err != nil {
t.Fatalf("go test -c -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
}
os.Args[0] = exepath
syms := loadSyms(t)
exepath = filepath.Join(tmpDir, "testaddr2line.exe")
out, err = exec.Command(testenv.GoToolPath(t), "build", "-o", exepath, "cmd/addr2line").CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v cmd/addr2line: %v\n%s", exepath, err, string(out))
}
testAddr2Line(t, exepath, syms[symName])
testAddr2Line(t, exepath, "0x"+syms[symName])
}
|