capture.go 612 B

123456789101112131415161718192021222324252627
  1. package stacktrace
  2. import "runtime"
  3. // Capture 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
  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. }