Sin descripción

symbolz.go 4.2KB

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