stacktrace.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "sync"
  24. "go.uber.org/zap/internal/bufferpool"
  25. )
  26. var (
  27. _stacktracePool = sync.Pool{
  28. New: func() interface{} {
  29. return newProgramCounters(64)
  30. },
  31. }
  32. )
  33. func takeStacktrace(skip int) string {
  34. buffer := bufferpool.Get()
  35. defer buffer.Free()
  36. programCounters := _stacktracePool.Get().(*programCounters)
  37. defer _stacktracePool.Put(programCounters)
  38. var numFrames int
  39. for {
  40. // Skip the call to runtime.Callers and takeStacktrace so that the
  41. // program counters start at the caller of takeStacktrace.
  42. numFrames = runtime.Callers(skip+2, programCounters.pcs)
  43. if numFrames < len(programCounters.pcs) {
  44. break
  45. }
  46. // Don't put the too-short counter slice back into the pool; this lets
  47. // the pool adjust if we consistently take deep stacktraces.
  48. programCounters = newProgramCounters(len(programCounters.pcs) * 2)
  49. }
  50. i := 0
  51. frames := runtime.CallersFrames(programCounters.pcs[:numFrames])
  52. // Note: On the last iteration, frames.Next() returns false, with a valid
  53. // frame, but we ignore this frame. The last frame is a a runtime frame which
  54. // adds noise, since it's only either runtime.main or runtime.goexit.
  55. for frame, more := frames.Next(); more; frame, more = frames.Next() {
  56. if i != 0 {
  57. buffer.AppendByte('\n')
  58. }
  59. i++
  60. buffer.AppendString(frame.Function)
  61. buffer.AppendByte('\n')
  62. buffer.AppendByte('\t')
  63. buffer.AppendString(frame.File)
  64. buffer.AppendByte(':')
  65. buffer.AppendInt(int64(frame.Line))
  66. }
  67. return buffer.String()
  68. }
  69. type programCounters struct {
  70. pcs []uintptr
  71. }
  72. func newProgramCounters(size int) *programCounters {
  73. return &programCounters{make([]uintptr, size)}
  74. }