stacktrace.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 zap
  21. import (
  22. "runtime"
  23. "strings"
  24. "sync"
  25. "go.uber.org/zap/internal/bufferpool"
  26. )
  27. const _zapPackage = "go.uber.org/zap"
  28. var (
  29. _stacktracePool = sync.Pool{
  30. New: func() interface{} {
  31. return newProgramCounters(64)
  32. },
  33. }
  34. // We add "." and "/" suffixes to the package name to ensure we only match
  35. // the exact package and not any package with the same prefix.
  36. _zapStacktracePrefixes = addPrefix(_zapPackage, ".", "/")
  37. _zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...)
  38. )
  39. func takeStacktrace() string {
  40. buffer := bufferpool.Get()
  41. defer buffer.Free()
  42. programCounters := _stacktracePool.Get().(*programCounters)
  43. defer _stacktracePool.Put(programCounters)
  44. var numFrames int
  45. for {
  46. // Skip the call to runtime.Counters and takeStacktrace so that the
  47. // program counters start at the caller of takeStacktrace.
  48. numFrames = runtime.Callers(2, programCounters.pcs)
  49. if numFrames < len(programCounters.pcs) {
  50. break
  51. }
  52. // Don't put the too-short counter slice back into the pool; this lets
  53. // the pool adjust if we consistently take deep stacktraces.
  54. programCounters = newProgramCounters(len(programCounters.pcs) * 2)
  55. }
  56. i := 0
  57. skipZapFrames := true // skip all consecutive zap frames at the beginning.
  58. frames := runtime.CallersFrames(programCounters.pcs[:numFrames])
  59. // Note: On the last iteration, frames.Next() returns false, with a valid
  60. // frame, but we ignore this frame. The last frame is a a runtime frame which
  61. // adds noise, since it's only either runtime.main or runtime.goexit.
  62. for frame, more := frames.Next(); more; frame, more = frames.Next() {
  63. if skipZapFrames && isZapFrame(frame.Function) {
  64. continue
  65. } else {
  66. skipZapFrames = false
  67. }
  68. if i != 0 {
  69. buffer.AppendByte('\n')
  70. }
  71. i++
  72. buffer.AppendString(frame.Function)
  73. buffer.AppendByte('\n')
  74. buffer.AppendByte('\t')
  75. buffer.AppendString(frame.File)
  76. buffer.AppendByte(':')
  77. buffer.AppendInt(int64(frame.Line))
  78. }
  79. return buffer.String()
  80. }
  81. func isZapFrame(function string) bool {
  82. for _, prefix := range _zapStacktracePrefixes {
  83. if strings.HasPrefix(function, prefix) {
  84. return true
  85. }
  86. }
  87. // We can't use a prefix match here since the location of the vendor
  88. // directory affects the prefix. Instead we do a contains match.
  89. for _, contains := range _zapStacktraceVendorContains {
  90. if strings.Contains(function, contains) {
  91. return true
  92. }
  93. }
  94. return false
  95. }
  96. type programCounters struct {
  97. pcs []uintptr
  98. }
  99. func newProgramCounters(size int) *programCounters {
  100. return &programCounters{make([]uintptr, size)}
  101. }
  102. func addPrefix(prefix string, ss ...string) []string {
  103. withPrefix := make([]string, len(ss))
  104. for i, s := range ss {
  105. withPrefix[i] = prefix + s
  106. }
  107. return withPrefix
  108. }