OSDN Git Service

0bac26687d73d842b99793d5b75c142d8ee4a053
[pf3gnuchains/gcc-fork.git] / libgo / go / http / pprof / pprof.go
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.
4
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/.
13 //
14 // To use pprof, link this package into your program:
15 //      import _ "http/pprof"
16 //
17 // Then use the pprof tool to look at the heap profile:
18 //
19 //      pprof http://localhost:6060/debug/pprof/heap
20 //
21 package pprof
22
23 import (
24         "bufio"
25         "fmt"
26         "http"
27         "os"
28         "runtime"
29         "runtime/pprof"
30         "strconv"
31         "strings"
32 )
33
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))
38 }
39
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.Header().Set("content-type", "text/plain; charset=utf-8")
45         fmt.Fprintf(w, strings.Join(os.Args, "\x00"))
46 }
47
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.Header().Set("content-type", "text/plain; charset=utf-8")
52         pprof.WriteHeapProfile(w)
53 }
54
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.Header().Set("content-type", "text/plain; charset=utf-8")
60
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")
65
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))
71         }
72
73         for {
74                 word, err := b.ReadSlice('+')
75                 if err == nil {
76                         word = word[0 : len(word)-1] // trim +
77                 }
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())
83                         }
84                 }
85
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
90                 }
91         }
92 }