Source file src/pkg/cmd/vendor/github.com/google/pprof/driver/driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
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
64
65 type HTTPServerArgs plugin.HTTPServerArgs
66
67
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
80
81 type Writer interface {
82 Open(name string) (io.WriteCloser, error)
83 }
84
85
86
87 type FlagSet interface {
88
89
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
96
97 BoolVar(pointer *bool, name string, def bool, usage string)
98 IntVar(pointer *int, name string, def int, usage string)
99 Float64Var(pointer *float64, name string, def float64, usage string)
100 StringVar(pointer *string, name string, def string, usage string)
101
102
103
104 StringList(name string, def string, usage string) *[]*string
105
106
107
108
109
110
111 ExtraUsage() string
112
113
114 AddExtraUsage(eu string)
115
116
117
118
119
120 Parse(usage func()) []string
121 }
122
123
124
125
126
127 type Fetcher interface {
128 Fetch(src string, duration, timeout time.Duration) (*profile.Profile, string, error)
129 }
130
131
132 type Symbolizer interface {
133 Symbolize(mode string, srcs MappingSources, prof *profile.Profile) error
134 }
135
136
137
138 type MappingSources map[string][]struct {
139 Source string
140 Start uint64
141 }
142
143
144 type ObjTool interface {
145
146
147
148 Open(file string, start, limit, offset uint64) (ObjFile, error)
149
150
151
152 Disasm(file string, start, end uint64) ([]Inst, error)
153 }
154
155
156 type Inst struct {
157 Addr uint64
158 Text string
159 Function string
160 File string
161 Line int
162 }
163
164
165 type ObjFile interface {
166
167 Name() string
168
169
170 Base() uint64
171
172
173 BuildID() string
174
175
176
177
178
179 SourceLine(addr uint64) ([]Frame, error)
180
181
182
183
184
185
186 Symbols(r *regexp.Regexp, addr uint64) ([]*Sym, error)
187
188
189 Close() error
190 }
191
192
193 type Frame struct {
194 Func string
195 File string
196 Line int
197 }
198
199
200 type Sym struct {
201 Name []string
202 File string
203 Start uint64
204 End uint64
205 }
206
207
208 type UI interface {
209
210
211 ReadLine(prompt string) (string, error)
212
213
214
215
216
217 Print(...interface{})
218
219
220
221
222 PrintErr(...interface{})
223
224
225
226 IsTerminal() bool
227
228
229 WantBrowser() bool
230
231
232
233 SetAutoComplete(complete func(string) string)
234 }
235
236
237
238 type internalObjTool struct {
239 ObjTool
240 }
241
242 func (o *internalObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
243 f, err := o.ObjTool.Open(file, start, limit, offset)
244 if err != nil {
245 return nil, err
246 }
247 return &internalObjFile{f}, err
248 }
249
250 type internalObjFile struct {
251 ObjFile
252 }
253
254 func (f *internalObjFile) SourceLine(frame uint64) ([]plugin.Frame, error) {
255 frames, err := f.ObjFile.SourceLine(frame)
256 if err != nil {
257 return nil, err
258 }
259 var pluginFrames []plugin.Frame
260 for _, f := range frames {
261 pluginFrames = append(pluginFrames, plugin.Frame(f))
262 }
263 return pluginFrames, nil
264 }
265
266 func (f *internalObjFile) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
267 syms, err := f.ObjFile.Symbols(r, addr)
268 if err != nil {
269 return nil, err
270 }
271 var pluginSyms []*plugin.Sym
272 for _, s := range syms {
273 ps := plugin.Sym(*s)
274 pluginSyms = append(pluginSyms, &ps)
275 }
276 return pluginSyms, nil
277 }
278
279 func (o *internalObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
280 insts, err := o.ObjTool.Disasm(file, start, end)
281 if err != nil {
282 return nil, err
283 }
284 var pluginInst []plugin.Inst
285 for _, inst := range insts {
286 pluginInst = append(pluginInst, plugin.Inst(inst))
287 }
288 return pluginInst, nil
289 }
290
291
292
293 type internalSymbolizer struct {
294 Symbolizer
295 }
296
297 func (s *internalSymbolizer) Symbolize(mode string, srcs plugin.MappingSources, prof *profile.Profile) error {
298 isrcs := MappingSources{}
299 for m, s := range srcs {
300 isrcs[m] = s
301 }
302 return s.Symbolizer.Symbolize(mode, isrcs, prof)
303 }
304
View as plain text