Debian Codegraph @

src/cmd/vendor/github.com/google/pprof/driver/driver.go

raw
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. If the object
141 // is a linux kernel, relocationSymbol is the name of the symbol
142 // corresponding to the start address.
143 Open(file string, start, limit, offset uint64, relocationSymbol string) (ObjFile, error)
144
145 // Disasm disassembles the named object file, starting at
146 // the start address and stopping at (before) the end address.
147 Disasm(file string, start, end uint64, intelSyntax bool) ([]Inst, error)
148 }
149
150 // An Inst is a single instruction in an assembly listing.
151 type Inst struct {
152 Addr uint64 // virtual address of instruction
153 Text string // instruction text
154 Function string // function name
155 File string // source file
156 Line int // source line
157 }
158
159 // An ObjFile is a single object file: a shared library or executable.
160 type ObjFile interface {
161 // Name returns the underlying file name, if available.
162 Name() string
163
164 // ObjAddr returns the objdump address corresponding to a runtime address.
165 ObjAddr(addr uint64) (uint64, error)
166
167 // BuildID returns the GNU build ID of the file, or an empty string.
168 BuildID() string
169
170 // SourceLine reports the source line information for a given
171 // address in the file. Due to inlining, the source line information
172 // is in general a list of positions representing a call stack,
173 // with the leaf function first.
174 SourceLine(addr uint64) ([]Frame, error)
175
176 // Symbols returns a list of symbols in the object file.
177 // If r is not nil, Symbols restricts the list to symbols
178 // with names matching the regular expression.
179 // If addr is not zero, Symbols restricts the list to symbols
180 // containing that address.
181 Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
182
183 // Close closes the file, releasing associated resources.
184 Close() error
185 }
186
187 // A Frame describes a single line in a source file.
188 type Frame struct {
189 Func string // name of function
190 File string // source file name
191 Line int // line in file
192 Column int // column in file
193 StartLine int // start line of function (if available)
194 }
195
196 // A Sym describes a single symbol in an object file.
197 type Sym struct {
198 Name []string // names of symbol (many if symbol was dedup'ed)
199 File string // object file containing symbol
200 Start uint64 // start virtual address
201 End uint64 // virtual address of last byte in sym (Start+size-1)
202 }
203
204 // A UI manages user interactions.
205 type UI interface {
206 // ReadLine returns a line of text (a command) read from the user.
207 // prompt is printed before reading the command.
208 ReadLine(prompt string) (string, error)
209
210 // Print shows a message to the user.
211 // It formats the text as fmt.Print would and adds a final \n if not already present.
212 // For line-based UI, Print writes to standard error.
213 // (Standard output is reserved for report data.)
214 Print(...interface{})
215
216 // PrintErr shows an error message to the user.
217 // It formats the text as fmt.Print would and adds a final \n if not already present.
218 // For line-based UI, PrintErr writes to standard error.
219 PrintErr(...interface{})
220
221 // IsTerminal returns whether the UI is known to be tied to an
222 // interactive terminal (as opposed to being redirected to a file).
223 IsTerminal() bool
224
225 // WantBrowser indicates whether browser should be opened with the -http option.
226 WantBrowser() bool
227
228 // SetAutoComplete instructs the UI to call complete(cmd) to obtain
229 // the auto-completion of cmd, if the UI supports auto-completion at all.
230 SetAutoComplete(complete func(string) string)
231 }
232
233 // internalObjTool is a wrapper to map from the pprof external
234 // interface to the internal interface.
235 type internalObjTool struct {
236 ObjTool
237 }
238
239 func (o *internalObjTool) Open(file string, start, limit, offset uint64, relocationSymbol string) (plugin.ObjFile, error) {
240 f, err := o.ObjTool.Open(file, start, limit, offset, relocationSymbol)
241 if err != nil {
242 return nil, err
243 }
244 return &internalObjFile{f}, err
245 }
246
247 type internalObjFile struct {
248 ObjFile
249 }
250
251 func (f *internalObjFile) SourceLine(frame uint64) ([]plugin.Frame, error) {
252 frames, err := f.ObjFile.SourceLine(frame)
253 if err != nil {
254 return nil, err
255 }
256 var pluginFrames []plugin.Frame
257 for _, f := range frames {
258 pluginFrames = append(pluginFrames, plugin.Frame(f))
259 }
260 return pluginFrames, nil
261 }
262
263 func (f *internalObjFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
264 syms, err := f.ObjFile.Symbols(r, addr)
265 if err != nil {
266 return nil, err
267 }
268 var pluginSyms []*plugin.Sym
269 for _, s := range syms {
270 ps := plugin.Sym(*s)
271 pluginSyms = append(pluginSyms, &ps)
272 }
273 return pluginSyms, nil
274 }
275
276 func (o *internalObjTool) Disasm(file string, start, end uint64, intelSyntax bool) ([]plugin.Inst, error) {
277 insts, err := o.ObjTool.Disasm(file, start, end, intelSyntax)
278 if err != nil {
279 return nil, err
280 }
281 var pluginInst []plugin.Inst
282 for _, inst := range insts {
283 pluginInst = append(pluginInst, plugin.Inst(inst))
284 }
285 return pluginInst, nil
286 }
287
288 // internalSymbolizer is a wrapper to map from the pprof external
289 // interface to the internal interface.
290 type internalSymbolizer struct {
291 Symbolizer
292 }
293
294 func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error {
295 isrcs := MappingSources{}
296 for m, s := range srcs {
297 isrcs[m] = s
298 }
299 return s.Symbolizer.Symbolize(mode, isrcs, prof)
300 }