diff --git a/Gopkg.lock b/Gopkg.lock index 716e76bb3..b0b6ec9ee 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -53,12 +53,6 @@ packages = [".","client","client/changelist","cryptoservice","storage","trustmanager","trustmanager/yubikey","trustpinning","tuf","tuf/data","tuf/signed","tuf/utils","tuf/validation"] revision = "fab4c6708bc200c4cfe56c74f902d6e8bb26c803" -[[projects]] - branch = "master" - name = "github.com/e-dard/netbug" - packages = ["."] - revision = "e64d308a0b205c901264e88a10e70d64acb1810d" - [[projects]] name = "github.com/go-kit/kit" packages = ["endpoint","log","log/level","transport/grpc"] @@ -200,6 +194,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "40165f65135cf5c4a677395ad6c4675aa3d322a8b618dca1e5e49ae89b29d253" + inputs-digest = "1c1aaef7211b2a2a41be582cbe36547df6ef364b5f2e13aeaeb54bd1ff9c8ed8" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index b268f2410..66fddc2c7 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -45,7 +45,3 @@ [[constraint]] name = "google.golang.org/grpc" - -[[constraint]] - branch = "master" - name = "github.com/e-dard/netbug" diff --git a/debug/debug.go b/debug/debug.go index ca3148112..ec0663da2 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -3,14 +3,18 @@ package debug import ( "context" "fmt" + "html/template" "io/ioutil" "net" "net/http" + nhpprof "net/http/pprof" + "net/url" "os" "os/signal" + "runtime/pprof" + "strings" "syscall" - "github.com/e-dard/netbug" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/google/uuid" @@ -18,6 +22,7 @@ import ( ) const debugSignal = syscall.SIGUSR1 +const debugPrefix = "/debug/" // AttachDebugHandler will attach a signal handler that will toggle the debug // server state when SIGUSR1 is sent to the process. @@ -44,11 +49,12 @@ func AttachDebugHandler(addrPath string, logger log.Logger) { "msg", "error shutting down debug server", "err", err, ) - } else { - level.Info(logger).Log( - "msg", "shutdown debug server", - ) + continue } + + level.Info(logger).Log( + "msg", "shutdown debug server", + ) } }() } @@ -62,7 +68,7 @@ func startDebugServer(addrPath string, logger log.Logger) (*http.Server, error) // Start the debug server r := http.NewServeMux() - netbug.RegisterAuthHandler(token.String(), "/debug/", r) + registerAuthHandler(token.String(), r, logger) serv := http.Server{ Handler: r, } @@ -90,3 +96,83 @@ func startDebugServer(addrPath string, logger log.Logger) (*http.Server, error) return &serv, nil } + +// The below handler code is adapted from MIT licensed github.com/e-dard/netbug +func handler(token string, logger log.Logger) http.Handler { + info := struct { + Profiles []*pprof.Profile + Token string + }{ + Profiles: pprof.Profiles(), + Token: url.QueryEscape(token), + } + + h := func(w http.ResponseWriter, r *http.Request) { + name := strings.TrimPrefix(r.URL.Path, "/") + switch name { + case "": + // Index page. + if err := indexTmpl.Execute(w, info); err != nil { + level.Info(logger).Log( + "msg", "error rendering debug template", + "err", err, + ) + return + } + case "cmdline": + nhpprof.Cmdline(w, r) + case "profile": + nhpprof.Profile(w, r) + case "trace": + nhpprof.Trace(w, r) + case "symbol": + nhpprof.Symbol(w, r) + default: + // Provides access to all profiles under runtime/pprof + nhpprof.Handler(name).ServeHTTP(w, r) + } + } + return http.HandlerFunc(h) +} + +func authHandler(token string, logger log.Logger) http.Handler { + h := handler(token, logger) + ah := func(w http.ResponseWriter, r *http.Request) { + if r.FormValue("token") == token { + h.ServeHTTP(w, r) + } else { + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprintln(w, "Unauthorized.") + } + } + return http.HandlerFunc(ah) +} + +func registerAuthHandler(token string, mux *http.ServeMux, logger log.Logger) { + mux.Handle(debugPrefix, http.StripPrefix(debugPrefix, authHandler(token, logger))) +} + +var indexTmpl = template.Must(template.New("index").Parse(` + + Debug Information + +
+ + profiles:
+ + {{range .Profiles}} +
{{.Count}}{{.Name}} + {{end}} +
CPU +
5-second trace +
30-second trace +
+
+ debug information:
+ +
cmdline +
symbol +
full goroutine stack dump
+ + +`))