From b7d37378b12d4e03dbf2a8e7118907453689ea9f Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 9 Oct 2018 13:38:09 -0700 Subject: [PATCH] utils: Add function to write goroutine stacks to a file Signed-off-by: Mrunal Patel --- utils/utils.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/utils.go b/utils/utils.go index 272b2e2d589..bee5754009e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "os" "os/exec" "runtime" "strings" @@ -159,3 +160,16 @@ func WriteGoroutineStacks(w io.Writer) error { _, err := w.Write(buf) return err } + +// WriteGoroutineStacksToFile write goroutine stacks +// to the specified file. +func WriteGoroutineStacksToFile(path string) error { + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + return err + } + defer f.Close() + defer f.Sync() + + return WriteGoroutineStacks(f) +}