stack.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package errors
  2. import (
  3. "fmt"
  4. "io"
  5. "path"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. )
  10. // Frame represents a program counter inside a stack frame.
  11. type Frame uintptr
  12. // pc returns the program counter for this frame;
  13. // multiple frames may have the same PC value.
  14. func (f Frame) pc() uintptr { return uintptr(f) - 1 }
  15. // file returns the full path to the file that contains the
  16. // function for this Frame's pc.
  17. func (f Frame) file() string {
  18. fn := runtime.FuncForPC(f.pc())
  19. if fn == nil {
  20. return "unknown"
  21. }
  22. file, _ := fn.FileLine(f.pc())
  23. return file
  24. }
  25. // line returns the line number of source code of the
  26. // function for this Frame's pc.
  27. func (f Frame) line() int {
  28. fn := runtime.FuncForPC(f.pc())
  29. if fn == nil {
  30. return 0
  31. }
  32. _, line := fn.FileLine(f.pc())
  33. return line
  34. }
  35. // name returns the name of this function, if known.
  36. func (f Frame) name() string {
  37. fn := runtime.FuncForPC(f.pc())
  38. if fn == nil {
  39. return "unknown"
  40. }
  41. return fn.Name()
  42. }
  43. // Format formats the frame according to the fmt.Formatter interface.
  44. //
  45. // %s source file
  46. // %d source line
  47. // %n function name
  48. // %v equivalent to %s:%d
  49. //
  50. // Format accepts flags that alter the printing of some verbs, as follows:
  51. //
  52. // %+s function name and path of source file relative to the compile time
  53. // GOPATH separated by \n\t (<funcname>\n\t<path>)
  54. // %+v equivalent to %+s:%d
  55. func (f Frame) Format(s fmt.State, verb rune) {
  56. f.format(s, s, verb)
  57. }
  58. // format allows stack trace printing calls to be made with a bytes.Buffer.
  59. func (f Frame) format(w io.Writer, s fmt.State, verb rune) {
  60. switch verb {
  61. case 's':
  62. switch {
  63. case s.Flag('+'):
  64. io.WriteString(w, f.name())
  65. io.WriteString(w, "\n\t")
  66. io.WriteString(w, f.file())
  67. default:
  68. io.WriteString(w, path.Base(f.file()))
  69. }
  70. case 'd':
  71. io.WriteString(w, strconv.Itoa(f.line()))
  72. case 'n':
  73. io.WriteString(w, funcname(f.name()))
  74. case 'v':
  75. f.format(w, s, 's')
  76. io.WriteString(w, ":")
  77. f.format(w, s, 'd')
  78. }
  79. }
  80. // StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
  81. type StackTrace []Frame
  82. // Format formats the stack of Frames according to the fmt.Formatter interface.
  83. //
  84. // %s lists source files for each Frame in the stack
  85. // %v lists the source file and line number for each Frame in the stack
  86. //
  87. // Format accepts flags that alter the printing of some verbs, as follows:
  88. //
  89. // %+v Prints filename, function, and line number for each Frame in the stack.
  90. func (st StackTrace) Format(s fmt.State, verb rune) {
  91. switch verb {
  92. case 'v':
  93. switch {
  94. case s.Flag('+'):
  95. for _, f := range st {
  96. fmt.Fprintf(s, "\n%+v", f)
  97. }
  98. case s.Flag('#'):
  99. fmt.Fprintf(s, "%#v", []Frame(st))
  100. default:
  101. fmt.Fprintf(s, "%v", []Frame(st))
  102. }
  103. case 's':
  104. fmt.Fprintf(s, "%s", []Frame(st))
  105. }
  106. }
  107. // stack represents a stack of program counters.
  108. type stack []uintptr
  109. func (s *stack) Format(st fmt.State, verb rune) {
  110. switch verb {
  111. case 'v':
  112. switch {
  113. case st.Flag('+'):
  114. for _, pc := range *s {
  115. f := Frame(pc)
  116. fmt.Fprintf(st, "\n%+v", f)
  117. }
  118. }
  119. }
  120. }
  121. func (s *stack) StackTrace() StackTrace {
  122. f := make([]Frame, len(*s))
  123. for i := 0; i < len(f); i++ {
  124. f[i] = Frame((*s)[i])
  125. }
  126. return f
  127. }
  128. func callers() *stack {
  129. const depth = 32
  130. var pcs [depth]uintptr
  131. n := runtime.Callers(3, pcs[:])
  132. var st stack = pcs[0:n]
  133. return &st
  134. }
  135. // funcname removes the path prefix component of a function's name reported by func.Name().
  136. func funcname(name string) string {
  137. i := strings.LastIndex(name, "/")
  138. name = name[i+1:]
  139. i = strings.Index(name, ".")
  140. return name[i+1:]
  141. }