stacktrace.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package hclog
  21. import (
  22. "bytes"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. )
  28. var (
  29. _stacktraceIgnorePrefixes = []string{
  30. "runtime.goexit",
  31. "runtime.main",
  32. }
  33. _stacktracePool = sync.Pool{
  34. New: func() interface{} {
  35. return newProgramCounters(64)
  36. },
  37. }
  38. )
  39. // A stacktrace gathered by a previous call to log.Stacktrace. If passed
  40. // to a logging function, the stacktrace will be appended.
  41. type CapturedStacktrace string
  42. // Gather a stacktrace of the current goroutine and return it to be passed
  43. // to a logging function.
  44. func Stacktrace() CapturedStacktrace {
  45. return CapturedStacktrace(takeStacktrace())
  46. }
  47. func takeStacktrace() string {
  48. programCounters := _stacktracePool.Get().(*programCounters)
  49. defer _stacktracePool.Put(programCounters)
  50. var buffer bytes.Buffer
  51. for {
  52. // Skip the call to runtime.Counters and takeStacktrace so that the
  53. // program counters start at the caller of takeStacktrace.
  54. n := runtime.Callers(2, programCounters.pcs)
  55. if n < cap(programCounters.pcs) {
  56. programCounters.pcs = programCounters.pcs[:n]
  57. break
  58. }
  59. // Don't put the too-short counter slice back into the pool; this lets
  60. // the pool adjust if we consistently take deep stacktraces.
  61. programCounters = newProgramCounters(len(programCounters.pcs) * 2)
  62. }
  63. i := 0
  64. frames := runtime.CallersFrames(programCounters.pcs)
  65. for frame, more := frames.Next(); more; frame, more = frames.Next() {
  66. if shouldIgnoreStacktraceFunction(frame.Function) {
  67. continue
  68. }
  69. if i != 0 {
  70. buffer.WriteByte('\n')
  71. }
  72. i++
  73. buffer.WriteString(frame.Function)
  74. buffer.WriteByte('\n')
  75. buffer.WriteByte('\t')
  76. buffer.WriteString(frame.File)
  77. buffer.WriteByte(':')
  78. buffer.WriteString(strconv.Itoa(int(frame.Line)))
  79. }
  80. return buffer.String()
  81. }
  82. func shouldIgnoreStacktraceFunction(function string) bool {
  83. for _, prefix := range _stacktraceIgnorePrefixes {
  84. if strings.HasPrefix(function, prefix) {
  85. return true
  86. }
  87. }
  88. return false
  89. }
  90. type programCounters struct {
  91. pcs []uintptr
  92. }
  93. func newProgramCounters(size int) *programCounters {
  94. return &programCounters{make([]uintptr, size)}
  95. }