stackframe.go 2.7 KB

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