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
35 http
.Handle("/debug/pprof/cmdline", http
.HandlerFunc(Cmdline
))
36 http
.Handle("/debug/pprof/heap", http
.HandlerFunc(Heap
))
37 http
.Handle("/debug/pprof/symbol", http
.HandlerFunc(Symbol
))
40 // Cmdline responds with the running program's
41 // command line, with arguments separated by NUL bytes.
42 // The package initialization registers it as /debug/pprof/cmdline.
43 func Cmdline(w http
.ResponseWriter
, r
*http
.Request
) {
44 w
.SetHeader("content-type", "text/plain; charset=utf-8")
45 fmt
.Fprintf(w
, strings
.Join(os
.Args
, "\x00"))
48 // Heap responds with the pprof-formatted heap profile.
49 // The package initialization registers it as /debug/pprof/heap.
50 func Heap(w http
.ResponseWriter
, r
*http
.Request
) {
51 w
.SetHeader("content-type", "text/plain; charset=utf-8")
52 pprof
.WriteHeapProfile(w
)
55 // Symbol looks up the program counters listed in the request,
56 // responding with a table mapping program counters to function names.
57 // The package initialization registers it as /debug/pprof/symbol.
58 func Symbol(w http
.ResponseWriter
, r
*http
.Request
) {
59 w
.SetHeader("content-type", "text/plain; charset=utf-8")
61 // We don't know how many symbols we have, but we
62 // do have symbol information. Pprof only cares whether
63 // this number is 0 (no symbols available) or > 0.
64 fmt
.Fprintf(w
, "num_symbols: 1\n")
67 if r
.Method
== "POST" {
68 b
= bufio
.NewReader(r
.Body
)
70 b
= bufio
.NewReader(strings
.NewReader(r
.URL
.RawQuery
))
74 word
, err
:= b
.ReadSlice('+')
76 word
= word
[0 : len(word
)-1] // trim +
78 pc
, _
:= strconv
.Btoui64(string(word
), 0)
80 f
:= runtime
.FuncForPC(uintptr(pc
))
82 fmt
.Fprintf(w
, "%#x %s\n", pc
, f
.Name())
86 // Wait until here to check for err; the last
87 // symbol will have an err because it doesn't end in +.