Nessuna descrizione

disasm_test.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "fmt"
  17. "regexp"
  18. "testing"
  19. "github.com/google/pprof/pkg/plugin"
  20. )
  21. // TestFindSymbols tests the FindSymbols routine using a hardcoded nm output.
  22. func TestFindSymbols(t *testing.T) {
  23. type testcase struct {
  24. query, syms string
  25. want []plugin.Sym
  26. }
  27. testsyms := `0000000000001000 t lineA001
  28. 0000000000001000 t lineA002
  29. 0000000000001000 t line1000
  30. 0000000000002000 t line200A
  31. 0000000000002000 t line2000
  32. 0000000000002000 t line200B
  33. 0000000000003000 t line3000
  34. 0000000000003000 t _ZNK4DumbclEPKc
  35. 0000000000003000 t lineB00C
  36. 0000000000003000 t line300D
  37. 0000000000004000 t _the_end
  38. `
  39. testcases := []testcase{
  40. {
  41. "line.*[AC]",
  42. testsyms,
  43. []plugin.Sym{
  44. {Name: []string{"lineA001"}, File: "object.o", Start: 0x1000, End: 0x1FFF},
  45. {Name: []string{"line200A"}, File: "object.o", Start: 0x2000, End: 0x2FFF},
  46. {Name: []string{"lineB00C"}, File: "object.o", Start: 0x3000, End: 0x3FFF},
  47. },
  48. },
  49. {
  50. "Dumb::operator",
  51. testsyms,
  52. []plugin.Sym{
  53. {Name: []string{"Dumb::operator()(char const*) const"}, File: "object.o", Start: 0x3000, End: 0x3FFF},
  54. },
  55. },
  56. }
  57. for _, tc := range testcases {
  58. syms, err := findSymbols([]byte(tc.syms), "object.o", regexp.MustCompile(tc.query), 0)
  59. if err != nil {
  60. t.Fatalf("%q: findSymbols: %v", tc.query, err)
  61. }
  62. if err := checkSymbol(syms, tc.want); err != nil {
  63. t.Errorf("%q: %v", tc.query, err)
  64. }
  65. }
  66. }
  67. func checkSymbol(got []*plugin.Sym, want []plugin.Sym) error {
  68. if len(got) != len(want) {
  69. return fmt.Errorf("unexpected number of symbols %d (want %d)", len(got), len(want))
  70. }
  71. for i, g := range got {
  72. w := want[i]
  73. if len(g.Name) != len(w.Name) {
  74. return fmt.Errorf("names, got %d, want %d", len(g.Name), len(w.Name))
  75. }
  76. for n := range g.Name {
  77. if g.Name[n] != w.Name[n] {
  78. return fmt.Errorf("name %d, got %q, want %q", n, g.Name[n], w.Name[n])
  79. }
  80. }
  81. if g.File != w.File {
  82. return fmt.Errorf("filename, got %q, want %q", g.File, w.File)
  83. }
  84. if g.Start != w.Start {
  85. return fmt.Errorf("start address, got %#x, want %#x", g.Start, w.Start)
  86. }
  87. if g.End != w.End {
  88. return fmt.Errorf("end address, got %#x, want %#x", g.End, w.End)
  89. }
  90. }
  91. return nil
  92. }
  93. // TestFunctionAssembly tests the FunctionAssembly routine by using a
  94. // fake objdump script.
  95. func TestFunctionAssembly(t *testing.T) {
  96. type testcase struct {
  97. s plugin.Sym
  98. asm string
  99. want []plugin.Inst
  100. }
  101. testcases := []testcase{
  102. {
  103. plugin.Sym{Name: []string{"symbol1"}, Start: 0x1000, End: 0x1FFF},
  104. " 1000: instruction one\n 1001: instruction two\n 1002: instruction three\n 1003: instruction four",
  105. []plugin.Inst{
  106. {Addr: 0x1000, Text: "instruction one"},
  107. {Addr: 0x1001, Text: "instruction two"},
  108. {Addr: 0x1002, Text: "instruction three"},
  109. {Addr: 0x1003, Text: "instruction four"},
  110. },
  111. },
  112. {
  113. plugin.Sym{Name: []string{"symbol2"}, Start: 0x2000, End: 0x2FFF},
  114. " 2000: instruction one\n 2001: instruction two",
  115. []plugin.Inst{
  116. {Addr: 0x2000, Text: "instruction one"},
  117. {Addr: 0x2001, Text: "instruction two"},
  118. },
  119. },
  120. {
  121. plugin.Sym{Name: []string{"_main"}, Start: 0x30000, End: 0x3FFF},
  122. "_main:\n; /tmp/hello.c:3\n30001: push %rbp",
  123. []plugin.Inst{
  124. {Addr: 0x30001, Text: "push %rbp", Function: "_main", File: "/tmp/hello.c", Line: 3},
  125. },
  126. },
  127. {
  128. plugin.Sym{Name: []string{"main"}, Start: 0x4000, End: 0x4FFF},
  129. "000000000040052d <main>:\nmain():\n/tmp/hello.c:3\n40001: push %rbp",
  130. []plugin.Inst{
  131. {Addr: 0x40001, Text: "push %rbp", Function: "main", File: "/tmp/hello.c", Line: 3},
  132. },
  133. },
  134. }
  135. for _, tc := range testcases {
  136. insts, err := disassemble([]byte(tc.asm))
  137. if err != nil {
  138. t.Fatalf("FunctionAssembly: %v", err)
  139. }
  140. if len(insts) != len(tc.want) {
  141. t.Errorf("Unexpected number of assembly instructions %d (want %d)\n", len(insts), len(tc.want))
  142. }
  143. for i := range insts {
  144. if insts[i] != tc.want[i] {
  145. t.Errorf("Expected symbol %v, got %v\n", tc.want[i], insts[i])
  146. }
  147. }
  148. }
  149. }