stackframe.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package errors
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "os"
  7. "runtime"
  8. "strings"
  9. )
  10. // A StackFrame contains all necessary information about to generate a line
  11. // in a callstack.
  12. type StackFrame struct {
  13. // The path to the file containing this ProgramCounter
  14. File string
  15. // The LineNumber in that file
  16. LineNumber int
  17. // The Name of the function that contains this ProgramCounter
  18. Name string
  19. // The Package that contains this function
  20. Package string
  21. // The underlying ProgramCounter
  22. ProgramCounter uintptr
  23. }
  24. // NewStackFrame popoulates a stack frame object from the program counter.
  25. func NewStackFrame(pc uintptr) (frame StackFrame) {
  26. frame = StackFrame{ProgramCounter: pc}
  27. if frame.Func() == nil {
  28. return
  29. }
  30. frame.Package, frame.Name = packageAndName(frame.Func())
  31. // pc -1 because the program counters we use are usually return addresses,
  32. // and we want to show the line that corresponds to the function call
  33. frame.File, frame.LineNumber = frame.Func().FileLine(pc - 1)
  34. return
  35. }
  36. // Func returns the function that contained this frame.
  37. func (frame *StackFrame) Func() *runtime.Func {
  38. if frame.ProgramCounter == 0 {
  39. return nil
  40. }
  41. return runtime.FuncForPC(frame.ProgramCounter)
  42. }
  43. // String returns the stackframe formatted in the same way as go does
  44. // in runtime/debug.Stack()
  45. func (frame *StackFrame) String() string {
  46. str := fmt.Sprintf("%s:%d (0x%x)\n", frame.File, frame.LineNumber, frame.ProgramCounter)
  47. source, err := frame.SourceLine()
  48. if err != nil {
  49. return str
  50. }
  51. return str + fmt.Sprintf("\t%s: %s\n", frame.Name, source)
  52. }
  53. // SourceLine gets the line of code (from File and Line) of the original source if possible.
  54. func (frame *StackFrame) SourceLine() (string, error) {
  55. if frame.LineNumber <= 0 {
  56. return "???", nil
  57. }
  58. file, err := os.Open(frame.File)
  59. if err != nil {
  60. return "", New(err)
  61. }
  62. defer file.Close()
  63. scanner := bufio.NewScanner(file)
  64. currentLine := 1
  65. for scanner.Scan() {
  66. if currentLine == frame.LineNumber {
  67. return string(bytes.Trim(scanner.Bytes(), " \t")), nil
  68. }
  69. currentLine++
  70. }
  71. if err := scanner.Err(); err != nil {
  72. return "", New(err)
  73. }
  74. return "???", nil
  75. }
  76. func packageAndName(fn *runtime.Func) (string, string) {
  77. name := fn.Name()
  78. pkg := ""
  79. // The name includes the path name to the package, which is unnecessary
  80. // since the file name is already included. Plus, it has center dots.
  81. // That is, we see
  82. // runtime/debug.*T·ptrmethod
  83. // and want
  84. // *T.ptrmethod
  85. // Since the package path might contains dots (e.g. code.google.com/...),
  86. // we first remove the path prefix if there is one.
  87. if lastslash := strings.LastIndex(name, "/"); lastslash >= 0 {
  88. pkg += name[:lastslash] + "/"
  89. name = name[lastslash+1:]
  90. }
  91. if period := strings.Index(name, "."); period >= 0 {
  92. pkg += name[:period]
  93. name = name[period+1:]
  94. }
  95. name = strings.Replace(name, "·", ".", -1)
  96. return pkg, name
  97. }