TakeStacktrace takes at most n stacks, skiping the first skip stacks.
(n, skip uint)
| 7 | |
| 8 | // TakeStacktrace takes at most n stacks, skiping the first skip stacks. |
| 9 | func TakeStacktrace(n, skip uint) []byte { |
| 10 | var buf []byte |
| 11 | pcs := make([]uintptr, n) |
| 12 | |
| 13 | // +2 to exclude runtime.Callers and TakeStacktrace |
| 14 | numFrames := runtime.Callers(2+int(skip), pcs) |
| 15 | if numFrames == 0 { |
| 16 | return buf |
| 17 | } |
| 18 | frames := runtime.CallersFrames(pcs[:numFrames]) |
| 19 | for i := 0; ; i++ { |
| 20 | frame, more := frames.Next() |
| 21 | if i != 0 { |
| 22 | buf = append(buf, '\n') |
| 23 | } |
| 24 | buf = append(buf, []byte(frame.Function)...) |
| 25 | buf = append(buf, '\n') |
| 26 | buf = append(buf, '\t') |
| 27 | buf = append(buf, []byte(frame.File)...) |
| 28 | buf = append(buf, ':') |
| 29 | buf = append(buf, []byte(strconv.Itoa(frame.Line))...) |
| 30 | if !more { |
| 31 | break |
| 32 | } |
| 33 | } |
| 34 | return buf |
| 35 | } |