Nenhuma descrição

symbolz_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. } {
  33. if got := symbolz(try); got != want {
  34. t.Errorf(`symbolz(%s)=%s, want "%s"`, try, got, want)
  35. }
  36. }
  37. }
  38. func TestSymbolize(t *testing.T) {
  39. m := []*profile.Mapping{
  40. {
  41. ID: 1,
  42. Start: 0x1000,
  43. Limit: 0x5000,
  44. BuildID: "buildid",
  45. },
  46. }
  47. p := &profile.Profile{
  48. Location: []*profile.Location{
  49. {ID: 1, Mapping: m[0], Address: 0x1000},
  50. {ID: 2, Mapping: m[0], Address: 0x2000},
  51. {ID: 3, Mapping: m[0], Address: 0x3000},
  52. {ID: 4, Mapping: m[0], Address: 0x4000},
  53. },
  54. Mapping: m,
  55. }
  56. s := plugin.MappingSources{
  57. "buildid": []struct {
  58. Source string
  59. Start uint64
  60. }{
  61. {Source: "http://localhost:80/profilez"},
  62. },
  63. }
  64. if err := Symbolize(s, fetchSymbols, p, &proftest.TestUI{T: t}); err != nil {
  65. t.Errorf("symbolz: %v", err)
  66. }
  67. if l := p.Location[0]; len(l.Line) != 0 {
  68. t.Errorf("unexpected symbolization for %#x: %v", l.Address, l.Line)
  69. }
  70. for _, l := range p.Location[1:] {
  71. if len(l.Line) != 1 {
  72. t.Errorf("failed to symbolize %#x", l.Address)
  73. continue
  74. }
  75. address := l.Address - l.Mapping.Start
  76. if got, want := l.Line[0].Function.Name, fmt.Sprintf("%#x", address); got != want {
  77. t.Errorf("symbolz %#x, got %s, want %s", address, got, want)
  78. }
  79. }
  80. }
  81. func fetchSymbols(source, post string) ([]byte, error) {
  82. var symbolz string
  83. addresses := strings.Split(post, "+")
  84. // Do not symbolize the first symbol.
  85. for _, address := range addresses[1:] {
  86. symbolz += fmt.Sprintf("%s\t%s\n", address, address)
  87. }
  88. return []byte(symbolz), nil
  89. }