1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package stacktrace
import "runtime"
// Capture captures a stacktrace for the current calling go program
//
// skip is the number of frames to skip
func Capture(userSkip int) Stacktrace {
var (
skip = userSkip + 2 // add one for our own function, one for runtime.Callers
frames []Frame
)
pc := make([]uintptr, 10)
n := runtime.Callers(skip, pc)
if n == 0 {
return Stacktrace{}
}
f := runtime.CallersFrames(pc)
for {
frame, more := f.Next()
frames = append(frames, newFrame(frame))
if !more {
break
}
}
return Stacktrace{
Frames: frames,
}
}
|