Source file src/cmd/vendor/github.com/google/pprof/internal/driver/webui.go

     1  // Copyright 2017 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
    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  // webInterface holds the state needed for serving a browser based interface.
    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  // maxEntries is the maximum number of entries to print for text interfaces.
    62  const maxEntries = 50
    63  
    64  // errorCatcher is a UI that captures errors for reporting to the browser.
    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  // webArgs contains arguments passed to templates in webhtml.go.
    76  type webArgs struct {
    77  	Title       string
    78  	Errors      []string
    79  	Total       int64
    80  	SampleTypes []string
    81  	Legend      []string
    82  	Standalone  bool // True for command-line generation of HTML
    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), // Keep legacy URL working.
   134  			"/flamegraphold": redirectWithQuery("flamegraph", http.StatusMovedPermanently), // Keep legacy URL working.
   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  			// Only allow local clients
   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  			// Fall back to default behavior
   200  			h = http.DefaultServeMux
   201  		}
   202  		h.ServeHTTP(w, req)
   203  	})
   204  
   205  	// We serve the ui at /ui/ and redirect there from the root. This is done
   206  	// to surface any problems with serving the ui at a non-root early. See:
   207  	//
   208  	// https://github.com/google/pprof/pull/348
   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  // redirectWithQuery responds with a given redirect code, preserving query
   217  // parameters in the redirect URL. It does not convert relative paths to
   218  // absolute paths like http.Redirect does, so that HTTPServerArgs.Handlers can
   219  // generate relative redirects that work with the external prefixing.
   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  	// Construct URL.
   239  	baseURL, _ := gourl.Parse(url)
   240  	current := currentConfig()
   241  	u, _ := current.makeURL(*baseURL)
   242  
   243  	// Give server a little time to get ready.
   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  	// No visualizer succeeded, so just print URL.
   258  	o.UI.PrintErr(u.String())
   259  }
   260  
   261  // makeReport generates a report for the specified command.
   262  // If configEditor is not null, it is used to edit the config used for the report.
   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  // renderHTML generates html using the named template based on the contents of data.
   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  // render responds with html generated by passing data to the named template.
   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  // dot generates a web page containing an svg diagram.
   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 // error already reported
   318  	}
   319  
   320  	// Generate dot graph.
   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  	// Convert to svg.
   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  	// Get all node names into an array.
   337  	nodes := []string{""} // dot starts with node numbered 1
   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  	// Fix dot bug related to unquoted ampersands.
   357  	svg := bytes.Replace(out.Bytes(), []byte("&;"), []byte("&;"), -1)
   358  
   359  	// Cleanup for embedding by dropping stuff before the <svg> start.
   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 // error already reported
   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  // disasm generates a web page containing disassembly.
   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 // error already reported
   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  // source generates a web page containing source code annotated with profile
   408  // data.
   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 // error already reported
   414  	}
   415  
   416  	// Generate source listing.
   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  // peek generates a web page listing callers/callers.
   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 // error already reported
   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  // saveConfig saves URL configuration.
   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  // deleteConfig deletes a configuration.
   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  // getFromLegend returns the suffix of an entry in legend that starts
   473  // with param.  It returns def if no such entry is found.
   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