aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-22 18:24:35 -0400
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-22 18:24:35 -0400
commit5a011b89abc5f9d6d7b5333121659c2767263fe0 (patch)
treea3a1e465fc9201091a0d91b876dff71b59391ae0
parent3cdf864c19294adb6dba97e9c4ce6abc5416cb7d (diff)
downloadAtlas-5a011b89abc5f9d6d7b5333121659c2767263fe0.tar.gz
Atlas-5a011b89abc5f9d6d7b5333121659c2767263fe0.zip
cmd/atlas: Add support for running an insecure debug server
-rw-r--r--cmd/atlas/main.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/cmd/atlas/main.go b/cmd/atlas/main.go
index 65a801b..e019b4e 100644
--- a/cmd/atlas/main.go
+++ b/cmd/atlas/main.go
@@ -5,10 +5,14 @@ import (
"context"
"errors"
"fmt"
+ "net/http"
"os"
"os/signal"
+ "strings"
"syscall"
+ "net/http/pprof"
+
"github.com/hashicorp/go-envparse"
_ "github.com/mattn/go-sqlite3"
"github.com/r2northstar/atlas/pkg/atlas"
@@ -49,6 +53,15 @@ func main() {
}
}
+ if dbgAddr, _ := getEnvList("INSECURE_DEBUG_SERVER_ADDR", e, os.Environ()); dbgAddr != "" {
+ go func() {
+ fmt.Fprintf(os.Stderr, "warning: running insecure debug server on %q\n", dbgAddr)
+ if err := runDebugServer(dbgAddr); err != nil {
+ fmt.Fprintf(os.Stderr, "warning: failed to start debug server: %v\n", err)
+ }
+ }()
+ }
+
var c atlas.Config
if err := c.UnmarshalEnv(e, false); err != nil {
fmt.Fprintf(os.Stderr, "error: parse config: %v\n", err)
@@ -80,6 +93,27 @@ func main() {
}
}
+func runDebugServer(addr string) error {
+ m := http.NewServeMux()
+ m.HandleFunc("/debug/pprof/", pprof.Index)
+ m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
+ m.HandleFunc("/debug/pprof/profile", pprof.Profile)
+ m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
+ m.HandleFunc("/debug/pprof/trace", pprof.Trace)
+ return http.ListenAndServe(addr, m)
+}
+
+func getEnvList(k string, e ...[]string) (string, bool) {
+ for _, l := range e {
+ for _, x := range l {
+ if xk, xv, ok := strings.Cut(x, "="); ok && xk == k {
+ return xv, true
+ }
+ }
+ }
+ return "", false
+}
+
func readEnv(name string) ([]string, error) {
f, err := os.Open(name)
if err != nil {