暂无描述

symbolz_test.go 5.3KB

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