utils: Add function to write goroutine stacks
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os/exec"
+ "runtime"
"strings"
systemdDbus "github.com/coreos/go-systemd/dbus"
@@ -138,3 +139,23 @@ func CopyDetachable(dst io.Writer, src io.Reader, keys []byte) (written int64, e
}
return written, err
}
+
+// WriteGoroutineStacks writes out the goroutine stacks
+// of the caller. Up to 32 MB is allocated to print the
+// stack.
+func WriteGoroutineStacks(w io.Writer) error {
+ buf := make([]byte, 1<<20)
+ for i := 0; ; i++ {
+ n := runtime.Stack(buf, true)
+ if n < len(buf) {
+ buf = buf[:n]
+ break
+ }
+ if len(buf) >= 32<<20 {
+ break
+ }
+ buf = make([]byte, 2*len(buf))
+ }
+ _, err := w.Write(buf)
+ return err
+}
