stacktrace.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // CapturedStacktrace represents a stacktrace captured by a previous call
  40. // to log.Stacktrace. If passed to a logging function, the stacktrace
  41. // will be appended.
  42. type CapturedStacktrace string
  43. // Stacktrace captures a stacktrace of the current goroutine and returns
  44. // it to be passed to a logging function.
  45. func Stacktrace() CapturedStacktrace {
  46. return CapturedStacktrace(takeStacktrace())
  47. }
  48. func takeStacktrace() string {
  49. programCounters := _stacktracePool.Get().(*programCounters)
  50. defer _stacktracePool.Put(programCounters)
  51. var buffer bytes.Buffer
  52. for {
  53. // Skip the call to runtime.Counters and takeStacktrace so that the
  54. // program counters start at the caller of takeStacktrace.
  55. n := runtime.Callers(2, programCounters.pcs)
  56. if n < cap(programCounters.pcs) {
  57. programCounters.pcs = programCounters.pcs[:n]
  58. break
  59. }
  60. // Don't put the too-short counter slice back into the pool; this lets
  61. // the pool adjust if we consistently take deep stacktraces.
  62. programCounters = newProgramCounters(len(programCounters.pcs) * 2)
  63. }
  64. i := 0
  65. frames := runtime.CallersFrames(programCounters.pcs)
  66. for frame, more := frames.Next(); more; frame, more = frames.Next() {
  67. if shouldIgnoreStacktraceFunction(frame.Function) {
  68. continue
  69. }
  70. if i != 0 {
  71. buffer.WriteByte('\n')
  72. }
  73. i++
  74. buffer.WriteString(frame.Function)
  75. buffer.WriteByte('\n')
  76. buffer.WriteByte('\t')
  77. buffer.WriteString(frame.File)
  78. buffer.WriteByte(':')
  79. buffer.WriteString(strconv.Itoa(int(frame.Line)))
  80. }
  81. return buffer.String()
  82. }
  83. func shouldIgnoreStacktraceFunction(function string) bool {
  84. for _, prefix := range _stacktraceIgnorePrefixes {
  85. if strings.HasPrefix(function, prefix) {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. type programCounters struct {
  92. pcs []uintptr
  93. }
  94. func newProgramCounters(size int) *programCounters {
  95. return &programCounters{make([]uintptr, size)}
  96. }