Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / http / pprof / pprof.go
blobf7db9aab93bf220d916c83e27f943a63fa1ef5fc
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/.
9 //
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 package pprof
23 import (
24 "bufio"
25 "fmt"
26 "http"
27 "os"
28 "runtime"
29 "runtime/pprof"
30 "strconv"
31 "strings"
34 func init() {
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")
66 var b *bufio.Reader
67 if r.Method == "POST" {
68 b = bufio.NewReader(r.Body)
69 } else {
70 b = bufio.NewReader(strings.NewReader(r.URL.RawQuery))
73 for {
74 word, err := b.ReadSlice('+')
75 if err == nil {
76 word = word[0 : len(word)-1] // trim +
78 pc, _ := strconv.Btoui64(string(word), 0)
79 if pc != 0 {
80 f := runtime.FuncForPC(uintptr(pc))
81 if f != nil {
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 +.
88 if err != nil {
89 break