stack.go 3.8 KB

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