暂无描述

binutils_test.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. package binutils
  15. import (
  16. "bytes"
  17. "fmt"
  18. "math"
  19. "path/filepath"
  20. "reflect"
  21. "regexp"
  22. "runtime"
  23. "testing"
  24. "github.com/google/pprof/internal/plugin"
  25. )
  26. var testAddrMap = map[int]string{
  27. 1000: "_Z3fooid.clone2",
  28. 2000: "_ZNSaIiEC1Ev.clone18",
  29. 3000: "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm",
  30. }
  31. func functionName(level int) (name string) {
  32. if name = testAddrMap[level]; name != "" {
  33. return name
  34. }
  35. return fmt.Sprintf("fun%d", level)
  36. }
  37. func TestAddr2Liner(t *testing.T) {
  38. const offset = 0x500
  39. a := addr2Liner{rw: &mockAddr2liner{}, base: offset}
  40. for i := 1; i < 8; i++ {
  41. addr := i*0x1000 + offset
  42. s, err := a.addrInfo(uint64(addr))
  43. if err != nil {
  44. t.Fatalf("addrInfo(%#x): %v", addr, err)
  45. }
  46. if len(s) != i {
  47. t.Fatalf("addrInfo(%#x): got len==%d, want %d", addr, len(s), i)
  48. }
  49. for l, f := range s {
  50. level := (len(s) - l) * 1000
  51. want := plugin.Frame{Func: functionName(level), File: fmt.Sprintf("file%d", level), Line: level}
  52. if f != want {
  53. t.Errorf("AddrInfo(%#x)[%d]: = %+v, want %+v", addr, l, f, want)
  54. }
  55. }
  56. }
  57. s, err := a.addrInfo(0xFFFF)
  58. if err != nil {
  59. t.Fatalf("addrInfo(0xFFFF): %v", err)
  60. }
  61. if len(s) != 0 {
  62. t.Fatalf("AddrInfo(0xFFFF): got len==%d, want 0", len(s))
  63. }
  64. a.rw.close()
  65. }
  66. type mockAddr2liner struct {
  67. output []string
  68. }
  69. func (a *mockAddr2liner) write(s string) error {
  70. var lines []string
  71. switch s {
  72. case "1000":
  73. lines = []string{"_Z3fooid.clone2", "file1000:1000"}
  74. case "2000":
  75. lines = []string{"_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  76. case "3000":
  77. lines = []string{"_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  78. case "4000":
  79. lines = []string{"fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  80. case "5000":
  81. lines = []string{"fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  82. case "6000":
  83. lines = []string{"fun6000", "file6000:6000", "fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  84. case "7000":
  85. lines = []string{"fun7000", "file7000:7000", "fun6000", "file6000:6000", "fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  86. case "8000":
  87. lines = []string{"fun8000", "file8000:8000", "fun7000", "file7000:7000", "fun6000", "file6000:6000", "fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  88. case "9000":
  89. lines = []string{"fun9000", "file9000:9000", "fun8000", "file8000:8000", "fun7000", "file7000:7000", "fun6000", "file6000:6000", "fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  90. default:
  91. lines = []string{"??", "??:0"}
  92. }
  93. a.output = append(a.output, "0x"+s)
  94. a.output = append(a.output, lines...)
  95. return nil
  96. }
  97. func (a *mockAddr2liner) readLine() (string, error) {
  98. if len(a.output) == 0 {
  99. return "", fmt.Errorf("end of file")
  100. }
  101. next := a.output[0]
  102. a.output = a.output[1:]
  103. return next, nil
  104. }
  105. func (a *mockAddr2liner) close() {
  106. }
  107. func TestAddr2LinerLookup(t *testing.T) {
  108. const oddSizedData = `
  109. 00001000 T 0x1000
  110. 00002000 T 0x2000
  111. 00003000 T 0x3000
  112. `
  113. const evenSizedData = `
  114. 0000000000001000 T 0x1000
  115. 0000000000002000 T 0x2000
  116. 0000000000003000 T 0x3000
  117. 0000000000004000 T 0x4000
  118. `
  119. for _, d := range []string{oddSizedData, evenSizedData} {
  120. a, err := parseAddr2LinerNM(0, bytes.NewBufferString(d))
  121. if err != nil {
  122. t.Errorf("nm parse error: %v", err)
  123. continue
  124. }
  125. for address, want := range map[uint64]string{
  126. 0x1000: "0x1000",
  127. 0x1001: "0x1000",
  128. 0x1FFF: "0x1000",
  129. 0x2000: "0x2000",
  130. 0x2001: "0x2000",
  131. } {
  132. if got, _ := a.addrInfo(address); !checkAddress(got, address, want) {
  133. t.Errorf("%x: got %v, want %s", address, got, want)
  134. }
  135. }
  136. for _, unknown := range []uint64{0x0fff, 0x4001} {
  137. if got, _ := a.addrInfo(unknown); got != nil {
  138. t.Errorf("%x: got %v, want nil", unknown, got)
  139. }
  140. }
  141. }
  142. }
  143. func checkAddress(got []plugin.Frame, address uint64, want string) bool {
  144. if len(got) != 1 {
  145. return false
  146. }
  147. return got[0].Func == want
  148. }
  149. func TestSetTools(t *testing.T) {
  150. // Test that multiple calls work.
  151. bu := &Binutils{}
  152. bu.SetTools("")
  153. bu.SetTools("")
  154. }
  155. func TestSetFastSymbolization(t *testing.T) {
  156. // Test that multiple calls work.
  157. bu := &Binutils{}
  158. bu.SetFastSymbolization(true)
  159. bu.SetFastSymbolization(false)
  160. }
  161. func skipUnlessLinuxAmd64(t *testing.T) {
  162. if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
  163. t.Skip("This test only works on x86-64 Linux")
  164. }
  165. }
  166. func skipUnlessDarwinAmd64(t *testing.T) {
  167. if runtime.GOOS != "darwin" || runtime.GOARCH != "amd64" {
  168. t.Skip("This test only works on x86-64 Mac")
  169. }
  170. }
  171. func TestDisasm(t *testing.T) {
  172. skipUnlessLinuxAmd64(t)
  173. bu := &Binutils{}
  174. insts, err := bu.Disasm(filepath.Join("testdata", "exe_linux_64"), 0, math.MaxUint64)
  175. if err != nil {
  176. t.Fatalf("Disasm: unexpected error %v", err)
  177. }
  178. mainCount := 0
  179. for _, x := range insts {
  180. if x.Function == "main" {
  181. mainCount++
  182. }
  183. }
  184. if mainCount == 0 {
  185. t.Error("Disasm: found no main instructions")
  186. }
  187. }
  188. func findSymbol(syms []*plugin.Sym, name string) *plugin.Sym {
  189. for _, s := range syms {
  190. for _, n := range s.Name {
  191. if n == name {
  192. return s
  193. }
  194. }
  195. }
  196. return nil
  197. }
  198. func TestObjFile(t *testing.T) {
  199. skipUnlessLinuxAmd64(t)
  200. for _, tc := range []struct {
  201. desc string
  202. start, limit, offset uint64
  203. addr uint64
  204. }{
  205. {"fake mapping", 0, math.MaxUint64, 0, 0x40052d},
  206. {"fixed load address", 0x400000, 0x4006fc, 0, 0x40052d},
  207. // True user-mode ASLR binaries are ET_DYN rather than ET_EXEC so this case
  208. // is a bit artificial except that it approximates the
  209. // vmlinux-with-kernel-ASLR case where the binary *is* ET_EXEC.
  210. {"simulated ASLR address", 0x500000, 0x5006fc, 0, 0x50052d},
  211. } {
  212. t.Run(tc.desc, func(t *testing.T) {
  213. bu := &Binutils{}
  214. f, err := bu.Open(filepath.Join("testdata", "exe_linux_64"), tc.start, tc.limit, tc.offset)
  215. if err != nil {
  216. t.Fatalf("Open: unexpected error %v", err)
  217. }
  218. defer f.Close()
  219. syms, err := f.Symbols(regexp.MustCompile("main"), 0)
  220. if err != nil {
  221. t.Fatalf("Symbols: unexpected error %v", err)
  222. }
  223. m := findSymbol(syms, "main")
  224. if m == nil {
  225. t.Fatalf("Symbols: did not find main")
  226. }
  227. for _, addr := range []uint64{m.Start + f.Base(), tc.addr} {
  228. gotFrames, err := f.SourceLine(addr)
  229. if err != nil {
  230. t.Fatalf("SourceLine: unexpected error %v", err)
  231. }
  232. wantFrames := []plugin.Frame{
  233. {Func: "main", File: "/tmp/hello.c", Line: 3},
  234. }
  235. if !reflect.DeepEqual(gotFrames, wantFrames) {
  236. t.Fatalf("SourceLine for main: got %v; want %v\n", gotFrames, wantFrames)
  237. }
  238. }
  239. })
  240. }
  241. }
  242. func TestMachoFiles(t *testing.T) {
  243. skipUnlessDarwinAmd64(t)
  244. t.Skip("Disabled because of issues with addr2line (see https://github.com/google/pprof/pull/313#issuecomment-364073010)")
  245. // Load `file`, pretending it was mapped at `start`. Then get the symbol
  246. // table. Check that it contains the symbol `sym` and that the address
  247. // `addr` gives the `expected` stack trace.
  248. for _, tc := range []struct {
  249. desc string
  250. file string
  251. start, limit, offset uint64
  252. addr uint64
  253. sym string
  254. expected []plugin.Frame
  255. }{
  256. {"normal mapping", "exe_mac_64", 0x100000000, math.MaxUint64, 0,
  257. 0x100000f50, "_main",
  258. []plugin.Frame{
  259. {Func: "main", File: "/tmp/hello.c", Line: 3},
  260. }},
  261. {"other mapping", "exe_mac_64", 0x200000000, math.MaxUint64, 0,
  262. 0x200000f50, "_main",
  263. []plugin.Frame{
  264. {Func: "main", File: "/tmp/hello.c", Line: 3},
  265. }},
  266. {"lib normal mapping", "lib_mac_64", 0, math.MaxUint64, 0,
  267. 0xfa0, "_bar",
  268. []plugin.Frame{
  269. {Func: "bar", File: "/tmp/lib.c", Line: 6},
  270. }},
  271. } {
  272. t.Run(tc.desc, func(t *testing.T) {
  273. bu := &Binutils{}
  274. f, err := bu.Open(filepath.Join("testdata", tc.file), tc.start, tc.limit, tc.offset)
  275. if err != nil {
  276. t.Fatalf("Open: unexpected error %v", err)
  277. }
  278. defer f.Close()
  279. syms, err := f.Symbols(nil, 0)
  280. if err != nil {
  281. t.Fatalf("Symbols: unexpected error %v", err)
  282. }
  283. m := findSymbol(syms, tc.sym)
  284. if m == nil {
  285. t.Fatalf("Symbols: could not find symbol %v", tc.sym)
  286. }
  287. gotFrames, err := f.SourceLine(tc.addr)
  288. if err != nil {
  289. t.Fatalf("SourceLine: unexpected error %v", err)
  290. }
  291. if !reflect.DeepEqual(gotFrames, tc.expected) {
  292. t.Fatalf("SourceLine for main: got %v; want %v\n", gotFrames, tc.expected)
  293. }
  294. })
  295. }
  296. }
  297. func TestLLVMSymbolizer(t *testing.T) {
  298. if runtime.GOOS != "linux" {
  299. t.Skip("testtdata/llvm-symbolizer has only been tested on linux")
  300. }
  301. cmd := filepath.Join("testdata", "fake-llvm-symbolizer")
  302. symbolizer, err := newLLVMSymbolizer(cmd, "foo", 0)
  303. if err != nil {
  304. t.Fatalf("newLLVMSymbolizer: unexpected error %v", err)
  305. }
  306. defer symbolizer.rw.close()
  307. for _, c := range []struct {
  308. addr uint64
  309. frames []plugin.Frame
  310. }{
  311. {0x10, []plugin.Frame{
  312. {Func: "Inlined_0x10", File: "foo.h", Line: 0},
  313. {Func: "Func_0x10", File: "foo.c", Line: 2},
  314. }},
  315. {0x20, []plugin.Frame{
  316. {Func: "Inlined_0x20", File: "foo.h", Line: 0},
  317. {Func: "Func_0x20", File: "foo.c", Line: 2},
  318. }},
  319. } {
  320. frames, err := symbolizer.addrInfo(c.addr)
  321. if err != nil {
  322. t.Errorf("LLVM: unexpected error %v", err)
  323. continue
  324. }
  325. if !reflect.DeepEqual(frames, c.frames) {
  326. t.Errorf("LLVM: expect %v; got %v\n", c.frames, frames)
  327. }
  328. }
  329. }