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