Ei kuvausta

symbolz.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, "/") {
  63. if dir := path.Dir(url.Path); dir == "/debug/pprof" {
  64. // For Go language profile handlers in net/http/pprof package.
  65. url.Path = "/debug/pprof/symbol"
  66. } else {
  67. url.Path = "/symbolz"
  68. }
  69. url.RawQuery = ""
  70. return url.String()
  71. }
  72. }
  73. return ""
  74. }
  75. // symbolizeMapping symbolizes locations belonging to a Mapping by querying
  76. // a symbolz handler. An offset is applied to all addresses to take care of
  77. // normalization occured for merged Mappings.
  78. func symbolizeMapping(source string, offset int64, syms func(string, string) ([]byte, error), m *profile.Mapping, p *profile.Profile) error {
  79. // Construct query of addresses to symbolize.
  80. var a []string
  81. for _, l := range p.Location {
  82. if l.Mapping == m && l.Address != 0 && len(l.Line) == 0 {
  83. // Compensate for normalization.
  84. addr := int64(l.Address) + offset
  85. if addr < 0 {
  86. return fmt.Errorf("unexpected negative adjusted address, mapping %v source %d, offset %d", l.Mapping, l.Address, offset)
  87. }
  88. a = append(a, fmt.Sprintf("%#x", addr))
  89. }
  90. }
  91. if len(a) == 0 {
  92. // No addresses to symbolize.
  93. return nil
  94. }
  95. lines := make(map[uint64]profile.Line)
  96. functions := make(map[string]*profile.Function)
  97. b, err := syms(source, strings.Join(a, "+"))
  98. if err != nil {
  99. return err
  100. }
  101. buf := bytes.NewBuffer(b)
  102. for {
  103. l, err := buf.ReadString('\n')
  104. if err != nil {
  105. if err == io.EOF {
  106. break
  107. }
  108. return err
  109. }
  110. if symbol := symbolzRE.FindStringSubmatch(l); len(symbol) == 3 {
  111. addr, err := strconv.ParseInt(symbol[1], 0, 64)
  112. if err != nil {
  113. return fmt.Errorf("unexpected parse failure %s: %v", symbol[1], err)
  114. }
  115. if addr < 0 {
  116. return fmt.Errorf("unexpected negative adjusted address, source %s, offset %d", symbol[1], offset)
  117. }
  118. // Reapply offset expected by the profile.
  119. addr -= offset
  120. name := symbol[2]
  121. fn := functions[name]
  122. if fn == nil {
  123. fn = &profile.Function{
  124. ID: uint64(len(p.Function) + 1),
  125. Name: name,
  126. SystemName: name,
  127. }
  128. functions[name] = fn
  129. p.Function = append(p.Function, fn)
  130. }
  131. lines[uint64(addr)] = profile.Line{Function: fn}
  132. }
  133. }
  134. for _, l := range p.Location {
  135. if l.Mapping != m {
  136. continue
  137. }
  138. if line, ok := lines[l.Address]; ok {
  139. l.Line = []profile.Line{line}
  140. }
  141. }
  142. return nil
  143. }