2023-08-17 03:03:57 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build !js && !wasm
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
2024-03-22 13:58:09 +00:00
|
|
|
"fmt"
|
2023-08-17 03:03:57 +00:00
|
|
|
"net/http"
|
2024-03-21 04:02:33 +00:00
|
|
|
"runtime"
|
2023-08-17 03:03:57 +00:00
|
|
|
"runtime/pprof"
|
2024-03-21 04:02:33 +00:00
|
|
|
"strconv"
|
2023-08-17 03:03:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
c2nLogHeap = func(w http.ResponseWriter, r *http.Request) {
|
2024-03-21 04:02:33 +00:00
|
|
|
// Support same optional gc parameter as net/http/pprof:
|
|
|
|
if gc, _ := strconv.Atoi(r.FormValue("gc")); gc > 0 {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
2023-08-17 03:03:57 +00:00
|
|
|
pprof.WriteHeapProfile(w)
|
|
|
|
}
|
2024-03-22 13:58:09 +00:00
|
|
|
|
|
|
|
c2nPprof = func(w http.ResponseWriter, r *http.Request, profile string) {
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
p := pprof.Lookup(string(profile))
|
|
|
|
if p == nil {
|
|
|
|
http.Error(w, "Unknown profile", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc, _ := strconv.Atoi(r.FormValue("gc"))
|
|
|
|
if profile == "heap" && gc > 0 {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
debug, _ := strconv.Atoi(r.FormValue("debug"))
|
|
|
|
if debug != 0 {
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, profile))
|
|
|
|
}
|
|
|
|
p.WriteTo(w, debug)
|
|
|
|
}
|
2023-08-17 03:03:57 +00:00
|
|
|
}
|