capture.go 617 B

12345678910111213141516171819202122232425262728
  1. package stacktrace
  2. import "runtime"
  3. // Caputure captures a stacktrace for the current calling go program
  4. //
  5. // skip is the number of frames to skip
  6. func Capture(userSkip int) Stacktrace {
  7. var (
  8. skip = userSkip + 1 // add one for our own function
  9. frames []Frame
  10. prevPc uintptr = 0
  11. )
  12. for i := skip; ; i++ {
  13. pc, file, line, ok := runtime.Caller(i)
  14. //detect if caller is repeated to avoid loop, gccgo
  15. //currently runs into a loop without this check
  16. if !ok || pc == prevPc {
  17. break
  18. }
  19. frames = append(frames, NewFrame(pc, file, line))
  20. prevPc = pc
  21. }
  22. return Stacktrace{
  23. Frames: frames,
  24. }
  25. }