1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package pprof serves via its HTTP server runtime profiling data
6 // in the format expected by the pprof visualization tool.
7 // For more information about pprof, see
8 // http://code.google.com/p/google-perftools/.
10 // The package is typically only imported for the side effect of
11 // registering its HTTP handlers.
12 // The handled paths all begin with /debug/pprof/.
14 // To use pprof, link this package into your program:
15 // import _ "http/pprof"
17 // Then use the pprof tool to look at the heap profile:
19 // pprof http://localhost:6060/debug/pprof/heap
21 // Or to look at a 30-second CPU profile:
23 // pprof http://localhost:6060/debug/pprof/profile
41 http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
42 http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
43 http.Handle("/debug/pprof/heap", http.HandlerFunc(Heap))
44 http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
47 // Cmdline responds with the running program's
48 // command line, with arguments separated by NUL bytes.
49 // The package initialization registers it as /debug/pprof/cmdline.
50 func Cmdline(w http.ResponseWriter, r *http.Request) {
51 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
52 fmt.Fprintf(w, strings.Join(os.Args, "\x00"))
55 // Heap responds with the pprof-formatted heap profile.
56 // The package initialization registers it as /debug/pprof/heap.
57 func Heap(w http.ResponseWriter, r *http.Request) {
58 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
59 pprof.WriteHeapProfile(w)
62 // Profile responds with the pprof-formatted cpu profile.
63 // The package initialization registers it as /debug/pprof/profile.
64 func Profile(w http.ResponseWriter, r *http.Request) {
65 sec, _ := strconv.Atoi64(r.FormValue("seconds"))
70 // Set Content Type assuming StartCPUProfile will work,
71 // because if it does it starts writing.
72 w.Header().Set("Content-Type", "application/octet-stream")
73 if err := pprof.StartCPUProfile(w); err != nil {
74 // StartCPUProfile failed, so no writes yet.
75 // Can change header back to text content
76 // and send error code.
77 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
78 w.WriteHeader(http.StatusInternalServerError)
79 fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
83 pprof.StopCPUProfile()
86 // Symbol looks up the program counters listed in the request,
87 // responding with a table mapping program counters to function names.
88 // The package initialization registers it as /debug/pprof/symbol.
89 func Symbol(w http.ResponseWriter, r *http.Request) {
90 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
92 // We have to read the whole POST body before
93 // writing any output. Buffer the output here.
96 // We don't know how many symbols we have, but we
97 // do have symbol information. Pprof only cares whether
98 // this number is 0 (no symbols available) or > 0.
99 fmt.Fprintf(&buf, "num_symbols: 1\n")
102 if r.Method == "POST" {
103 b = bufio.NewReader(r.Body)
105 b = bufio.NewReader(strings.NewReader(r.URL.RawQuery))
109 word, err := b.ReadSlice('+')
111 word = word[0 : len(word)-1] // trim +
113 pc, _ := strconv.Btoui64(string(word), 0)
115 f := runtime.FuncForPC(uintptr(pc))
117 fmt.Fprintf(&buf, "%#x %s\n", pc, f.Name())
121 // Wait until here to check for err; the last
122 // symbol will have an err because it doesn't end in +.
125 fmt.Fprintf(&buf, "reading request: %v\n", err)