Debian Codegraph @

stack/webstack/webstack.go

raw
1 // Copyright 2020 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 // Package webstack provides a http.HandlerFunc that serves a snapshot similar
6 // to net/http/pprof.Index().
7 //
8 // Contrary to net/http/pprof, the handler is not automatically registered.
9 package webstack
10
11 import (
12 "bytes"
13 "io"
14 "net/http"
15 "runtime"
16 "strconv"
17
18 "github.com/maruel/panicparse/v2/stack"
19 )
20
21 // SnapshotHandler implements http.HandlerFunc to returns a panicparse HTML
22 // format for a snapshot of the current goroutines.
23 //
24 // For best results, compile the executable with optimization (-N) and inlining
25 // (-l) disabled with -gcflags '-N -l'.
26 //
27 // Arguments are passed as form values. If you want to change the default,
28 // override the form values in a wrapper as shown in the example.
29 //
30 // The implementation is designed to be reasonably fast, it currently does a
31 // small amount of disk I/O only for file presence.
32 //
33 // It is a direct replacement for "/debug/pprof/goroutine?debug=2" handler in
34 // net/http/pprof.
35 //
36 // augment: (default: 1) When set to 1, panicparse tries to find the sources on
37 // disk to improve the display of arguments based on type information. This is
38 // slower and should be avoided on high utilization server.
39 //
40 // maxmem: (default: 67108864) maximum amount of temporary memory to use to
41 // generate a snapshot. In practice at least the double of this is used.
42 // Minimum is 1048576.
43 //
44 // similarity: (default: "anypointer") Can be one of stack.Similarity value in
45 // lowercase: "exactflags", "exactlines", "anypointer" or "anyvalue".
46 func SnapshotHandler(w http.ResponseWriter, req *http.Request) {
47 if req.Method != "GET" {
48 http.Error(w, "invalid method", http.StatusMethodNotAllowed)
49 return
50 }
51
52 maxmem := 64 << 20
53 if s := req.FormValue("maxmem"); s != "" {
54 var err error
55 if maxmem, err = strconv.Atoi(s); err != nil {
56 http.Error(w, "invalid maxmem value", http.StatusBadRequest)
57 return
58 }
59 }
60 opts := stack.DefaultOpts()
61 if s := req.FormValue("augment"); s != "" {
62 v, err := strconv.Atoi(s)
63 if err != nil || v < 0 || v > 1 {
64 http.Error(w, "invalid augment value", http.StatusBadRequest)
65 return
66 }
67 if v == 0 {
68 opts.AnalyzeSources = false
69 }
70 }
71 c, err := snapshot(maxmem, opts)
72 if err != nil {
73 http.Error(w, "failed to process the snapshot, try a larger maxmem value", http.StatusInternalServerError)
74 return
75 }
76
77 var s stack.Similarity
78 switch req.FormValue("similarity") {
79 case "exactflags":
80 s = stack.ExactFlags
81 case "exactlines":
82 s = stack.ExactLines
83 case "anypointer", "":
84 s = stack.AnyPointer
85 case "anyvalue":
86 s = stack.AnyValue
87 default:
88 http.Error(w, "invalid similarity value", http.StatusBadRequest)
89 return
90 }
91
92 w.Header().Set("Content-Type", "text/html; charset=utf-8")
93 _ = c.Aggregate(s).ToHTML(w, "")
94 }
95
96 // snapshot returns a Context based on the snapshot of the stacks of the
97 // current process.
98 func snapshot(maxmem int, opts *stack.Opts) (*stack.Snapshot, error) {
99 // We don't know how big the buffer needs to be to collect all the
100 // goroutines. Start with 1 MB and try a few times, doubling each time. Give
101 // up and use a truncated trace if maxmem is not enough.
102 buf := make([]byte, 1<<20)
103 if maxmem < len(buf) {
104 maxmem = len(buf)
105 }
106 for i := 0; ; i++ {
107 n := runtime.Stack(buf, true)
108 if n < len(buf) {
109 buf = buf[:n]
110 break
111 }
112 if len(buf) >= maxmem {
113 break
114 }
115 l := len(buf) * 2
116 if l > maxmem {
117 l = maxmem
118 }
119 buf = make([]byte, l)
120 }
121 s, _, err := stack.ScanSnapshot(bytes.NewReader(buf), io.Discard, opts)
122 // That's expected.
123 if err == io.EOF {
124 err = nil
125 }
126 return s, err
127 }