| 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 driverscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 20 | "ioscip-go gomod github.com/golang/go/src go1.24.0 io/" |
| 21 | "net/httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/" |
| 22 | "net/urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.24.0 os/" |
| 24 | "os/execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/" |
| 25 | "path/filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/" |
| 26 | "runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/" |
| 27 | "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/" |
| 28 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 29 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 30 | "timescip-go gomod github.com/golang/go/src go1.24.0 time/" |
| 31 | |
| 32 | "github.com/google/pprof/internal/measurementscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/measurement`/" |
| 33 | "github.com/google/pprof/internal/pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/" |
| 34 | "github.com/google/pprof/profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/" |
| 35 | ) |
| 36 | |
| 37 | // fetchProfiles fetches and symbolizes the profiles specified by s. |
| 38 | // It will merge all the profiles it is able to retrieve, even if |
| 39 | // there are some failures. It will return an error if it is unable to |
| 40 | // fetch any profiles. |
| 41 | func fetchProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/fetchProfiles().(s *sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#, o *pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Optionsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#) (*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) { |
| 42 | sources := make([]profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#, 0, len(s.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Sources.)) |
| 43 | for _, src := range s.Sourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Sources. { |
| 44 | sources = append(sources, profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#{ |
| 45 | addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#addr.: src, |
| 46 | sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#source.: s, |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | bases := make([]profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#, 0, len(s.Basescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Base.)) |
| 51 | for _, src := range s.Basescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Base. { |
| 52 | bases = append(bases, profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#{ |
| 53 | addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#addr.: src, |
| 54 | sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#source.: s, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | p, pbase, m, mbase, save, err := grabSourcesAndBasesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/grabSourcesAndBases().(sources, bases, o.Fetchscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#Fetch., o.Objscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#Obj., o.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#UI., o.HTTPTransportscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#HTTPTransport.) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | |
| 63 | if pbase != nil { |
| 64 | if s.DiffBasescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#DiffBase. { |
| 65 | pbase.SetLabelscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SetLabel().("pprof::base", []string{"true"}) |
| 66 | } |
| 67 | if s.Normalizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Normalize. { |
| 68 | err := p.Normalizescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Normalize().(pbase) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | } |
| 73 | pbase.Scalescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Scale().(-1) |
| 74 | p, m, err = combineProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/combineProfiles().([]*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#{p, pbase}, []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#{m, mbase}) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if s.AllFramesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#AllFrames. { |
| 81 | p.DropFramesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#DropFrames. = "" |
| 82 | p.KeepFramesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#KeepFrames. = "" |
| 83 | } |
| 84 | |
| 85 | // Symbolize the merged profile. |
| 86 | if err := o.Symscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#Sym..Symbolizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Symbolizer#Symbolize.(s.Symbolizescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Symbolize., m, p); err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | p.RemoveUninterestingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#RemoveUninteresting().() |
| 90 | unsourceMappingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/unsourceMappings().(p) |
| 91 | |
| 92 | if s.Commentscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Comment. != "" { |
| 93 | p.Commentsscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Comments. = append(p.Commentsscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Comments., s.Commentscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Comment.) |
| 94 | } |
| 95 | |
| 96 | // Save a copy of the merged profile if there is at least one remote source. |
| 97 | if save { |
| 98 | dir, err := setTmpDirscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setTmpDir().(o.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#UI.) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | |
| 103 | prefix := "pprof." |
| 104 | if len(p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.) > 0 && p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.[0].Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. != "" { |
| 105 | prefix += filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Base().(p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.[0].Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) + "." |
| 106 | } |
| 107 | for _, s := range p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType. { |
| 108 | prefix += s.Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type. + "." |
| 109 | } |
| 110 | |
| 111 | tempFile, err := newTempFilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/newTempFile().(dir, prefix, ".pb.gz") |
| 112 | if err == nil { |
| 113 | if err = p.Writescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Write().(tempFile); err == nil { |
| 114 | o.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#UI..PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.("Saved profile in ", tempFile.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().()) |
| 115 | } |
| 116 | } |
| 117 | if err != nil { |
| 118 | o.UIscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Options#UI..PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.("Could not save profile: ", err) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if err := p.CheckValidscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#CheckValid().(); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | |
| 126 | return p, nil |
| 127 | } |
| 128 | |
| 129 | func grabSourcesAndBasesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/grabSourcesAndBases().(sources, bases []profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#, fetch pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Fetcherscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Fetcher#, obj pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.ObjToolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#, 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#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (*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#, *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#, 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#, 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#, bool, error) { |
| 130 | wg := syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#{} |
| 131 | wg.Addscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Add().(2) |
| 132 | var psrc, pbase *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# |
| 133 | var msrc, mbase 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# |
| 134 | var savesrc, savebase bool |
| 135 | var errsrc, errbase error |
| 136 | var countsrc, countbase int |
| 137 | go func() { |
| 138 | defer wg.Donescip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Done().() |
| 139 | psrc, msrc, savesrc, countsrc, errsrc = chunkedGrabscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/chunkedGrab().(sources, fetch, obj, ui, tr) |
| 140 | }() |
| 141 | go func() { |
| 142 | defer wg.Donescip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Done().() |
| 143 | pbase, mbase, savebase, countbase, errbase = chunkedGrabscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/chunkedGrab().(bases, fetch, obj, ui, tr) |
| 144 | }() |
| 145 | wg.Waitscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Wait().() |
| 146 | save := savesrc || savebase |
| 147 | |
| 148 | if errsrc != nil { |
| 149 | return nil, nil, nil, nil, false, 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().("problem fetching source profiles: %v", errsrc) |
| 150 | } |
| 151 | if errbase != nil { |
| 152 | return nil, nil, nil, nil, false, 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().("problem fetching base profiles: %v,", errbase) |
| 153 | } |
| 154 | if countsrc == 0 { |
| 155 | return nil, nil, nil, nil, false, 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().("failed to fetch any source profiles") |
| 156 | } |
| 157 | if countbase == 0 && len(bases) > 0 { |
| 158 | return nil, nil, nil, nil, false, 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().("failed to fetch any base profiles") |
| 159 | } |
| 160 | if want, got := len(sources), countsrc; want != got { |
| 161 | ui.PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.(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().("Fetched %d source profiles out of %d", got, want)) |
| 162 | } |
| 163 | if want, got := len(bases), countbase; want != got { |
| 164 | ui.PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.(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().("Fetched %d base profiles out of %d", got, want)) |
| 165 | } |
| 166 | |
| 167 | return psrc, pbase, msrc, mbase, save, nil |
| 168 | } |
| 169 | |
| 170 | // chunkedGrab fetches the profiles described in source and merges them into |
| 171 | // a single profile. It fetches a chunk of profiles concurrently, with a maximum |
| 172 | // chunk size to limit its memory usage. |
| 173 | func chunkedGrabscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/chunkedGrab().(sources []profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#, fetch pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Fetcherscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Fetcher#, obj pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.ObjToolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#, 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#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (*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#, 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#, bool, int, error) { |
| 174 | const chunkSize = 128 |
| 175 | |
| 176 | var 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# |
| 177 | var msrc 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# |
| 178 | var save bool |
| 179 | var count int |
| 180 | |
| 181 | for start := 0; start < len(sources); start += chunkSize { |
| 182 | end := min(start+chunkSize, len(sources)) |
| 183 | chunkP, chunkMsrc, chunkSave, chunkCount, chunkErr := concurrentGrabscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/concurrentGrab().(sources[start:end], fetch, obj, ui, tr) |
| 184 | switch { |
| 185 | case chunkErr != nil: |
| 186 | return nil, nil, false, 0, chunkErr |
| 187 | case chunkP == nil: |
| 188 | continue |
| 189 | case p == nil: |
| 190 | p, msrc, save, count = chunkP, chunkMsrc, chunkSave, chunkCount |
| 191 | default: |
| 192 | p, msrc, chunkErr = combineProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/combineProfiles().([]*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#{p, chunkP}, []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#{msrc, chunkMsrc}) |
| 193 | if chunkErr != nil { |
| 194 | return nil, nil, false, 0, chunkErr |
| 195 | } |
| 196 | if chunkSave { |
| 197 | save = true |
| 198 | } |
| 199 | count += chunkCount |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return p, msrc, save, count, nil |
| 204 | } |
| 205 | |
| 206 | // concurrentGrab fetches multiple profiles concurrently |
| 207 | func concurrentGrabscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/concurrentGrab().(sources []profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#, fetch pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Fetcherscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Fetcher#, obj pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.ObjToolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#, 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#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (*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#, 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#, bool, int, error) { |
| 208 | wg := syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#{} |
| 209 | wg.Addscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Add().(len(sources)) |
| 210 | for i := range sources { |
| 211 | go func(s *profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#) { |
| 212 | defer wg.Donescip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Done().() |
| 213 | s.pscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#p., s.msrcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#msrc., s.remotescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#remote., s.errscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#err. = grabProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/grabProfile().(s.sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#source., s.addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#addr., fetch, obj, ui, tr) |
| 214 | }(&sources[i]) |
| 215 | } |
| 216 | wg.Waitscip-go gomod github.com/golang/go/src go1.24.0 sync/WaitGroup#Wait().() |
| 217 | |
| 218 | var save bool |
| 219 | profiles := make([]*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#, 0, len(sources)) |
| 220 | msrcs := make([]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#, 0, len(sources)) |
| 221 | for i := range sources { |
| 222 | s := &sources[i] |
| 223 | if err := s.errscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#err.; err != nil { |
| 224 | ui.PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.(s.addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#addr. + ": " + err.Error()) |
| 225 | continue |
| 226 | } |
| 227 | save = save || s.remotescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#remote. |
| 228 | profiles = append(profiles, s.pscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#p.) |
| 229 | msrcs = append(msrcs, s.msrcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#msrc.) |
| 230 | *s = profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#{} |
| 231 | } |
| 232 | |
| 233 | if len(profiles) == 0 { |
| 234 | return nil, nil, false, 0, nil |
| 235 | } |
| 236 | |
| 237 | p, msrc, err := combineProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/combineProfiles().(profiles, msrcs) |
| 238 | if err != nil { |
| 239 | return nil, nil, false, 0, err |
| 240 | } |
| 241 | return p, msrc, save, len(profiles), nil |
| 242 | } |
| 243 | |
| 244 | func combineProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/combineProfiles().(profiles []*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#, msrcs []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#) (*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#, 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#, error) { |
| 245 | // Merge profiles. |
| 246 | // |
| 247 | // The merge call below only treats exactly matching sample type lists as |
| 248 | // compatible and will fail otherwise. Make the profiles' sample types |
| 249 | // compatible for the merge, see CompatibilizeSampleTypes() doc for details. |
| 250 | if err := profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.CompatibilizeSampleTypesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/CompatibilizeSampleTypes().(profiles); err != nil { |
| 251 | return nil, nil, err |
| 252 | } |
| 253 | if err := measurementscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/measurement`/.ScaleProfilesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/measurement`/ScaleProfiles().(profiles); err != nil { |
| 254 | return nil, nil, err |
| 255 | } |
| 256 | |
| 257 | // Avoid expensive work for the common case of a single profile/src. |
| 258 | if len(profiles) == 1 && len(msrcs) == 1 { |
| 259 | return profiles[0], msrcs[0], nil |
| 260 | } |
| 261 | |
| 262 | p, err := profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Mergescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Merge().(profiles) |
| 263 | if err != nil { |
| 264 | return nil, nil, err |
| 265 | } |
| 266 | |
| 267 | // Combine mapping sources. |
| 268 | msrc := make(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#) |
| 269 | for _, ms := range msrcs { |
| 270 | for m, s := range ms { |
| 271 | msrc[m] = append(msrc[m], s...) |
| 272 | } |
| 273 | } |
| 274 | return p, msrc, nil |
| 275 | } |
| 276 | |
| 277 | type profileSourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource# struct { |
| 278 | addrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#addr. string |
| 279 | sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#source. *sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source# |
| 280 | |
| 281 | pscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#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# |
| 282 | msrcscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#msrc. 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# |
| 283 | remotescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#remote. bool |
| 284 | errscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/profileSource#err. error |
| 285 | } |
| 286 | |
| 287 | func homeEnvscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/homeEnv().() string { |
| 288 | switch runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.24.0 runtime/GOOS. { |
| 289 | case "windows": |
| 290 | return "USERPROFILE" |
| 291 | case "plan9": |
| 292 | return "home" |
| 293 | default: |
| 294 | return "HOME" |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // setTmpDir prepares the directory to use to save profiles retrieved |
| 299 | // remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof, and, if |
| 300 | // $HOME is not set, falls back to os.TempDir(). |
| 301 | func setTmpDirscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/setTmpDir().(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#) (string, error) { |
| 302 | var dirs []string |
| 303 | if profileDir := osscip-go gomod github.com/golang/go/src go1.24.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.24.0 os/Getenv().("PPROF_TMPDIR"); profileDir != "" { |
| 304 | dirs = append(dirs, profileDir) |
| 305 | } |
| 306 | if homeDir := osscip-go gomod github.com/golang/go/src go1.24.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.24.0 os/Getenv().(homeEnvscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/homeEnv().()); homeDir != "" { |
| 307 | dirs = append(dirs, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(homeDir, "pprof")) |
| 308 | } |
| 309 | dirs = append(dirs, osscip-go gomod github.com/golang/go/src go1.24.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.24.0 os/TempDir().()) |
| 310 | for _, tmpDir := range dirs { |
| 311 | if err := osscip-go gomod github.com/golang/go/src go1.24.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.24.0 os/MkdirAll().(tmpDir, 0755); err != nil { |
| 312 | ui.PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.("Could not use temp dir ", tmpDir, ": ", err.Error()) |
| 313 | continue |
| 314 | } |
| 315 | return tmpDir, nil |
| 316 | } |
| 317 | 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().("failed to identify temp dir") |
| 318 | } |
| 319 | |
| 320 | const testSourceAddressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/testSourceAddress. = "pproftest.local" |
| 321 | |
| 322 | // grabProfile fetches a profile. Returns the profile, sources for the |
| 323 | // profile mappings, a bool indicating if the profile was fetched |
| 324 | // remotely, and an error. |
| 325 | func grabProfilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/grabProfile().(s *sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#, source string, fetcher pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.Fetcherscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Fetcher#, obj pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.ObjToolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#, 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#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (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#, msrc 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#, remote bool, err error) { |
| 326 | var src string |
| 327 | duration, timeout := timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#(s.Secondsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Seconds.)*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second., timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#(s.Timeoutscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#Timeout.)*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second. |
| 328 | if fetcher != nil { |
| 329 | p, src, err = fetcher.Fetchscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/Fetcher#Fetch.(source, duration, timeout) |
| 330 | if err != nil { |
| 331 | return |
| 332 | } |
| 333 | } |
| 334 | if err != nil || p == nil { |
| 335 | // Fetch the profile over HTTP or from a file. |
| 336 | p, src, err = fetchscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/fetch().(source, duration, timeout, ui, tr) |
| 337 | if err != nil { |
| 338 | return |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if err = p.CheckValidscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#CheckValid().(); err != nil { |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | // Update the binary locations from command line and paths. |
| 347 | locateBinariesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/locateBinaries().(p, s, obj, ui) |
| 348 | |
| 349 | // Collect the source URL for all mappings. |
| 350 | if src != "" { |
| 351 | msrc = collectMappingSourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/collectMappingSources().(p, src) |
| 352 | remote = true |
| 353 | if stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(src, "http://"+testSourceAddressscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/testSourceAddress.) { |
| 354 | // Treat test inputs as local to avoid saving |
| 355 | // testcase profiles during driver testing. |
| 356 | remote = false |
| 357 | } |
| 358 | } |
| 359 | return |
| 360 | } |
| 361 | |
| 362 | // collectMappingSources saves the mapping sources of a profile. |
| 363 | func collectMappingSourcesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/collectMappingSources().(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#, source string) 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# { |
| 364 | ms := 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#{} |
| 365 | for _, m := range p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping. { |
| 366 | src := struct { |
| 367 | Source string |
| 368 | Start uint64 |
| 369 | }{ |
| 370 | source, m.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Start., |
| 371 | } |
| 372 | key := m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. |
| 373 | if key == "" { |
| 374 | key = m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. |
| 375 | } |
| 376 | if key == "" { |
| 377 | // If there is no build id or source file, use the source as the |
| 378 | // mapping file. This will enable remote symbolization for this |
| 379 | // mapping, in particular for Go profiles on the legacy format. |
| 380 | // The source is reset back to empty string by unsourceMapping |
| 381 | // which is called after symbolization is finished. |
| 382 | m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. = source |
| 383 | key = source |
| 384 | } |
| 385 | ms[key] = append(ms[key], src) |
| 386 | } |
| 387 | return ms |
| 388 | } |
| 389 | |
| 390 | // unsourceMappings iterates over the mappings in a profile and replaces file |
| 391 | // set to the remote source URL by collectMappingSources back to empty string. |
| 392 | func unsourceMappingsscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/unsourceMappings().(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#) { |
| 393 | for _, m := range p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping. { |
| 394 | if m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. == "" && filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.VolumeNamescip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/VolumeName().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) == "" { |
| 395 | if u, 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().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.); err == nil && u.IsAbsscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#IsAbs().() { |
| 396 | m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. = "" |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // locateBinaries searches for binary files listed in the profile and, if found, |
| 403 | // updates the profile accordingly. |
| 404 | func locateBinariesscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/locateBinaries().(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#, s *sourcescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#, obj pluginscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/.ObjToolscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#, 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#) { |
| 405 | // Construct search path to examine |
| 406 | searchPath := osscip-go gomod github.com/golang/go/src go1.24.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.24.0 os/Getenv().("PPROF_BINARY_PATH") |
| 407 | if searchPath == "" { |
| 408 | // Use $HOME/pprof/binaries as default directory for local symbolization binaries |
| 409 | searchPath = filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(osscip-go gomod github.com/golang/go/src go1.24.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.24.0 os/Getenv().(homeEnvscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/homeEnv().()), "pprof", "binaries") |
| 410 | } |
| 411 | mapping: |
| 412 | for _, m := range p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping. { |
| 413 | var noVolumeFile string |
| 414 | var baseName string |
| 415 | var dirName string |
| 416 | if m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. != "" { |
| 417 | noVolumeFile = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/TrimPrefix().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File., filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.VolumeNamescip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/VolumeName().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.)) |
| 418 | baseName = filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Base().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) |
| 419 | dirName = filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Dir().(noVolumeFile) |
| 420 | } |
| 421 | |
| 422 | for _, path := range filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.SplitListscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/SplitList().(searchPath) { |
| 423 | var fileNames []string |
| 424 | if m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. != "" { |
| 425 | fileNames = []string{filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID., baseName)} |
| 426 | if matches, err := filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Glob().(filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID., "*")); err == nil { |
| 427 | fileNames = append(fileNames, matches...) |
| 428 | } |
| 429 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, noVolumeFile, m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID.)) // perf path format |
| 430 | // Llvm buildid protocol: the first two characters of the build id |
| 431 | // are used as directory, and the remaining part is in the filename. |
| 432 | // e.g. `/ab/cdef0123456.debug` |
| 433 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID.[:2], m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID.[2:]+".debug")) |
| 434 | } |
| 435 | if m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. != "" { |
| 436 | // Try both the basename and the full path, to support the same directory |
| 437 | // structure as the perf symfs option. |
| 438 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, baseName)) |
| 439 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, noVolumeFile)) |
| 440 | // Other locations: use the same search paths as GDB, according to |
| 441 | // https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html |
| 442 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, noVolumeFile+".debug")) |
| 443 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, dirName, ".debug", baseName+".debug")) |
| 444 | fileNames = append(fileNames, filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Join().(path, "usr", "lib", "debug", dirName, baseName+".debug")) |
| 445 | } |
| 446 | for _, name := range fileNames { |
| 447 | if f, err := obj.Openscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjTool#Open.(name, m.Startscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Start., m.Limitscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Limit., m.Offsetscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#Offset., m.KernelRelocationSymbolscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#KernelRelocationSymbol.); err == nil { |
| 448 | defer f.Closescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjFile#Close.() |
| 449 | fileBuildID := f.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/ObjFile#BuildID.() |
| 450 | if m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. != "" && m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. != fileBuildID { |
| 451 | ui.PrintErrscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#PrintErr.("Ignoring local file " + name + ": build-id mismatch (" + m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. + " != " + fileBuildID + ")") |
| 452 | } else { |
| 453 | // Explicitly do not update KernelRelocationSymbol -- |
| 454 | // the new local file name is most likely missing it. |
| 455 | m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. = name |
| 456 | continue mapping |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | if len(p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.) == 0 { |
| 463 | // If there are no mappings, add a fake mapping to attempt symbolization. |
| 464 | // This is useful for some profiles generated by the golang runtime, which |
| 465 | // do not include any mappings. Symbolization with a fake mapping will only |
| 466 | // be successful against a non-PIE binary. |
| 467 | 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#{IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#ID.: 1} |
| 468 | p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping. = []*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#{m} |
| 469 | for _, l := range p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. { |
| 470 | l.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping. = m |
| 471 | } |
| 472 | } |
| 473 | // If configured, apply executable filename override and (maybe, see below) |
| 474 | // build ID override from source. Assume the executable is the first mapping. |
| 475 | if execName, buildID := s.ExecNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#ExecName., s.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/source#BuildID.; execName != "" || buildID != "" { |
| 476 | m := p.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Mapping.[0] |
| 477 | if execName != "" { |
| 478 | // Explicitly do not update KernelRelocationSymbol -- |
| 479 | // the source override is most likely missing it. |
| 480 | m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File. = execName |
| 481 | } |
| 482 | // Only apply the build ID override if the build ID in the main mapping is |
| 483 | // missing. Overwriting the build ID in case it's present is very likely a |
| 484 | // wrong thing to do so we refuse to do that. |
| 485 | if buildID != "" && m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. == "" { |
| 486 | m.BuildIDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#BuildID. = buildID |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // fetch fetches a profile from source, within the timeout specified, |
| 492 | // producing messages through the ui. It returns the profile and the |
| 493 | // url of the actual source of the profile for remote profiles. |
| 494 | func fetchscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/fetch().(source string, duration, timeout timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#, 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#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (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#, src string, err error) { |
| 495 | var f ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.24.0 io/ReadCloser# |
| 496 | |
| 497 | // First determine whether the source is a file, if not, it will be treated as a URL. |
| 498 | if _, err = osscip-go gomod github.com/golang/go/src go1.24.0 os/.Statscip-go gomod github.com/golang/go/src go1.24.0 os/Stat().(source); err == nil { |
| 499 | if isPerfFilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/isPerfFile().(source) { |
| 500 | f, err = convertPerfDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/convertPerfData().(source, ui) |
| 501 | } else { |
| 502 | f, err = osscip-go gomod github.com/golang/go/src go1.24.0 os/.Openscip-go gomod github.com/golang/go/src go1.24.0 os/Open().(source) |
| 503 | } |
| 504 | } else { |
| 505 | sourceURL, timeout := adjustURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/adjustURL().(source, duration, timeout) |
| 506 | if sourceURL != "" { |
| 507 | ui.Printscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#Print.("Fetching profile over HTTP from " + sourceURL) |
| 508 | if duration > 0 { |
| 509 | ui.Printscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#Print.(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().("Please wait... (%v)", duration)) |
| 510 | } |
| 511 | f, err = fetchURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/fetchURL().(sourceURL, timeout, tr) |
| 512 | src = sourceURL |
| 513 | } |
| 514 | } |
| 515 | if err == nil { |
| 516 | defer f.Closescip-go gomod github.com/golang/go/src go1.24.0 io/Closer#Close().() |
| 517 | p, err = profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/.Parsescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Parse().(f) |
| 518 | } |
| 519 | return |
| 520 | } |
| 521 | |
| 522 | // fetchURL fetches a profile from a URL using HTTP. |
| 523 | func fetchURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/fetchURL().(source string, timeout timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#, tr httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.RoundTripperscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/RoundTripper#) (ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.24.0 io/ReadCloser#, error) { |
| 524 | client := &httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Clientscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Client#{ |
| 525 | Transportscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Client#Transport.: tr, |
| 526 | Timeoutscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Client#Timeout.: timeout + 5*timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second., |
| 527 | } |
| 528 | resp, err := client.Getscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Client#Get().(source) |
| 529 | if err != nil { |
| 530 | return nil, 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().("http fetch: %v", err) |
| 531 | } |
| 532 | if resp.StatusCodescip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/StatusOK. { |
| 533 | defer resp.Bodyscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.24.0 io/Closer#Close().() |
| 534 | return nil, statusCodeErrorscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/statusCodeError().(resp) |
| 535 | } |
| 536 | |
| 537 | return resp.Bodyscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Body., nil |
| 538 | } |
| 539 | |
| 540 | func statusCodeErrorscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/statusCodeError().(resp *httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#) error { |
| 541 | if resp.Headerscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Header#Get().("X-Go-Pprof") != "" && 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().(resp.Headerscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Header#Get().("Content-Type"), "text/plain") { |
| 542 | // error is from pprof endpoint |
| 543 | if body, err := ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.24.0 io/ReadAll().(resp.Bodyscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Body.); err == nil { |
| 544 | 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().("server response: %s - %s", resp.Statusscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Status., body) |
| 545 | } |
| 546 | } |
| 547 | 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().("server response: %s", resp.Statusscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Response#Status.) |
| 548 | } |
| 549 | |
| 550 | // isPerfFile checks if a file is in perf.data format. It also returns false |
| 551 | // if it encounters an error during the check. |
| 552 | func isPerfFilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/isPerfFile().(path string) bool { |
| 553 | sourceFile, openErr := osscip-go gomod github.com/golang/go/src go1.24.0 os/.Openscip-go gomod github.com/golang/go/src go1.24.0 os/Open().(path) |
| 554 | if openErr != nil { |
| 555 | return false |
| 556 | } |
| 557 | defer sourceFile.Closescip-go gomod github.com/golang/go/src go1.24.0 os/File#Close().() |
| 558 | |
| 559 | // If the file is the output of a perf record command, it should begin |
| 560 | // with the string PERFILE2. |
| 561 | perfHeader := []byte("PERFILE2") |
| 562 | actualHeader := make([]byte, len(perfHeader)) |
| 563 | if _, readErr := sourceFile.Readscip-go gomod github.com/golang/go/src go1.24.0 os/File#Read().(actualHeader); readErr != nil { |
| 564 | return false |
| 565 | } |
| 566 | return bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(actualHeader, perfHeader) |
| 567 | } |
| 568 | |
| 569 | // convertPerfData converts the file at path which should be in perf.data format |
| 570 | // using the perf_to_profile tool and returns the file containing the |
| 571 | // profile.proto formatted data. |
| 572 | func convertPerfDatascip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/convertPerfData().(perfPath string, 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#) (*osscip-go gomod github.com/golang/go/src go1.24.0 os/.Filescip-go gomod github.com/golang/go/src go1.24.0 os/File#, error) { |
| 573 | ui.Printscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/plugin`/UI#Print.(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().( |
| 574 | "Converting %s to a profile.proto... (May take a few minutes)", |
| 575 | perfPath)) |
| 576 | profile, err := newTempFilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/newTempFile().(osscip-go gomod github.com/golang/go/src go1.24.0 os/.TempDirscip-go gomod github.com/golang/go/src go1.24.0 os/TempDir().(), "pprof_", ".pb.gz") |
| 577 | if err != nil { |
| 578 | return nil, err |
| 579 | } |
| 580 | deferDeleteTempFilescip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/deferDeleteTempFile().(profile.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().()) |
| 581 | cmd := execscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Command().("perf_to_profile", "-i", perfPath, "-o", profile.Namescip-go gomod github.com/golang/go/src go1.24.0 os/File#Name().(), "-f") |
| 582 | cmd.Stdoutscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#Stdout., cmd.Stderrscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#Stderr. = osscip-go gomod github.com/golang/go/src go1.24.0 os/.Stdoutscip-go gomod github.com/golang/go/src go1.24.0 os/Stdout., osscip-go gomod github.com/golang/go/src go1.24.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.24.0 os/Stderr. |
| 583 | if err := cmd.Runscip-go gomod github.com/golang/go/src go1.24.0 `os/exec`/Cmd#Run().(); err != nil { |
| 584 | profile.Closescip-go gomod github.com/golang/go/src go1.24.0 os/File#Close().() |
| 585 | return nil, 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().("failed to convert perf.data file. Try github.com/google/perf_data_converter: %v", err) |
| 586 | } |
| 587 | return profile, nil |
| 588 | } |
| 589 | |
| 590 | // adjustURL validates if a profile source is a URL and returns an |
| 591 | // cleaned up URL and the timeout to use for retrieval over HTTP. |
| 592 | // If the source cannot be recognized as a URL it returns an empty string. |
| 593 | func adjustURLscip-go gomod github.com/google/pprof . `github.com/google/pprof/internal/driver`/adjustURL().(source string, duration, timeout timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) (string, timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) { |
| 594 | u, 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) |
| 595 | if err != nil || (u.Hostscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Host. == "" && u.Schemescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Scheme. != "" && u.Schemescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Scheme. != "file") { |
| 596 | // Try adding http:// to catch sources of the form hostname:port/path. |
| 597 | // url.Parse treats "hostname" as the scheme. |
| 598 | u, 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().("http://" + source) |
| 599 | } |
| 600 | if err != nil || u.Hostscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Host. == "" { |
| 601 | return "", 0 |
| 602 | } |
| 603 | |
| 604 | // Apply duration/timeout overrides to URL. |
| 605 | values := u.Queryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Query().() |
| 606 | if duration > 0 { |
| 607 | values.Setscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Values#Set().("seconds", fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprint().(int(duration.Secondsscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#Seconds().()))) |
| 608 | } else { |
| 609 | if urlSeconds := values.Getscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Values#Get().("seconds"); urlSeconds != "" { |
| 610 | if us, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.24.0 strconv/ParseInt().(urlSeconds, 10, 32); err == nil { |
| 611 | duration = timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#(us) * timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second. |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | if timeout <= 0 { |
| 616 | if duration > 0 { |
| 617 | timeout = duration + duration/2 |
| 618 | } else { |
| 619 | timeout = 60 * timescip-go gomod github.com/golang/go/src go1.24.0 time/.Secondscip-go gomod github.com/golang/go/src go1.24.0 time/Second. |
| 620 | } |
| 621 | } |
| 622 | u.RawQueryscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#RawQuery. = values.Encodescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Values#Encode().() |
| 623 | return u.Stringscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#String().(), timeout |
| 624 | } |