No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

153 líneas
4.0 KiB

  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/internal/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
  105. 1001: instruction two
  106. 1002: instruction three
  107. 1003: instruction four
  108. `,
  109. []plugin.Inst{
  110. {Addr: 0x1000, Text: "instruction one"},
  111. {Addr: 0x1001, Text: "instruction two"},
  112. {Addr: 0x1002, Text: "instruction three"},
  113. {Addr: 0x1003, Text: "instruction four"},
  114. },
  115. },
  116. {
  117. plugin.Sym{Name: []string{"symbol2"}, Start: 0x2000, End: 0x2FFF},
  118. ` 2000: instruction one
  119. 2001: instruction two
  120. `,
  121. []plugin.Inst{
  122. {Addr: 0x2000, Text: "instruction one"},
  123. {Addr: 0x2001, Text: "instruction two"},
  124. },
  125. },
  126. }
  127. for _, tc := range testcases {
  128. insts, err := disassemble([]byte(tc.asm))
  129. if err != nil {
  130. t.Fatalf("FunctionAssembly: %v", err)
  131. }
  132. if len(insts) != len(tc.want) {
  133. t.Errorf("Unexpected number of assembly instructions %d (want %d)\n", len(insts), len(tc.want))
  134. }
  135. for i := range insts {
  136. if insts[i] != tc.want[i] {
  137. t.Errorf("Expected symbol %v, got %v\n", tc.want[i], insts[i])
  138. }
  139. }
  140. }
  141. }