2014-10-23 07:10:51 +08:00
|
|
|
package stacktrace
|
|
|
|
|
|
|
|
import "runtime"
|
|
|
|
|
|
|
|
// Caputure 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 + 1 // add one for our own function
|
|
|
|
frames []Frame
|
2015-07-11 02:06:06 +08:00
|
|
|
prevPc uintptr = 0
|
2014-10-23 07:10:51 +08:00
|
|
|
)
|
|
|
|
for i := skip; ; i++ {
|
|
|
|
pc, file, line, ok := runtime.Caller(i)
|
2015-07-11 02:06:06 +08:00
|
|
|
//detect if caller is repeated to avoid loop, gccgo
|
|
|
|
//currently runs into a loop without this check
|
|
|
|
if !ok || pc == prevPc {
|
2014-10-23 07:10:51 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
frames = append(frames, NewFrame(pc, file, line))
|
2015-07-11 02:06:06 +08:00
|
|
|
prevPc = pc
|
2014-10-23 07:10:51 +08:00
|
|
|
}
|
|
|
|
return Stacktrace{
|
|
|
|
Frames: frames,
|
|
|
|
}
|
|
|
|
}
|