frame_go1_12.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.13
  5. package xerrors
  6. import (
  7. "runtime"
  8. )
  9. // A Frame contains part of a call stack.
  10. type Frame struct {
  11. // Make room for three PCs: the one we were asked for, what it called,
  12. // and possibly a PC for skipPleaseUseCallersFrames. See:
  13. // https://go.googlesource.com/go/+/032678e0fb/src/runtime/extern.go#169
  14. frames [3]uintptr
  15. }
  16. // Caller returns a Frame that describes a frame on the caller's stack.
  17. // The argument skip is the number of frames to skip over.
  18. // Caller(0) returns the frame for the caller of Caller.
  19. func Caller(skip int) Frame {
  20. var s Frame
  21. runtime.Callers(skip+1, s.frames[:])
  22. return s
  23. }
  24. // location reports the file, line, and function of a frame.
  25. //
  26. // The returned function may be "" even if file and line are not.
  27. func (f Frame) location() (function, file string, line int) {
  28. frames := runtime.CallersFrames(f.frames[:])
  29. if _, ok := frames.Next(); !ok {
  30. return "", "", 0
  31. }
  32. fr, ok := frames.Next()
  33. if !ok {
  34. return "", "", 0
  35. }
  36. return fr.Function, fr.File, fr.Line
  37. }
  38. // Format prints the stack as error detail.
  39. // It should be called from an error's Format implementation
  40. // after printing any other error detail.
  41. func (f Frame) Format(p Printer) {
  42. if p.Detail() {
  43. function, file, line := f.location()
  44. if function != "" {
  45. p.Printf("%s\n ", function)
  46. }
  47. if file != "" {
  48. p.Printf("%s:%d\n", file, line)
  49. }
  50. }
  51. }