golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/binutils/disasm_test.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package binutils 16 17 import ( 18 "fmt" 19 "regexp" 20 "testing" 21 22 "github.com/google/pprof/internal/plugin" 23 ) 24 25 // TestFindSymbols tests the FindSymbols routine using a hardcoded nm output. 26 func TestFindSymbols(t *testing.T) { 27 type testcase struct { 28 query, syms string 29 want []plugin.Sym 30 } 31 32 testsyms := `0000000000001000 t lineA001 33 0000000000001000 t lineA002 34 0000000000001000 t line1000 35 0000000000002000 t line200A 36 0000000000002000 t line2000 37 0000000000002000 t line200B 38 0000000000003000 t line3000 39 0000000000003000 t _ZNK4DumbclEPKc 40 0000000000003000 t lineB00C 41 0000000000003000 t line300D 42 0000000000004000 t _the_end 43 ` 44 testcases := []testcase{ 45 { 46 "line.*[AC]", 47 testsyms, 48 []plugin.Sym{ 49 {Name: []string{"lineA001"}, File: "object.o", Start: 0x1000, End: 0x1FFF}, 50 {Name: []string{"line200A"}, File: "object.o", Start: 0x2000, End: 0x2FFF}, 51 {Name: []string{"lineB00C"}, File: "object.o", Start: 0x3000, End: 0x3FFF}, 52 }, 53 }, 54 { 55 "Dumb::operator", 56 testsyms, 57 []plugin.Sym{ 58 {Name: []string{"Dumb::operator()(char const*) const"}, File: "object.o", Start: 0x3000, End: 0x3FFF}, 59 }, 60 }, 61 } 62 63 for _, tc := range testcases { 64 syms, err := findSymbols([]byte(tc.syms), "object.o", regexp.MustCompile(tc.query), 0) 65 if err != nil { 66 t.Fatalf("%q: findSymbols: %v", tc.query, err) 67 } 68 if err := checkSymbol(syms, tc.want); err != nil { 69 t.Errorf("%q: %v", tc.query, err) 70 } 71 } 72 } 73 74 func checkSymbol(got []*plugin.Sym, want []plugin.Sym) error { 75 if len(got) != len(want) { 76 return fmt.Errorf("unexpected number of symbols %d (want %d)\n", len(got), len(want)) 77 } 78 79 for i, g := range got { 80 w := want[i] 81 if len(g.Name) != len(w.Name) { 82 return fmt.Errorf("names, got %d, want %d", len(g.Name), len(w.Name)) 83 } 84 for n := range g.Name { 85 if g.Name[n] != w.Name[n] { 86 return fmt.Errorf("name %d, got %q, want %q", n, g.Name[n], w.Name[n]) 87 } 88 } 89 if g.File != w.File { 90 return fmt.Errorf("filename, got %q, want %q", g.File, w.File) 91 } 92 if g.Start != w.Start { 93 return fmt.Errorf("start address, got %#x, want %#x", g.Start, w.Start) 94 } 95 if g.End != w.End { 96 return fmt.Errorf("end address, got %#x, want %#x", g.End, w.End) 97 } 98 } 99 return nil 100 } 101 102 // TestFunctionAssembly tests the FunctionAssembly routine by using a 103 // fake objdump script. 104 func TestFunctionAssembly(t *testing.T) { 105 type testcase struct { 106 s plugin.Sym 107 asm string 108 want []plugin.Inst 109 } 110 testcases := []testcase{ 111 { 112 plugin.Sym{Name: []string{"symbol1"}, Start: 0x1000, End: 0x1FFF}, 113 ` 1000: instruction one 114 1001: instruction two 115 1002: instruction three 116 1003: instruction four 117 `, 118 []plugin.Inst{ 119 {Addr: 0x1000, Text: "instruction one"}, 120 {Addr: 0x1001, Text: "instruction two"}, 121 {Addr: 0x1002, Text: "instruction three"}, 122 {Addr: 0x1003, Text: "instruction four"}, 123 }, 124 }, 125 { 126 plugin.Sym{Name: []string{"symbol2"}, Start: 0x2000, End: 0x2FFF}, 127 ` 2000: instruction one 128 2001: instruction two 129 `, 130 []plugin.Inst{ 131 {Addr: 0x2000, Text: "instruction one"}, 132 {Addr: 0x2001, Text: "instruction two"}, 133 }, 134 }, 135 } 136 137 const objdump = "testdata/wrapper/objdump" 138 139 for _, tc := range testcases { 140 insts, err := disassemble([]byte(tc.asm)) 141 if err != nil { 142 t.Fatalf("FunctionAssembly: %v", err) 143 } 144 145 if len(insts) != len(tc.want) { 146 t.Errorf("Unexpected number of assembly instructions %d (want %d)\n", len(insts), len(tc.want)) 147 } 148 for i := range insts { 149 if insts[i] != tc.want[i] { 150 t.Errorf("Expected symbol %v, got %v\n", tc.want[i], insts[i]) 151 } 152 } 153 } 154 }