暂无描述

binutils_test.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "testing"
  18. "github.com/google/pprof/internal/plugin"
  19. )
  20. var testAddrMap = map[int]string{
  21. 1000: "_Z3fooid.clone2",
  22. 2000: "_ZNSaIiEC1Ev.clone18",
  23. 3000: "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm",
  24. }
  25. func functionName(level int) (name string) {
  26. if name = testAddrMap[level]; name != "" {
  27. return name
  28. }
  29. return fmt.Sprintf("fun%d", level)
  30. }
  31. func TestAddr2Liner(t *testing.T) {
  32. const offset = 0x500
  33. a := addr2Liner{&mockAddr2liner{}, offset, nil}
  34. for i := 1; i < 8; i++ {
  35. addr := i*0x1000 + offset
  36. s, err := a.addrInfo(uint64(addr))
  37. if err != nil {
  38. t.Fatalf("addrInfo(%#x): %v", addr, err)
  39. }
  40. if len(s) != i {
  41. t.Fatalf("addrInfo(%#x): got len==%d, want %d", addr, len(s), i)
  42. }
  43. for l, f := range s {
  44. level := (len(s) - l) * 1000
  45. want := plugin.Frame{functionName(level), fmt.Sprintf("file%d", level), level}
  46. if f != want {
  47. t.Errorf("AddrInfo(%#x)[%d]: = %+v, want %+v", addr, l, f, want)
  48. }
  49. }
  50. }
  51. s, err := a.addrInfo(0xFFFF)
  52. if err != nil {
  53. t.Fatalf("addrInfo(0xFFFF): %v", err)
  54. }
  55. if len(s) != 0 {
  56. t.Fatalf("AddrInfo(0xFFFF): got len==%d, want 0", len(s))
  57. }
  58. a.rw.close()
  59. }
  60. type mockAddr2liner struct {
  61. output []string
  62. }
  63. func (a *mockAddr2liner) write(s string) error {
  64. var lines []string
  65. switch s {
  66. case "1000":
  67. lines = []string{"_Z3fooid.clone2", "file1000:1000"}
  68. case "2000":
  69. lines = []string{"_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  70. case "3000":
  71. lines = []string{"_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  72. case "4000":
  73. lines = []string{"fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  74. case "5000":
  75. lines = []string{"fun5000", "file5000:5000", "fun4000", "file4000:4000", "_ZNSt6vectorIS_IS_IiSaIiEESaIS1_EESaIS3_EEixEm", "file3000:3000", "_ZNSaIiEC1Ev.clone18", "file2000:2000", "_Z3fooid.clone2", "file1000:1000"}
  76. case "6000":
  77. 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"}
  78. case "7000":
  79. 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"}
  80. case "8000":
  81. 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"}
  82. case "9000":
  83. 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"}
  84. default:
  85. lines = []string{"??", "??:0"}
  86. }
  87. a.output = append(a.output, "0x"+s)
  88. a.output = append(a.output, lines...)
  89. return nil
  90. }
  91. func (a *mockAddr2liner) readLine() (string, error) {
  92. if len(a.output) == 0 {
  93. return "", fmt.Errorf("end of file")
  94. }
  95. next := a.output[0]
  96. a.output = a.output[1:]
  97. return next, nil
  98. }
  99. func (a *mockAddr2liner) close() {
  100. }
  101. func TestAddr2LinerLookup(t *testing.T) {
  102. oddSizedMap := addr2LinerNM{
  103. m: []symbolInfo{
  104. {0x1000, "0x1000"},
  105. {0x2000, "0x2000"},
  106. {0x3000, "0x3000"},
  107. },
  108. }
  109. evenSizedMap := addr2LinerNM{
  110. m: []symbolInfo{
  111. {0x1000, "0x1000"},
  112. {0x2000, "0x2000"},
  113. {0x3000, "0x3000"},
  114. {0x4000, "0x4000"},
  115. },
  116. }
  117. for _, a := range []*addr2LinerNM{
  118. &oddSizedMap, &evenSizedMap,
  119. } {
  120. for address, want := range map[uint64]string{
  121. 0x1000: "0x1000",
  122. 0x1001: "0x1000",
  123. 0x1FFF: "0x1000",
  124. 0x2000: "0x2000",
  125. 0x2001: "0x2000",
  126. } {
  127. if got, _ := a.addrInfo(address); !checkAddress(got, address, want) {
  128. t.Errorf("%x: got %v, want %s", address, got, want)
  129. }
  130. }
  131. }
  132. }
  133. func checkAddress(got []plugin.Frame, address uint64, want string) bool {
  134. if len(got) != 1 {
  135. return false
  136. }
  137. return got[0].Func == want
  138. }