stackframe.go 2.5 KB

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