| 1 | package profile |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "runtime" |
| 6 | ) |
| 7 | |
| 8 | // WriteAllGoroutineStacks writes a stack trace to the given writer. |
| 9 | // This is distinct from the Go-provided method because it does not truncate after 64 MB. |
| 10 | func WriteAllGoroutineStacks(w io.Writer) error { |
| 11 | // this is based on pprof.writeGoroutineStacks, and removes the 64 MB limit |
| 12 | buf := make([]byte, 1<<20) |
| 13 | for i := 0; ; i++ { |
| 14 | n := runtime.Stack(buf, true) |
| 15 | if n < len(buf) { |
| 16 | buf = buf[:n] |
| 17 | break |
| 18 | } |
| 19 | // if len(buf) >= 64<<20 { |
| 20 | // // Filled 64 MB - stop there. |
| 21 | // break |
| 22 | // } |
| 23 | buf = make([]byte, 2*len(buf)) |
| 24 | } |
| 25 | _, err := w.Write(buf) |
| 26 | return err |
| 27 | } |