暂无描述

binutils_test.go 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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("Disasm only tested on x86-64 linux")
  164. }
  165. }
  166. func TestDisasm(t *testing.T) {
  167. skipUnlessLinuxAmd64(t)
  168. bu := &Binutils{}
  169. insts, err := bu.Disasm(filepath.Join("testdata", "hello"), 0, math.MaxUint64)
  170. if err != nil {
  171. t.Fatalf("Disasm: unexpected error %v", err)
  172. }
  173. mainCount := 0
  174. for _, x := range insts {
  175. if x.Function == "main" {
  176. mainCount++
  177. }
  178. }
  179. if mainCount == 0 {
  180. t.Error("Disasm: found no main instructions")
  181. }
  182. }
  183. func TestObjFile(t *testing.T) {
  184. skipUnlessLinuxAmd64(t)
  185. bu := &Binutils{}
  186. f, err := bu.Open(filepath.Join("testdata", "hello"), 0, math.MaxUint64, 0)
  187. if err != nil {
  188. t.Fatalf("Open: unexpected error %v", err)
  189. }
  190. defer f.Close()
  191. syms, err := f.Symbols(regexp.MustCompile("main"), 0)
  192. if err != nil {
  193. t.Fatalf("Symbols: unexpected error %v", err)
  194. }
  195. find := func(name string) *plugin.Sym {
  196. for _, s := range syms {
  197. for _, n := range s.Name {
  198. if n == name {
  199. return s
  200. }
  201. }
  202. }
  203. return nil
  204. }
  205. m := find("main")
  206. if m == nil {
  207. t.Fatalf("Symbols: did not find main")
  208. }
  209. frames, err := f.SourceLine(m.Start)
  210. if err != nil {
  211. t.Fatalf("SourceLine: unexpected error %v", err)
  212. }
  213. expect := []plugin.Frame{
  214. {Func: "main", File: "/tmp/hello.c", Line: 3},
  215. }
  216. if !reflect.DeepEqual(frames, expect) {
  217. t.Fatalf("SourceLine for main: expect %v; got %v\n", expect, frames)
  218. }
  219. }
  220. func TestLLVMSymbolizer(t *testing.T) {
  221. if runtime.GOOS != "linux" {
  222. t.Skip("testtdata/llvm-symbolizer has only been tested on linux")
  223. }
  224. cmd := filepath.Join("testdata", "fake-llvm-symbolizer")
  225. symbolizer, err := newLLVMSymbolizer(cmd, "foo", 0)
  226. if err != nil {
  227. t.Fatalf("newLLVMSymbolizer: unexpected error %v", err)
  228. }
  229. defer symbolizer.rw.close()
  230. for _, c := range []struct {
  231. addr uint64
  232. frames []plugin.Frame
  233. }{
  234. {0x10, []plugin.Frame{
  235. {Func: "Inlined_0x10", File: "foo.h", Line: 0},
  236. {Func: "Func_0x10", File: "foo.c", Line: 2},
  237. }},
  238. {0x20, []plugin.Frame{
  239. {Func: "Inlined_0x20", File: "foo.h", Line: 0},
  240. {Func: "Func_0x20", File: "foo.c", Line: 2},
  241. }},
  242. } {
  243. frames, err := symbolizer.addrInfo(c.addr)
  244. if err != nil {
  245. t.Errorf("LLVM: unexpected error %v", err)
  246. continue
  247. }
  248. if !reflect.DeepEqual(frames, c.frames) {
  249. t.Errorf("LLVM: expect %v; got %v\n", c.frames, frames)
  250. }
  251. }
  252. }