| 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 | |
| 15 | // Package symbolz symbolizes a profile using the output from the symbolz |
| 16 | // service. |
| 17 | package symbolzscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/ |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 22 | "ioscip-go gomod github.com/golang/go/src go1.24.0 io/" |
| 23 | "net/urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/" |
| 24 | "pathscip-go gomod github.com/golang/go/src go1.24.0 path/" |
| 25 | "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/" |
| 26 | "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/" |
| 27 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 28 | |
| 29 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 30 | "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/" |
| 31 | ) |
| 32 | |
| 33 | var ( |
| 34 | symbolzREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolzRE. = regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.24.0 regexp/MustCompile().(`(0x[[:xdigit:]]+)\s+(.*)`) |
| 35 | ) |
| 36 | |
| 37 | // Symbolize symbolizes profile p by parsing data returned by a symbolz |
| 38 | // handler. syms receives the symbolz query (hex addresses separated by '+') |
| 39 | // and returns the symbolz output in a string. If force is false, it will only |
| 40 | // symbolize locations from mappings not already marked as HasFunctions. Does |
| 41 | // not skip unsymbolizable files since the symbolz handler can be flexible |
| 42 | // enough to handle some of those cases such as JIT locations in //anon. |
| 43 | func Symbolizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/Symbolize().(p *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#, force bool, sources pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.MappingSourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/MappingSources#, syms func(string, string) ([]byte, error), ui pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#) error { |
| 44 | for _, m := range p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping. { |
| 45 | if !force && m.HasFunctionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#HasFunctions. { |
| 46 | // Only check for HasFunctions as symbolz only populates function names. |
| 47 | continue |
| 48 | } |
| 49 | mappingSources := sources[m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.] |
| 50 | if m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. != "" { |
| 51 | mappingSources = append(mappingSources, sources[m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID.]...) |
| 52 | } |
| 53 | for _, source := range mappingSources { |
| 54 | if symz := symbolzscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolz().(source.Source); symz != "" { |
| 55 | if err := symbolizeMappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolizeMapping().(symz, int64(source.Start)-int64(m.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Start.), syms, m, p); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | m.HasFunctionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#HasFunctions. = true |
| 59 | break |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // hasGperftoolsSuffix checks whether path ends with one of the suffixes listed in |
| 68 | // pprof_remote_servers.html from the gperftools distribution |
| 69 | func hasGperftoolsSuffixscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/hasGperftoolsSuffix().(path string) bool { |
| 70 | suffixes := []string{ |
| 71 | "/pprof/heap", |
| 72 | "/pprof/growth", |
| 73 | "/pprof/profile", |
| 74 | "/pprof/pmuprofile", |
| 75 | "/pprof/contention", |
| 76 | } |
| 77 | for _, s := range suffixes { |
| 78 | if stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasSuffix().(path, s) { |
| 79 | return true |
| 80 | } |
| 81 | } |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | // symbolz returns the corresponding symbolz source for a profile URL. |
| 86 | func symbolzscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolz().(source string) string { |
| 87 | if url, err := urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Parse().(source); err == nil && url.Hostscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Host. != "" { |
| 88 | // All paths in the net/http/pprof Go package contain /debug/pprof/ |
| 89 | if stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.24.0 strings/Contains().(url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path., "/debug/pprof/") || hasGperftoolsSuffixscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/hasGperftoolsSuffix().(url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path.) { |
| 90 | url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path. = pathscip-go gomod github.com/golang/go/src go1.24.0 path/.Cleanscip-go gomod github.com/golang/go/src go1.24.0 path/Clean().(url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path. + "/../symbol") |
| 91 | } else { |
| 92 | url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path. = pathscip-go gomod github.com/golang/go/src go1.24.0 path/.Cleanscip-go gomod github.com/golang/go/src go1.24.0 path/Clean().(url.Pathscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Path. + "/../symbolz") |
| 93 | } |
| 94 | url.RawQueryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#RawQuery. = "" |
| 95 | return url.Stringscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#String().() |
| 96 | } |
| 97 | |
| 98 | return "" |
| 99 | } |
| 100 | |
| 101 | // symbolizeMapping symbolizes locations belonging to a Mapping by querying |
| 102 | // a symbolz handler. An offset is applied to all addresses to take care of |
| 103 | // normalization occurred for merged Mappings. |
| 104 | func symbolizeMappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolizeMapping().(source string, offset int64, syms func(string, string) ([]byte, error), m *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#, p *profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) error { |
| 105 | // Construct query of addresses to symbolize. |
| 106 | var a []string |
| 107 | for _, l := range p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. { |
| 108 | if l.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping. == m && l.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address. != 0 && len(l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) == 0 { |
| 109 | // Compensate for normalization. |
| 110 | addr, overflow := adjustscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/adjust().(l.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address., offset) |
| 111 | if overflow { |
| 112 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("cannot adjust address %d by %d, it would overflow (mapping %v)", l.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address., offset, l.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.) |
| 113 | } |
| 114 | a = append(a, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%#x", addr)) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if len(a) == 0 { |
| 119 | // No addresses to symbolize. |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | lines := make(map[uint64]profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#) |
| 124 | functions := make(map[string]*profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#) |
| 125 | |
| 126 | b, err := syms(source, stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.0 strings/Join().(a, "+")) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | buf := bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/NewBuffer().(b) |
| 132 | for { |
| 133 | l, err := buf.ReadStringscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#ReadString().('\n') |
| 134 | |
| 135 | if err != nil { |
| 136 | if err == ioscip-go gomod github.com/golang/go/src go1.24.0 io/.EOFscip-go gomod github.com/golang/go/src go1.24.0 io/EOF. { |
| 137 | break |
| 138 | } |
| 139 | return err |
| 140 | } |
| 141 | |
| 142 | if symbol := symbolzREscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/symbolzRE..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#FindStringSubmatch().(l); len(symbol) == 3 { |
| 143 | origAddr, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.24.0 strconv/ParseUint().(symbol[1], 0, 64) |
| 144 | if err != nil { |
| 145 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("unexpected parse failure %s: %v", symbol[1], err) |
| 146 | } |
| 147 | // Reapply offset expected by the profile. |
| 148 | addr, overflow := adjustscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/adjust().(origAddr, -offset) |
| 149 | if overflow { |
| 150 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("cannot adjust symbolz address %d by %d, it would overflow", origAddr, -offset) |
| 151 | } |
| 152 | |
| 153 | name := symbol[2] |
| 154 | fn := functions[name] |
| 155 | if fn == nil { |
| 156 | fn = &profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#{ |
| 157 | IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#ID.: uint64(len(p.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Function.) + 1), |
| 158 | Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.: name, |
| 159 | SystemNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#SystemName.: name, |
| 160 | } |
| 161 | functions[name] = fn |
| 162 | p.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Function. = append(p.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Function., fn) |
| 163 | } |
| 164 | |
| 165 | lines[addr] = profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#{Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.: fn} |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | for _, l := range p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. { |
| 170 | if l.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping. != m { |
| 171 | continue |
| 172 | } |
| 173 | if line, ok := lines[l.Addressscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Address.]; ok { |
| 174 | l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = []profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#{line} |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | // adjust shifts the specified address by the signed offset. It returns the |
| 182 | // adjusted address. It signals that the address cannot be adjusted without an |
| 183 | // overflow by returning true in the second return value. |
| 184 | func adjustscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/symbolz`/adjust().(addr uint64, offset int64) (uint64, bool) { |
| 185 | adj := uint64(int64(addr) + offset) |
| 186 | if offset < 0 { |
| 187 | if adj >= addr { |
| 188 | return 0, true |
| 189 | } |
| 190 | } else { |
| 191 | if adj < addr { |
| 192 | return 0, true |
| 193 | } |
| 194 | } |
| 195 | return adj, false |
| 196 | } |