暫無描述

symbolz_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. } {
  34. if got := symbolz(try); got != want {
  35. t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
  36. }
  37. }
  38. }
  39. func TestSymbolize(t *testing.T) {
  40. s := plugin.MappingSources{
  41. "buildid": []struct {
  42. Source string
  43. Start uint64
  44. }{
  45. {Source: "http://localhost:80/profilez"},
  46. },
  47. }
  48. for _, hasFunctions := range []bool{false, true} {
  49. for _, force := range []bool{false, true} {
  50. p := testProfile(hasFunctions)
  51. if err := Symbolize(p, force, s, fetchSymbols, &proftest.TestUI{T: t}); err != nil {
  52. t.Errorf("symbolz: %v", err)
  53. continue
  54. }
  55. var wantSym, wantNoSym []*profile.Location
  56. if force || !hasFunctions {
  57. wantNoSym = p.Location[:1]
  58. wantSym = p.Location[1:]
  59. } else {
  60. wantNoSym = p.Location
  61. }
  62. if err := checkSymbolized(wantSym, true); err != nil {
  63. t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
  64. }
  65. if err := checkSymbolized(wantNoSym, false); err != nil {
  66. t.Errorf("symbolz hasFns=%v force=%v: %v", hasFunctions, force, err)
  67. }
  68. }
  69. }
  70. }
  71. func testProfile(hasFunctions bool) *profile.Profile {
  72. m := []*profile.Mapping{
  73. {
  74. ID: 1,
  75. Start: 0x1000,
  76. Limit: 0x5000,
  77. BuildID: "buildid",
  78. HasFunctions: hasFunctions,
  79. },
  80. }
  81. p := &profile.Profile{
  82. Location: []*profile.Location{
  83. {ID: 1, Mapping: m[0], Address: 0x1000},
  84. {ID: 2, Mapping: m[0], Address: 0x2000},
  85. {ID: 3, Mapping: m[0], Address: 0x3000},
  86. {ID: 4, Mapping: m[0], Address: 0x4000},
  87. },
  88. Mapping: m,
  89. }
  90. return p
  91. }
  92. func checkSymbolized(locs []*profile.Location, wantSymbolized bool) error {
  93. for _, loc := range locs {
  94. if !wantSymbolized && len(loc.Line) != 0 {
  95. return fmt.Errorf("unexpected symbolization for %#x: %v", loc.Address, loc.Line)
  96. }
  97. if wantSymbolized {
  98. if len(loc.Line) != 1 {
  99. return fmt.Errorf("expected symbolization for %#x: %v", loc.Address, loc.Line)
  100. }
  101. address := loc.Address - loc.Mapping.Start
  102. if got, want := loc.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
  103. return fmt.Errorf("symbolz %#x, got %s, want %s", address, got, want)
  104. }
  105. }
  106. }
  107. return nil
  108. }
  109. func fetchSymbols(source, post string) ([]byte, error) {
  110. var symbolz string
  111. addresses := strings.Split(post, "+")
  112. // Do not symbolize the first symbol.
  113. for _, address := range addresses[1:] {
  114. symbolz += fmt.Sprintf("%s\t%s\n", address, address)
  115. }
  116. return []byte(symbolz), nil
  117. }