github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/cmd/addr2line/addr2line_test.go (about) 1 // Copyright 2014 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 main 6 7 import ( 8 "bufio" 9 "bytes" 10 "io/ioutil" 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("go", "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" { 55 switch len(f) { 56 case 2: 57 return funcname, f[0], f[1] 58 case 3: 59 return funcname, f[0] + ":" + f[1], f[2] 60 default: 61 t.Fatalf("no line number found in %q", pathAndLineNo) 62 } 63 } 64 if len(f) != 2 { 65 t.Fatalf("no line number found in %q", pathAndLineNo) 66 } 67 return funcname, f[0], f[1] 68 } 69 70 const symName = "cmd/addr2line.TestAddr2Line" 71 72 func testAddr2Line(t *testing.T, exepath, addr string) { 73 funcName, srcPath, srcLineNo := runAddr2Line(t, exepath, addr) 74 if symName != funcName { 75 t.Fatalf("expected function name %v; got %v", symName, funcName) 76 } 77 fi1, err := os.Stat("addr2line_test.go") 78 if err != nil { 79 t.Fatalf("Stat failed: %v", err) 80 } 81 fi2, err := os.Stat(srcPath) 82 if err != nil { 83 t.Fatalf("Stat failed: %v", err) 84 } 85 if !os.SameFile(fi1, fi2) { 86 t.Fatalf("addr2line_test.go and %s are not same file", srcPath) 87 } 88 if srcLineNo != "94" { 89 t.Fatalf("line number = %v; want 94", srcLineNo) 90 } 91 } 92 93 // This is line 93. The test depends on that. 94 func TestAddr2Line(t *testing.T) { 95 switch runtime.GOOS { 96 case "nacl", "android": 97 t.Skipf("skipping on %s", runtime.GOOS) 98 } 99 100 syms := loadSyms(t) 101 102 tmpDir, err := ioutil.TempDir("", "TestAddr2Line") 103 if err != nil { 104 t.Fatal("TempDir failed: ", err) 105 } 106 defer os.RemoveAll(tmpDir) 107 108 exepath := filepath.Join(tmpDir, "testaddr2line.exe") 109 out, err := exec.Command("go", "build", "-o", exepath, "cmd/addr2line").CombinedOutput() 110 if err != nil { 111 t.Fatalf("go build -o %v cmd/addr2line: %v\n%s", exepath, err, string(out)) 112 } 113 114 testAddr2Line(t, exepath, syms[symName]) 115 testAddr2Line(t, exepath, "0x"+syms[symName]) 116 }