설명 없음

symbolz.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 symbolizes a profile using the output from the symbolz
  15. // service.
  16. package symbolz
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io"
  21. "net/url"
  22. "path"
  23. "regexp"
  24. "strconv"
  25. "strings"
  26. "github.com/google/pprof/internal/plugin"
  27. "github.com/google/pprof/profile"
  28. )
  29. var (
  30. symbolzRE = regexp.MustCompile(`(0x[[:xdigit:]]+)\s+(.*)`)
  31. )
  32. // Symbolize symbolizes profile p by parsing data returned by a
  33. // symbolz handler. syms receives the symbolz query (hex addresses
  34. // separated by '+') and returns the symbolz output in a string. If
  35. // force is false, it will only symbolize locations from mappings
  36. // not already marked as HasFunctions.
  37. func Symbolize(p *profile.Profile, force bool, sources plugin.MappingSources, syms func(string, string) ([]byte, error), ui plugin.UI) error {
  38. for _, m := range p.Mapping {
  39. if !force && m.HasFunctions {
  40. // Only check for HasFunctions as symbolz only populates function names.
  41. continue
  42. }
  43. mappingSources := sources[m.File]
  44. if m.BuildID != "" {
  45. mappingSources = append(mappingSources, sources[m.BuildID]...)
  46. }
  47. for _, source := range mappingSources {
  48. if symz := symbolz(source.Source); symz != "" {
  49. if err := symbolizeMapping(symz, int64(source.Start)-int64(m.Start), syms, m, p); err != nil {
  50. return err
  51. }
  52. m.HasFunctions = true
  53. break
  54. }
  55. }
  56. }
  57. return nil
  58. }
  59. // symbolz returns the corresponding symbolz source for a profile URL.
  60. func symbolz(source string) string {
  61. if url, err := url.Parse(source); err == nil && url.Host != "" {
  62. if strings.Contains(url.Path, "/debug/pprof/") {
  63. url.Path = path.Clean(url.Path + "/../symbol")
  64. } else {
  65. url.Path = "/symbolz"
  66. }
  67. url.RawQuery = ""
  68. return url.String()
  69. }
  70. return ""
  71. }
  72. // symbolizeMapping symbolizes locations belonging to a Mapping by querying
  73. // a symbolz handler. An offset is applied to all addresses to take care of
  74. // normalization occurred for merged Mappings.
  75. func symbolizeMapping(source string, offset int64, syms func(string, string) ([]byte, error), m *profile.Mapping, p *profile.Profile) error {
  76. // Construct query of addresses to symbolize.
  77. var a []string
  78. for _, l := range p.Location {
  79. if l.Mapping == m && l.Address != 0 && len(l.Line) == 0 {
  80. // Compensate for normalization.
  81. addr := int64(l.Address) + offset
  82. if addr < 0 {
  83. return fmt.Errorf("unexpected negative adjusted address, mapping %v source %d, offset %d", l.Mapping, l.Address, offset)
  84. }
  85. a = append(a, fmt.Sprintf("%#x", addr))
  86. }
  87. }
  88. if len(a) == 0 {
  89. // No addresses to symbolize.
  90. return nil
  91. }
  92. lines := make(map[uint64]profile.Line)
  93. functions := make(map[string]*profile.Function)
  94. b, err := syms(source, strings.Join(a, "+"))
  95. if err != nil {
  96. return err
  97. }
  98. buf := bytes.NewBuffer(b)
  99. for {
  100. l, err := buf.ReadString('\n')
  101. if err != nil {
  102. if err == io.EOF {
  103. break
  104. }
  105. return err
  106. }
  107. if symbol := symbolzRE.FindStringSubmatch(l); len(symbol) == 3 {
  108. addr, err := strconv.ParseInt(symbol[1], 0, 64)
  109. if err != nil {
  110. return fmt.Errorf("unexpected parse failure %s: %v", symbol[1], err)
  111. }
  112. if addr < 0 {
  113. return fmt.Errorf("unexpected negative adjusted address, source %s, offset %d", symbol[1], offset)
  114. }
  115. // Reapply offset expected by the profile.
  116. addr -= offset
  117. name := symbol[2]
  118. fn := functions[name]
  119. if fn == nil {
  120. fn = &profile.Function{
  121. ID: uint64(len(p.Function) + 1),
  122. Name: name,
  123. SystemName: name,
  124. }
  125. functions[name] = fn
  126. p.Function = append(p.Function, fn)
  127. }
  128. lines[uint64(addr)] = profile.Line{Function: fn}
  129. }
  130. }
  131. for _, l := range p.Location {
  132. if l.Mapping != m {
  133. continue
  134. }
  135. if line, ok := lines[l.Address]; ok {
  136. l.Line = []profile.Line{line}
  137. }
  138. }
  139. return nil
  140. }