You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

170 lines
6.2 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 symbolz
  15. import (
  16. "fmt"
  17. "math"
  18. "strings"
  19. "testing"
  20. "github.com/google/pprof/internal/plugin"
  21. "github.com/google/pprof/internal/proftest"
  22. "github.com/google/pprof/profile"
  23. )
  24. func TestSymbolzURL(t *testing.T) {
  25. for try, want := range map[string]string{
  26. "http://host:8000/profilez": "http://host:8000/symbolz",
  27. "http://host:8000/profilez?seconds=5": "http://host:8000/symbolz",
  28. "http://host:8000/profilez?seconds=5&format=proto": "http://host:8000/symbolz",
  29. "http://host:8000/heapz?format=legacy": "http://host:8000/symbolz",
  30. "http://host:8000/debug/pprof/profile": "http://host:8000/debug/pprof/symbol",
  31. "http://host:8000/debug/pprof/profile?seconds=10": "http://host:8000/debug/pprof/symbol",
  32. "http://host:8000/debug/pprof/heap": "http://host:8000/debug/pprof/symbol",
  33. "http://some.host:8080/some/deeper/path/debug/pprof/endpoint?param=value": "http://some.host:8080/some/deeper/path/debug/pprof/symbol",
  34. "http://host:8000/pprof/profile": "http://host:8000/pprof/symbol",
  35. "http://host:8000/pprof/profile?seconds=15": "http://host:8000/pprof/symbol",
  36. "http://host:8000/pprof/heap": "http://host:8000/pprof/symbol",
  37. "http://host:8000/debug/pprof/block": "http://host:8000/debug/pprof/symbol",
  38. "http://host:8000/debug/pprof/trace?seconds=5": "http://host:8000/debug/pprof/symbol",
  39. "http://host:8000/debug/pprof/mutex": "http://host:8000/debug/pprof/symbol",
  40. "http://host/whatever/pprof/heap": "http://host/whatever/pprof/symbol",
  41. "http://host/whatever/pprof/growth": "http://host/whatever/pprof/symbol",
  42. "http://host/whatever/pprof/profile": "http://host/whatever/pprof/symbol",
  43. "http://host/whatever/pprof/pmuprofile": "http://host/whatever/pprof/symbol",
  44. "http://host/whatever/pprof/contention": "http://host/whatever/pprof/symbol",
  45. } {
  46. if got := symbolz(try); got != want {
  47. t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
  48. }
  49. }
  50. }
  51. func TestSymbolize(t *testing.T) {
  52. s := plugin.MappingSources{
  53. "buildid": []struct {
  54. Source string
  55. Start uint64
  56. }{
  57. {Source: "http://localhost:80/profilez"},
  58. },
  59. }
  60. for _, hasFunctions := range []bool{false, true} {
  61. for _, force := range []bool{false, true} {
  62. p := testProfile(hasFunctions)
  63. if err := Symbolize(p, force, s, fetchSymbols, &proftest.TestUI{T: t}); err != nil {
  64. t.Errorf("symbolz: %v", err)
  65. continue
  66. }
  67. var wantSym, wantNoSym []*profile.Location
  68. if force || !hasFunctions {
  69. wantNoSym = p.Location[:1]
  70. wantSym = p.Location[1:]
  71. } else {
  72. wantNoSym = p.Location
  73. }
  74. if err := checkSymbolized(wantSym, true); err != nil {
  75. t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
  76. }
  77. if err := checkSymbolized(wantNoSym, false); err != nil {
  78. t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
  79. }
  80. }
  81. }
  82. }
  83. func testProfile(hasFunctions bool) *profile.Profile {
  84. m := []*profile.Mapping{
  85. {
  86. ID: 1,
  87. Start: 0x1000,
  88. Limit: 0x5000,
  89. BuildID: "buildid",
  90. HasFunctions: hasFunctions,
  91. },
  92. }
  93. p := &profile.Profile{
  94. Location: []*profile.Location{
  95. {ID: 1, Mapping: m[0], Address: 0x1000},
  96. {ID: 2, Mapping: m[0], Address: 0x2000},
  97. {ID: 3, Mapping: m[0], Address: 0x3000},
  98. {ID: 4, Mapping: m[0], Address: 0x4000},
  99. },
  100. Mapping: m,
  101. }
  102. return p
  103. }
  104. func checkSymbolized(locs []*profile.Location, wantSymbolized bool) error {
  105. for _, loc := range locs {
  106. if !wantSymbolized && len(loc.Line) != 0 {
  107. return fmt.Errorf("unexpected symbolization for %#x: %v", loc.Address, loc.Line)
  108. }
  109. if wantSymbolized {
  110. if len(loc.Line) != 1 {
  111. return fmt.Errorf("expected symbolization for %#x: %v", loc.Address, loc.Line)
  112. }
  113. address := loc.Address - loc.Mapping.Start
  114. if got, want := loc.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
  115. return fmt.Errorf("symbolz %#x, got %s, want %s", address, got, want)
  116. }
  117. }
  118. }
  119. return nil
  120. }
  121. func fetchSymbols(source, post string) ([]byte, error) {
  122. var symbolz string
  123. addresses := strings.Split(post, "+")
  124. // Do not symbolize the first symbol.
  125. for _, address := range addresses[1:] {
  126. symbolz += fmt.Sprintf("%s\t%s\n", address, address)
  127. }
  128. return []byte(symbolz), nil
  129. }
  130. func TestAdjust(t *testing.T) {
  131. for _, tc := range []struct {
  132. addr uint64
  133. offset int64
  134. wantAdj uint64
  135. wantOverflow bool
  136. }{{math.MaxUint64, 0, math.MaxUint64, false},
  137. {math.MaxUint64, 1, 0, true},
  138. {math.MaxUint64 - 1, 1, math.MaxUint64, false},
  139. {math.MaxUint64 - 1, 2, 0, true},
  140. {math.MaxInt64 + 1, math.MaxInt64, math.MaxUint64, false},
  141. {0, 0, 0, false},
  142. {0, -1, 0, true},
  143. {1, -1, 0, false},
  144. {2, -1, 1, false},
  145. {2, -2, 0, false},
  146. {2, -3, 0, true},
  147. {-math.MinInt64, math.MinInt64, 0, false},
  148. {-math.MinInt64 + 1, math.MinInt64, 1, false},
  149. {-math.MinInt64 - 1, math.MinInt64, 0, true},
  150. } {
  151. if adj, overflow := adjust(tc.addr, tc.offset); adj != tc.wantAdj || overflow != tc.wantOverflow {
  152. t.Errorf("adjust(%d, %d) = (%d, %t), want (%d, %t)", tc.addr, tc.offset, adj, overflow, tc.wantAdj, tc.wantOverflow)
  153. }
  154. }
  155. }