| 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 binutilsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 21 | |
| 22 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 23 | ) |
| 24 | |
| 25 | // TestFindSymbols tests the FindSymbols routine using a hardcoded nm output. |
| 26 | func TestFindSymbolsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/TestFindSymbols().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 27 | type testcase struct { |
| 28 | query, syms string |
| 29 | want []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/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 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{ |
| 49 | {Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"lineA001"}, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: "object.o", Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x1000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x1FFF}, |
| 50 | {Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"line200A"}, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: "object.o", Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x2000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x2FFF}, |
| 51 | {Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"lineB00C"}, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: "object.o", Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x3000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x3FFF}, |
| 52 | }, |
| 53 | }, |
| 54 | { |
| 55 | "Dumb::operator", |
| 56 | testsyms, |
| 57 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{ |
| 58 | {Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"Dumb::operator()(char const*) const"}, Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.: "object.o", Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x3000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x3FFF}, |
| 59 | }, |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | for _, tc := range testcases { |
| 64 | syms, err := findSymbolsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/findSymbols().([]byte(tc.syms), "object.o", regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(tc.query), 0) |
| 65 | if err != nil { |
| 66 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("%q: findSymbols: %v", tc.query, err) |
| 67 | } |
| 68 | if err := checkSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/checkSymbol().(syms, tc.want); err != nil { |
| 69 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("%q: %v", tc.query, err) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func checkSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/checkSymbol().(got []*pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#, want []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#) error { |
| 75 | if len(got) != len(want) { |
| 76 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("unexpected number of symbols %d (want %d)", len(got), len(want)) |
| 77 | } |
| 78 | |
| 79 | for i, g := range got { |
| 80 | w := want[i] |
| 81 | if len(g.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.) != len(w.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.) { |
| 82 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("names, got %d, want %d", len(g.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.), len(w.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.)) |
| 83 | } |
| 84 | for n := range g.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name. { |
| 85 | if g.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.[n] != w.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.[n] { |
| 86 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("name %d, got %q, want %q", n, g.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.[n], w.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.[n]) |
| 87 | } |
| 88 | } |
| 89 | if g.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File. != w.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File. { |
| 90 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("filename, got %q, want %q", g.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File., w.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#File.) |
| 91 | } |
| 92 | if g.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start. != w.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start. { |
| 93 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("start address, got %#x, want %#x", g.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start., w.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.) |
| 94 | } |
| 95 | if g.Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End. != w.Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End. { |
| 96 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("end address, got %#x, want %#x", g.Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End., w.Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.) |
| 97 | } |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // TestFunctionAssembly tests the FunctionAssembly routine by using a |
| 103 | // fake objdump script. |
| 104 | func TestFunctionAssemblyscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/TestFunctionAssembly().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 105 | type testcase struct { |
| 106 | s pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym# |
| 107 | asm string |
| 108 | want []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst# |
| 109 | } |
| 110 | testcases := []testcase{ |
| 111 | { |
| 112 | pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"symbol1"}, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x1000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x1FFF}, |
| 113 | " 1000: instruction one\n 1001: instruction two\n 1002: instruction three\n 1003: instruction four", |
| 114 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#{ |
| 115 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x1000, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction one"}, |
| 116 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x1001, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction two"}, |
| 117 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x1002, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction three"}, |
| 118 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x1003, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction four"}, |
| 119 | }, |
| 120 | }, |
| 121 | { |
| 122 | pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"symbol2"}, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x2000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x2FFF}, |
| 123 | " 2000: instruction one\n 2001: instruction two", |
| 124 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#{ |
| 125 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x2000, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction one"}, |
| 126 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x2001, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "instruction two"}, |
| 127 | }, |
| 128 | }, |
| 129 | { |
| 130 | pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"_main"}, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x30000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x3FFF}, |
| 131 | "_main:\n; /tmp/hello.c:3\n30001: push %rbp", |
| 132 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#{ |
| 133 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x30001, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "push %rbp", Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Function.: "_main", Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#File.: "/tmp/hello.c", Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Line.: 3}, |
| 134 | }, |
| 135 | }, |
| 136 | { |
| 137 | pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#{Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Name.: []string{"main"}, Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#Start.: 0x4000, Endscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Sym#End.: 0x4FFF}, |
| 138 | "000000000040052d <main>:\nmain():\n/tmp/hello.c:3\n40001: push %rbp", |
| 139 | []pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Instscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#{ |
| 140 | {Addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Addr.: 0x40001, Textscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Text.: "push %rbp", Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Function.: "main", Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#File.: "/tmp/hello.c", Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Inst#Line.: 3}, |
| 141 | }, |
| 142 | }, |
| 143 | } |
| 144 | |
| 145 | for _, tc := range testcases { |
| 146 | insts, err := disassemblescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/binutils`/disassemble().([]byte(tc.asm)) |
| 147 | if err != nil { |
| 148 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("FunctionAssembly: %v", err) |
| 149 | } |
| 150 | |
| 151 | if len(insts) != len(tc.want) { |
| 152 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Unexpected number of assembly instructions %d (want %d)\n", len(insts), len(tc.want)) |
| 153 | } |
| 154 | for i := range insts { |
| 155 | if insts[i] != tc.want[i] { |
| 156 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Expected symbol %v, got %v\n", tc.want[i], insts[i]) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |