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.
 
 
 

201 lines
5.4 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 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 symbolz
  33. // handler. syms receives the symbolz query (hex addresses separated by '+')
  34. // and returns the symbolz output in a string. If force is false, it will only
  35. // symbolize locations from mappings not already marked as HasFunctions. Never
  36. // attempts symbolization of addresses from unsymbolizable system
  37. // mappings as those may look negative - e.g. "[vsyscall]".
  38. func Symbolize(p *profile.Profile, force bool, sources plugin.MappingSources, syms func(string, string) ([]byte, error), ui plugin.UI) error {
  39. for _, m := range p.Mapping {
  40. if !force && m.HasFunctions {
  41. // Only check for HasFunctions as symbolz only populates function names.
  42. continue
  43. }
  44. // Skip well-known system mappings.
  45. if m.Unsymbolizable() {
  46. continue
  47. }
  48. mappingSources := sources[m.File]
  49. if m.BuildID != "" {
  50. mappingSources = append(mappingSources, sources[m.BuildID]...)
  51. }
  52. for _, source := range mappingSources {
  53. if symz := symbolz(source.Source); symz != "" {
  54. if err := symbolizeMapping(symz, int64(source.Start)-int64(m.Start), syms, m, p); err != nil {
  55. return err
  56. }
  57. m.HasFunctions = true
  58. break
  59. }
  60. }
  61. }
  62. return nil
  63. }
  64. // hasGperftoolsSuffix checks whether path ends with one of the suffixes listed in
  65. // pprof_remote_servers.html from the gperftools distribution
  66. func hasGperftoolsSuffix(path string) bool {
  67. suffixes := []string{
  68. "/pprof/heap",
  69. "/pprof/growth",
  70. "/pprof/profile",
  71. "/pprof/pmuprofile",
  72. "/pprof/contention",
  73. }
  74. for _, s := range suffixes {
  75. if strings.HasSuffix(path, s) {
  76. return true
  77. }
  78. }
  79. return false
  80. }
  81. // symbolz returns the corresponding symbolz source for a profile URL.
  82. func symbolz(source string) string {
  83. if url, err := url.Parse(source); err == nil && url.Host != "" {
  84. // All paths in the net/http/pprof Go package contain /debug/pprof/
  85. if strings.Contains(url.Path, "/debug/pprof/") || hasGperftoolsSuffix(url.Path) {
  86. url.Path = path.Clean(url.Path + "/../symbol")
  87. } else {
  88. url.Path = "/symbolz"
  89. }
  90. url.RawQuery = ""
  91. return url.String()
  92. }
  93. return ""
  94. }
  95. // symbolizeMapping symbolizes locations belonging to a Mapping by querying
  96. // a symbolz handler. An offset is applied to all addresses to take care of
  97. // normalization occurred for merged Mappings.
  98. func symbolizeMapping(source string, offset int64, syms func(string, string) ([]byte, error), m *profile.Mapping, p *profile.Profile) error {
  99. // Construct query of addresses to symbolize.
  100. var a []string
  101. for _, l := range p.Location {
  102. if l.Mapping == m && l.Address != 0 && len(l.Line) == 0 {
  103. // Compensate for normalization.
  104. addr, overflow := adjust(l.Address, offset)
  105. if overflow {
  106. return fmt.Errorf("cannot adjust address %d by %d, it would overflow (mapping %v)", l.Address, offset, l.Mapping)
  107. }
  108. a = append(a, fmt.Sprintf("%#x", addr))
  109. }
  110. }
  111. if len(a) == 0 {
  112. // No addresses to symbolize.
  113. return nil
  114. }
  115. lines := make(map[uint64]profile.Line)
  116. functions := make(map[string]*profile.Function)
  117. b, err := syms(source, strings.Join(a, "+"))
  118. if err != nil {
  119. return err
  120. }
  121. buf := bytes.NewBuffer(b)
  122. for {
  123. l, err := buf.ReadString('\n')
  124. if err != nil {
  125. if err == io.EOF {
  126. break
  127. }
  128. return err
  129. }
  130. if symbol := symbolzRE.FindStringSubmatch(l); len(symbol) == 3 {
  131. origAddr, err := strconv.ParseUint(symbol[1], 0, 64)
  132. if err != nil {
  133. return fmt.Errorf("unexpected parse failure %s: %v", symbol[1], err)
  134. }
  135. // Reapply offset expected by the profile.
  136. addr, overflow := adjust(origAddr, -offset)
  137. if overflow {
  138. return fmt.Errorf("cannot adjust symbolz address %d by %d, it would overflow", origAddr, -offset)
  139. }
  140. name := symbol[2]
  141. fn := functions[name]
  142. if fn == nil {
  143. fn = &profile.Function{
  144. ID: uint64(len(p.Function) + 1),
  145. Name: name,
  146. SystemName: name,
  147. }
  148. functions[name] = fn
  149. p.Function = append(p.Function, fn)
  150. }
  151. lines[addr] = profile.Line{Function: fn}
  152. }
  153. }
  154. for _, l := range p.Location {
  155. if l.Mapping != m {
  156. continue
  157. }
  158. if line, ok := lines[l.Address]; ok {
  159. l.Line = []profile.Line{line}
  160. }
  161. }
  162. return nil
  163. }
  164. // adjust shifts the specified address by the signed offset. It returns the
  165. // adjusted address. It signals that the address cannot be adjusted without an
  166. // overflow by returning true in the second return value.
  167. func adjust(addr uint64, offset int64) (uint64, bool) {
  168. adj := uint64(int64(addr) + offset)
  169. if offset < 0 {
  170. if adj >= addr {
  171. return 0, true
  172. }
  173. } else {
  174. if adj < addr {
  175. return 0, true
  176. }
  177. }
  178. return adj, false
  179. }