SnapshotHandler implements http.HandlerFunc to returns a panicparse HTML format for a snapshot of the current goroutines. For best results, compile the executable with optimization (-N) and inlining (-l) disabled with -gcflags '-N -l'. Arguments are passed as form values. If you want to change the
(w http.ResponseWriter, req *http.Request)
| 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. |
searching dependent graphs…