1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package driver
16
17 import (
18 "bytes"
19 "fmt"
20 "html/template"
21 "io"
22 "net"
23 "net/http"
24 gourl "net/url"
25 "os"
26 "os/exec"
27 "strconv"
28 "strings"
29 "time"
30
31 "github.com/google/pprof/internal/graph"
32 "github.com/google/pprof/internal/measurement"
33 "github.com/google/pprof/internal/plugin"
34 "github.com/google/pprof/internal/report"
35 "github.com/google/pprof/profile"
36 )
37
38
39 type webInterface struct {
40 prof *profile.Profile
41 copier profileCopier
42 options *plugin.Options
43 help map[string]string
44 settingsFile string
45 }
46
47 func makeWebInterface(p *profile.Profile, copier profileCopier, opt *plugin.Options) (*webInterface, error) {
48 settingsFile, err := settingsFileName()
49 if err != nil {
50 return nil, err
51 }
52 return &webInterface{
53 prof: p,
54 copier: copier,
55 options: opt,
56 help: make(map[string]string),
57 settingsFile: settingsFile,
58 }, nil
59 }
60
61
62 const maxEntries = 50
63
64
65 type errorCatcher struct {
66 plugin.UI
67 errors []string
68 }
69
70 func (ec *errorCatcher) PrintErr(args ...interface{}) {
71 ec.errors = append(ec.errors, strings.TrimSuffix(fmt.Sprintln(args...), "\n"))
72 ec.UI.PrintErr(args...)
73 }
74
75
76 type webArgs struct {
77 Title string
78 Errors []string
79 Total int64
80 SampleTypes []string
81 Legend []string
82 Standalone bool
83 Help map[string]string
84 Nodes []string
85 HTMLBody template.HTML
86 TextBody string
87 Top []report.TextItem
88 Listing report.WebListData
89 FlameGraph template.JS
90 Stacks template.JS
91 Configs []configMenuEntry
92 UnitDefs []measurement.UnitType
93 }
94
95 func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, disableBrowser bool) error {
96 host, port, err := getHostAndPort(hostport)
97 if err != nil {
98 return err
99 }
100 interactiveMode = true
101 copier := makeProfileCopier(p)
102 ui, err := makeWebInterface(p, copier, o)
103 if err != nil {
104 return err
105 }
106 for n, c := range pprofCommands {
107 ui.help[n] = c.description
108 }
109 for n, help := range configHelp {
110 ui.help[n] = help
111 }
112 ui.help["details"] = "Show information about the profile and this view"
113 ui.help["graph"] = "Display profile as a directed graph"
114 ui.help["flamegraph"] = "Display profile as a flame graph"
115 ui.help["reset"] = "Show the entire profile"
116 ui.help["save_config"] = "Save current settings"
117
118 server := o.HTTPServer
119 if server == nil {
120 server = defaultWebServer
121 }
122 args := &plugin.HTTPServerArgs{
123 Hostport: net.JoinHostPort(host, strconv.Itoa(port)),
124 Host: host,
125 Port: port,
126 Handlers: map[string]http.Handler{
127 "/": http.HandlerFunc(ui.dot),
128 "/top": http.HandlerFunc(ui.top),
129 "/disasm": http.HandlerFunc(ui.disasm),
130 "/source": http.HandlerFunc(ui.source),
131 "/peek": http.HandlerFunc(ui.peek),
132 "/flamegraph": http.HandlerFunc(ui.stackView),
133 "/flamegraph2": redirectWithQuery("flamegraph", http.StatusMovedPermanently),
134 "/flamegraphold": redirectWithQuery("flamegraph", http.StatusMovedPermanently),
135 "/saveconfig": http.HandlerFunc(ui.saveConfig),
136 "/deleteconfig": http.HandlerFunc(ui.deleteConfig),
137 "/download": http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
138 w.Header().Set("Content-Type", "application/vnd.google.protobuf+gzip")
139 w.Header().Set("Content-Disposition", "attachment;filename=profile.pb.gz")
140 p.Write(w)
141 }),
142 },
143 }
144
145 url := "http://" + args.Hostport
146
147 o.UI.Print("Serving web UI on ", url)
148
149 if o.UI.WantBrowser() && !disableBrowser {
150 go openBrowser(url, o)
151 }
152 return server(args)
153 }
154
155 func getHostAndPort(hostport string) (string, int, error) {
156 host, portStr, err := net.SplitHostPort(hostport)
157 if err != nil {
158 return "", 0, fmt.Errorf("could not split http address: %v", err)
159 }
160 if host == "" {
161 host = "localhost"
162 }
163 var port int
164 if portStr == "" {
165 ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
166 if err != nil {
167 return "", 0, fmt.Errorf("could not generate random port: %v", err)
168 }
169 port = ln.Addr().(*net.TCPAddr).Port
170 err = ln.Close()
171 if err != nil {
172 return "", 0, fmt.Errorf("could not generate random port: %v", err)
173 }
174 } else {
175 port, err = strconv.Atoi(portStr)
176 if err != nil {
177 return "", 0, fmt.Errorf("invalid port number: %v", err)
178 }
179 }
180 return host, port, nil
181 }
182 func defaultWebServer(args *plugin.HTTPServerArgs) error {
183 ln, err := net.Listen("tcp", args.Hostport)
184 if err != nil {
185 return err
186 }
187 isLocal := isLocalhost(args.Host)
188 handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
189 if isLocal {
190
191 host, _, err := net.SplitHostPort(req.RemoteAddr)
192 if err != nil || !isLocalhost(host) {
193 http.Error(w, "permission denied", http.StatusForbidden)
194 return
195 }
196 }
197 h := args.Handlers[req.URL.Path]
198 if h == nil {
199
200 h = http.DefaultServeMux
201 }
202 h.ServeHTTP(w, req)
203 })
204
205
206
207
208
209 mux := http.NewServeMux()
210 mux.Handle("/ui/", http.StripPrefix("/ui", handler))
211 mux.Handle("/", redirectWithQuery("/ui", http.StatusTemporaryRedirect))
212 s := &http.Server{Handler: mux}
213 return s.Serve(ln)
214 }
215
216
217
218
219
220 func redirectWithQuery(path string, code int) http.HandlerFunc {
221 return func(w http.ResponseWriter, r *http.Request) {
222 pathWithQuery := &gourl.URL{Path: path, RawQuery: r.URL.RawQuery}
223 w.Header().Set("Location", pathWithQuery.String())
224 w.WriteHeader(code)
225 }
226 }
227
228 func isLocalhost(host string) bool {
229 for _, v := range []string{"localhost", "127.0.0.1", "[::1]", "::1"} {
230 if host == v {
231 return true
232 }
233 }
234 return false
235 }
236
237 func openBrowser(url string, o *plugin.Options) {
238
239 baseURL, _ := gourl.Parse(url)
240 current := currentConfig()
241 u, _ := current.makeURL(*baseURL)
242
243
244 time.Sleep(time.Millisecond * 500)
245
246 for _, b := range browsers() {
247 args := strings.Split(b, " ")
248 if len(args) == 0 {
249 continue
250 }
251 viewer := exec.Command(args[0], append(args[1:], u.String())...)
252 viewer.Stderr = os.Stderr
253 if err := viewer.Start(); err == nil {
254 return
255 }
256 }
257
258 o.UI.PrintErr(u.String())
259 }
260
261
262
263 func (ui *webInterface) makeReport(w http.ResponseWriter, req *http.Request,
264 cmd []string, configEditor func(*config)) (*report.Report, []string) {
265 cfg := currentConfig()
266 if err := cfg.applyURL(req.URL.Query()); err != nil {
267 http.Error(w, err.Error(), http.StatusBadRequest)
268 ui.options.UI.PrintErr(err)
269 return nil, nil
270 }
271 if configEditor != nil {
272 configEditor(&cfg)
273 }
274 catcher := &errorCatcher{UI: ui.options.UI}
275 options := *ui.options
276 options.UI = catcher
277 _, rpt, err := generateRawReport(ui.copier.newCopy(), cmd, cfg, &options)
278 if err != nil {
279 http.Error(w, err.Error(), http.StatusBadRequest)
280 ui.options.UI.PrintErr(err)
281 return nil, nil
282 }
283 return rpt, catcher.errors
284 }
285
286
287 func renderHTML(dst io.Writer, tmpl string, rpt *report.Report, errList, legend []string, data webArgs) error {
288 file := getFromLegend(legend, "File: ", "unknown")
289 profile := getFromLegend(legend, "Type: ", "unknown")
290 data.Title = file + " " + profile
291 data.Errors = errList
292 data.Total = rpt.Total()
293 data.Legend = legend
294 return getHTMLTemplates().ExecuteTemplate(dst, tmpl, data)
295 }
296
297
298 func (ui *webInterface) render(w http.ResponseWriter, req *http.Request, tmpl string,
299 rpt *report.Report, errList, legend []string, data webArgs) {
300 data.SampleTypes = sampleTypes(ui.prof)
301 data.Help = ui.help
302 data.Configs = configMenu(ui.settingsFile, *req.URL)
303 html := &bytes.Buffer{}
304 if err := renderHTML(html, tmpl, rpt, errList, legend, data); err != nil {
305 http.Error(w, "internal template error", http.StatusInternalServerError)
306 ui.options.UI.PrintErr(err)
307 return
308 }
309 w.Header().Set("Content-Type", "text/html")
310 w.Write(html.Bytes())
311 }
312
313
314 func (ui *webInterface) dot(w http.ResponseWriter, req *http.Request) {
315 rpt, errList := ui.makeReport(w, req, []string{"svg"}, nil)
316 if rpt == nil {
317 return
318 }
319
320
321 g, config := report.GetDOT(rpt)
322 legend := config.Labels
323 config.Labels = nil
324 dot := &bytes.Buffer{}
325 graph.ComposeDot(dot, g, &graph.DotAttributes{}, config)
326
327
328 svg, err := dotToSvg(dot.Bytes())
329 if err != nil {
330 http.Error(w, "Could not execute dot; may need to install graphviz.",
331 http.StatusNotImplemented)
332 ui.options.UI.PrintErr("Failed to execute dot. Is Graphviz installed?\n", err)
333 return
334 }
335
336
337 nodes := []string{""}
338 for _, n := range g.Nodes {
339 nodes = append(nodes, n.Info.Name)
340 }
341
342 ui.render(w, req, "graph", rpt, errList, legend, webArgs{
343 HTMLBody: template.HTML(string(svg)),
344 Nodes: nodes,
345 })
346 }
347
348 func dotToSvg(dot []byte) ([]byte, error) {
349 cmd := exec.Command("dot", "-Tsvg")
350 out := &bytes.Buffer{}
351 cmd.Stdin, cmd.Stdout, cmd.Stderr = bytes.NewBuffer(dot), out, os.Stderr
352 if err := cmd.Run(); err != nil {
353 return nil, err
354 }
355
356
357 svg := bytes.Replace(out.Bytes(), []byte("&;"), []byte("&;"), -1)
358
359
360 if pos := bytes.Index(svg, []byte("<svg")); pos >= 0 {
361 svg = svg[pos:]
362 }
363 return svg, nil
364 }
365
366 func (ui *webInterface) top(w http.ResponseWriter, req *http.Request) {
367 rpt, errList := ui.makeReport(w, req, []string{"top"}, func(cfg *config) {
368 cfg.NodeCount = 500
369 })
370 if rpt == nil {
371 return
372 }
373 top, legend := report.TextItems(rpt)
374 var nodes []string
375 for _, item := range top {
376 nodes = append(nodes, item.Name)
377 }
378
379 ui.render(w, req, "top", rpt, errList, legend, webArgs{
380 Top: top,
381 Nodes: nodes,
382 })
383 }
384
385
386 func (ui *webInterface) disasm(w http.ResponseWriter, req *http.Request) {
387 args := []string{"disasm", req.URL.Query().Get("f")}
388 rpt, errList := ui.makeReport(w, req, args, nil)
389 if rpt == nil {
390 return
391 }
392
393 out := &bytes.Buffer{}
394 if err := report.PrintAssembly(out, rpt, ui.options.Obj, maxEntries); err != nil {
395 http.Error(w, err.Error(), http.StatusBadRequest)
396 ui.options.UI.PrintErr(err)
397 return
398 }
399
400 legend := report.ProfileLabels(rpt)
401 ui.render(w, req, "plaintext", rpt, errList, legend, webArgs{
402 TextBody: out.String(),
403 })
404
405 }
406
407
408
409 func (ui *webInterface) source(w http.ResponseWriter, req *http.Request) {
410 args := []string{"weblist", req.URL.Query().Get("f")}
411 rpt, errList := ui.makeReport(w, req, args, nil)
412 if rpt == nil {
413 return
414 }
415
416
417 listing, err := report.MakeWebList(rpt, ui.options.Obj, maxEntries)
418 if err != nil {
419 http.Error(w, err.Error(), http.StatusBadRequest)
420 ui.options.UI.PrintErr(err)
421 return
422 }
423
424 legend := report.ProfileLabels(rpt)
425 ui.render(w, req, "sourcelisting", rpt, errList, legend, webArgs{
426 Listing: listing,
427 })
428 }
429
430
431 func (ui *webInterface) peek(w http.ResponseWriter, req *http.Request) {
432 args := []string{"peek", req.URL.Query().Get("f")}
433 rpt, errList := ui.makeReport(w, req, args, func(cfg *config) {
434 cfg.Granularity = "lines"
435 })
436 if rpt == nil {
437 return
438 }
439
440 out := &bytes.Buffer{}
441 if err := report.Generate(out, rpt, ui.options.Obj); err != nil {
442 http.Error(w, err.Error(), http.StatusBadRequest)
443 ui.options.UI.PrintErr(err)
444 return
445 }
446
447 legend := report.ProfileLabels(rpt)
448 ui.render(w, req, "plaintext", rpt, errList, legend, webArgs{
449 TextBody: out.String(),
450 })
451 }
452
453
454 func (ui *webInterface) saveConfig(w http.ResponseWriter, req *http.Request) {
455 if err := setConfig(ui.settingsFile, *req.URL); err != nil {
456 http.Error(w, err.Error(), http.StatusBadRequest)
457 ui.options.UI.PrintErr(err)
458 return
459 }
460 }
461
462
463 func (ui *webInterface) deleteConfig(w http.ResponseWriter, req *http.Request) {
464 name := req.URL.Query().Get("config")
465 if err := removeConfig(ui.settingsFile, name); err != nil {
466 http.Error(w, err.Error(), http.StatusBadRequest)
467 ui.options.UI.PrintErr(err)
468 return
469 }
470 }
471
472
473
474 func getFromLegend(legend []string, param, def string) string {
475 for _, s := range legend {
476 if strings.HasPrefix(s, param) {
477 return s[len(param):]
478 }
479 }
480 return def
481 }
482
View as plain text