| 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 driver provides an external entry point to the pprof driver. |
| 16 | package driver |
| 17 | |
| 18 | import ( |
| 19 | "io" |
| 20 | "maps" |
| 21 | "net/http" |
| 22 | "regexp" |
| 23 | "time" |
| 24 | |
| 25 | internaldriver "github.com/google/pprof/internal/driver" |
| 26 | "github.com/google/pprof/internal/plugin" |
| 27 | "github.com/google/pprof/profile" |
| 28 | ) |
| 29 | |
| 30 | // PProf acquires a profile, and symbolizes it using a profile |
| 31 | // manager. Then it generates a report formatted according to the |
| 32 | // options selected through the flags package. |
| 33 | func PProf(o *Options) error { |
| 34 | return internaldriver.PProf(o.internalOptions()) |
| 35 | } |
| 36 | |
| 37 | func (o *Options) internalOptions() *plugin.Options { |
| 38 | var obj plugin.ObjTool |
| 39 | if o.Obj != nil { |
| 40 | obj = &internalObjTool{o.Obj} |
| 41 | } |
| 42 | var sym plugin.Symbolizer |
| 43 | if o.Sym != nil { |
| 44 | sym = &internalSymbolizer{o.Sym} |
| 45 | } |
| 46 | var httpServer func(args *plugin.HTTPServerArgs) error |
| 47 | if o.HTTPServer != nil { |
| 48 | httpServer = func(args *plugin.HTTPServerArgs) error { |
| 49 | return o.HTTPServer(((*HTTPServerArgs)(args))) |
| 50 | } |
| 51 | } |
| 52 | return &plugin.Options{ |
| 53 | Writer: o.Writer, |
| 54 | Flagset: o.Flagset, |
| 55 | Fetch: o.Fetch, |
| 56 | Sym: sym, |
| 57 | Obj: obj, |
| 58 | UI: o.UI, |
| 59 | HTTPServer: httpServer, |
| 60 | HTTPTransport: o.HTTPTransport, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // HTTPServerArgs contains arguments needed by an HTTP server that |
| 65 | // is exporting a pprof web interface. |
| 66 | type HTTPServerArgs plugin.HTTPServerArgs |
| 67 | |
| 68 | // Options groups all the optional plugins into pprof. |
| 69 | type Options struct { |
| 70 | Writer Writer |
| 71 | Flagset FlagSet |
| 72 | Fetch Fetcher |
| 73 | Sym Symbolizer |
| 74 | Obj ObjTool |
| 75 | UI UI |
| 76 | HTTPServer func(*HTTPServerArgs) error |
| 77 | HTTPTransport http.RoundTripper |
| 78 | } |
| 79 | |
| 80 | // Writer provides a mechanism to write data under a certain name, |
| 81 | // typically a filename. |
| 82 | type Writer interface { |
| 83 | Open(name string) (io.WriteCloser, error) |
| 84 | } |
| 85 | |
| 86 | // A FlagSet creates and parses command-line flags. |
| 87 | // It is similar to the standard flag.FlagSet. |
| 88 | type FlagSet interface { |
| 89 | // Bool, Int, Float64, and String define new flags, |
| 90 | // like the functions of the same name in package flag. |
| 91 | Bool(name string, def bool, usage string) *bool |
| 92 | Int(name string, def int, usage string) *int |
| 93 | Float64(name string, def float64, usage string) *float64 |
| 94 | String(name string, def string, usage string) *string |
| 95 | |
| 96 | // StringList is similar to String but allows multiple values for a |
| 97 | // single flag |
| 98 | StringList(name string, def string, usage string) *[]*string |
| 99 | |
| 100 | // ExtraUsage returns any additional text that should be printed after the |
| 101 | // standard usage message. The extra usage message returned includes all text |
| 102 | // added with AddExtraUsage(). |
| 103 | // The typical use of ExtraUsage is to show any custom flags defined by the |
| 104 | // specific pprof plugins being used. |
| 105 | ExtraUsage() string |
| 106 | |
| 107 | // AddExtraUsage appends additional text to the end of the extra usage message. |
| 108 | AddExtraUsage(eu string) |
| 109 | |
| 110 | // Parse initializes the flags with their values for this run |
| 111 | // and returns the non-flag command line arguments. |
| 112 | // If an unknown flag is encountered or there are no arguments, |
| 113 | // Parse should call usage and return nil. |
| 114 | Parse(usage func()) []string |
| 115 | } |
| 116 | |
| 117 | // A Fetcher reads and returns the profile named by src, using |
| 118 | // the specified duration and timeout. It returns the fetched |
| 119 | // profile and a string indicating a URL from where the profile |
| 120 | // was fetched, which may be different than src. |
| 121 | type Fetcher interface { |
| 122 | Fetch(src string, duration, timeout time.Duration) (*profile.Profile, string, error) |
| 123 | } |
| 124 | |
| 125 | // A Symbolizer introduces symbol information into a profile. |
| 126 | type Symbolizer interface { |
| 127 | Symbolize(mode string, srcs MappingSources, prof *profile.Profile) error |
| 128 | } |
| 129 | |
| 130 | // MappingSources map each profile.Mapping to the source of the profile. |
| 131 | // The key is either Mapping.File or Mapping.BuildId. |
| 132 | type MappingSources map[string][]struct { |
| 133 | Source string // URL of the source the mapping was collected from |
| 134 | Start uint64 // delta applied to addresses from this source (to represent Merge adjustments) |
| 135 | } |
| 136 | |
| 137 | // An ObjTool inspects shared libraries and executable files. |
| 138 | type ObjTool interface { |
| 139 | // Open opens the named object file. If the object is a shared |
| 140 | // library, start/limit/offset are the addresses where it is mapped |
| 141 | // into memory in the address space being inspected. If the object |
| 142 | // is a linux kernel, relocationSymbol is the name of the symbol |
| 143 | // corresponding to the start address. |
| 144 | Open(file string, start, limit, offset uint64, relocationSymbol string) (ObjFile, error) |
| 145 | |
| 146 | // Disasm disassembles the named object file, starting at |
| 147 | // the start address and stopping at (before) the end address. |
| 148 | Disasm(file string, start, end uint64, intelSyntax bool) ([]Inst, error) |
| 149 | } |
| 150 | |
| 151 | // An Inst is a single instruction in an assembly listing. |
| 152 | type Inst struct { |
| 153 | Addr uint64 // virtual address of instruction |
| 154 | Text string // instruction text |
| 155 | Function string // function name |
| 156 | File string // source file |
| 157 | Line int // source line |
| 158 | } |
| 159 | |
| 160 | // An ObjFile is a single object file: a shared library or executable. |
| 161 | type ObjFile interface { |
| 162 | // Name returns the underlying file name, if available. |
| 163 | Name() string |
| 164 | |
| 165 | // ObjAddr returns the objdump address corresponding to a runtime address. |
| 166 | ObjAddr(addr uint64) (uint64, error) |
| 167 | |
| 168 | // BuildID returns the GNU build ID of the file, or an empty string. |
| 169 | BuildID() string |
| 170 | |
| 171 | // SourceLine reports the source line information for a given |
| 172 | // address in the file. Due to inlining, the source line information |
| 173 | // is in general a list of positions representing a call stack, |
| 174 | // with the leaf function first. |
| 175 | SourceLine(addr uint64) ([]Frame, error) |
| 176 | |
| 177 | // Symbols returns a list of symbols in the object file. |
| 178 | // If r is not nil, Symbols restricts the list to symbols |
| 179 | // with names matching the regular expression. |
| 180 | // If addr is not zero, Symbols restricts the list to symbols |
| 181 | // containing that address. |
| 182 | Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error) |
| 183 | |
| 184 | // Close closes the file, releasing associated resources. |
| 185 | Close() error |
| 186 | } |
| 187 | |
| 188 | // A Frame describes a single line in a source file. |
| 189 | type Frame struct { |
| 190 | Func string // name of function |
| 191 | File string // source file name |
| 192 | Line int // line in file |
| 193 | Column int // column in file |
| 194 | StartLine int // start line of function (if available) |
| 195 | } |
| 196 | |
| 197 | // A Sym describes a single symbol in an object file. |
| 198 | type Sym struct { |
| 199 | Name []string // names of symbol (many if symbol was dedup'ed) |
| 200 | File string // object file containing symbol |
| 201 | Start uint64 // start virtual address |
| 202 | End uint64 // virtual address of last byte in sym (Start+size-1) |
| 203 | } |
| 204 | |
| 205 | // A UI manages user interactions. |
| 206 | type UI interface { |
| 207 | // ReadLine returns a line of text (a command) read from the user. |
| 208 | // prompt is printed before reading the command. |
| 209 | ReadLine(prompt string) (string, error) |
| 210 | |
| 211 | // Print shows a message to the user. |
| 212 | // It formats the text as fmt.Print would and adds a final \n if not already present. |
| 213 | // For line-based UI, Print writes to standard error. |
| 214 | // (Standard output is reserved for report data.) |
| 215 | Print(...interface{}) |
| 216 | |
| 217 | // PrintErr shows an error message to the user. |
| 218 | // It formats the text as fmt.Print would and adds a final \n if not already present. |
| 219 | // For line-based UI, PrintErr writes to standard error. |
| 220 | PrintErr(...interface{}) |
| 221 | |
| 222 | // IsTerminal returns whether the UI is known to be tied to an |
| 223 | // interactive terminal (as opposed to being redirected to a file). |
| 224 | IsTerminal() bool |
| 225 | |
| 226 | // WantBrowser indicates whether browser should be opened with the -http option. |
| 227 | WantBrowser() bool |
| 228 | |
| 229 | // SetAutoComplete instructs the UI to call complete(cmd) to obtain |
| 230 | // the auto-completion of cmd, if the UI supports auto-completion at all. |
| 231 | SetAutoComplete(complete func(string) string) |
| 232 | } |
| 233 | |
| 234 | // internalObjTool is a wrapper to map from the pprof external |
| 235 | // interface to the internal interface. |
| 236 | type internalObjTool struct { |
| 237 | ObjTool |
| 238 | } |
| 239 | |
| 240 | func (o *internalObjTool) Open(file string, start, limit, offset uint64, relocationSymbol string) (plugin.ObjFile, error) { |
| 241 | f, err := o.ObjTool.Open(file, start, limit, offset, relocationSymbol) |
| 242 | if err != nil { |
| 243 | return nil, err |
| 244 | } |
| 245 | return &internalObjFile{f}, err |
| 246 | } |
| 247 | |
| 248 | type internalObjFile struct { |
| 249 | ObjFile |
| 250 | } |
| 251 | |
| 252 | func (f *internalObjFile) SourceLine(frame uint64) ([]plugin.Frame, error) { |
| 253 | frames, err := f.ObjFile.SourceLine(frame) |
| 254 | if err != nil { |
| 255 | return nil, err |
| 256 | } |
| 257 | var pluginFrames []plugin.Frame |
| 258 | for _, f := range frames { |
| 259 | pluginFrames = append(pluginFrames, plugin.Frame(f)) |
| 260 | } |
| 261 | return pluginFrames, nil |
| 262 | } |
| 263 | |
| 264 | func (f *internalObjFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) { |
| 265 | syms, err := f.ObjFile.Symbols(r, addr) |
| 266 | if err != nil { |
| 267 | return nil, err |
| 268 | } |
| 269 | var pluginSyms []*plugin.Sym |
| 270 | for _, s := range syms { |
| 271 | ps := plugin.Sym(*s) |
| 272 | pluginSyms = append(pluginSyms, &ps) |
| 273 | } |
| 274 | return pluginSyms, nil |
| 275 | } |
| 276 | |
| 277 | func (o *internalObjTool) Disasm(file string, start, end uint64, intelSyntax bool) ([]plugin.Inst, error) { |
| 278 | insts, err := o.ObjTool.Disasm(file, start, end, intelSyntax) |
| 279 | if err != nil { |
| 280 | return nil, err |
| 281 | } |
| 282 | var pluginInst []plugin.Inst |
| 283 | for _, inst := range insts { |
| 284 | pluginInst = append(pluginInst, plugin.Inst(inst)) |
| 285 | } |
| 286 | return pluginInst, nil |
| 287 | } |
| 288 | |
| 289 | // internalSymbolizer is a wrapper to map from the pprof external |
| 290 | // interface to the internal interface. |
| 291 | type internalSymbolizer struct { |
| 292 | Symbolizer |
| 293 | } |
| 294 | |
| 295 | func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error { |
| 296 | isrcs := MappingSources{} |
| 297 | maps.Copy(isrcs, srcs) |
| 298 | return s.Symbolizer.Symbolize(mode, isrcs, prof) |
| 299 | } |