2023-10-15 10:10:45 +00:00
|
|
|
// Copyright (c) 2023 Uber Technologies, Inc.
|
2021-10-12 19:56:42 +00:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Package stacktrace provides support for gathering stack traces
|
|
|
|
// efficiently.
|
|
|
|
package stacktrace
|
2021-10-12 19:56:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
2022-06-01 17:21:21 +00:00
|
|
|
"go.uber.org/zap/buffer"
|
2021-10-12 19:56:42 +00:00
|
|
|
"go.uber.org/zap/internal/bufferpool"
|
2023-10-15 10:10:45 +00:00
|
|
|
"go.uber.org/zap/internal/pool"
|
2021-10-12 19:56:42 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
var _stackPool = pool.New(func() *Stack {
|
|
|
|
return &Stack{
|
|
|
|
storage: make([]uintptr, 64),
|
|
|
|
}
|
|
|
|
})
|
2022-06-01 17:21:21 +00:00
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Stack is a captured stack trace.
|
|
|
|
type Stack struct {
|
2022-06-01 17:21:21 +00:00
|
|
|
pcs []uintptr // program counters; always a subslice of storage
|
|
|
|
frames *runtime.Frames
|
|
|
|
|
|
|
|
// The size of pcs varies depending on requirements:
|
|
|
|
// it will be one if the only the first frame was requested,
|
|
|
|
// and otherwise it will reflect the depth of the call stack.
|
|
|
|
//
|
|
|
|
// storage decouples the slice we need (pcs) from the slice we pool.
|
|
|
|
// We will always allocate a reasonably large storage, but we'll use
|
|
|
|
// only as much of it as we need.
|
|
|
|
storage []uintptr
|
|
|
|
}
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Depth specifies how deep of a stack trace should be captured.
|
|
|
|
type Depth int
|
2022-06-01 17:21:21 +00:00
|
|
|
|
|
|
|
const (
|
2023-10-15 10:10:45 +00:00
|
|
|
// First captures only the first frame.
|
|
|
|
First Depth = iota
|
2022-06-01 17:21:21 +00:00
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Full captures the entire call stack, allocating more
|
2022-06-01 17:21:21 +00:00
|
|
|
// storage for it if needed.
|
2023-10-15 10:10:45 +00:00
|
|
|
Full
|
2021-10-12 19:56:42 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Capture captures a stack trace of the specified depth, skipping
|
2022-06-01 17:21:21 +00:00
|
|
|
// the provided number of frames. skip=0 identifies the caller of
|
2023-10-15 10:10:45 +00:00
|
|
|
// Capture.
|
2022-06-01 17:21:21 +00:00
|
|
|
//
|
|
|
|
// The caller must call Free on the returned stacktrace after using it.
|
2023-10-15 10:10:45 +00:00
|
|
|
func Capture(skip int, depth Depth) *Stack {
|
|
|
|
stack := _stackPool.Get()
|
2022-06-01 17:21:21 +00:00
|
|
|
|
|
|
|
switch depth {
|
2023-10-15 10:10:45 +00:00
|
|
|
case First:
|
2022-06-01 17:21:21 +00:00
|
|
|
stack.pcs = stack.storage[:1]
|
2023-10-15 10:10:45 +00:00
|
|
|
case Full:
|
2022-06-01 17:21:21 +00:00
|
|
|
stack.pcs = stack.storage
|
2021-10-12 19:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:21:21 +00:00
|
|
|
// Unlike other "skip"-based APIs, skip=0 identifies runtime.Callers
|
|
|
|
// itself. +2 to skip captureStacktrace and runtime.Callers.
|
|
|
|
numFrames := runtime.Callers(
|
|
|
|
skip+2,
|
|
|
|
stack.pcs,
|
|
|
|
)
|
2021-10-12 19:56:42 +00:00
|
|
|
|
2022-06-01 17:21:21 +00:00
|
|
|
// runtime.Callers truncates the recorded stacktrace if there is no
|
|
|
|
// room in the provided slice. For the full stack trace, keep expanding
|
|
|
|
// storage until there are fewer frames than there is room.
|
2023-10-15 10:10:45 +00:00
|
|
|
if depth == Full {
|
2022-06-01 17:21:21 +00:00
|
|
|
pcs := stack.pcs
|
|
|
|
for numFrames == len(pcs) {
|
|
|
|
pcs = make([]uintptr, len(pcs)*2)
|
|
|
|
numFrames = runtime.Callers(skip+2, pcs)
|
2021-10-12 19:56:42 +00:00
|
|
|
}
|
2022-06-01 17:21:21 +00:00
|
|
|
|
|
|
|
// Discard old storage instead of returning it to the pool.
|
|
|
|
// This will adjust the pool size over time if stack traces are
|
|
|
|
// consistently very deep.
|
|
|
|
stack.storage = pcs
|
|
|
|
stack.pcs = pcs[:numFrames]
|
|
|
|
} else {
|
|
|
|
stack.pcs = stack.pcs[:numFrames]
|
2021-10-12 19:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:21:21 +00:00
|
|
|
stack.frames = runtime.CallersFrames(stack.pcs)
|
|
|
|
return stack
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free releases resources associated with this stacktrace
|
|
|
|
// and returns it back to the pool.
|
2023-10-15 10:10:45 +00:00
|
|
|
func (st *Stack) Free() {
|
2022-06-01 17:21:21 +00:00
|
|
|
st.frames = nil
|
|
|
|
st.pcs = nil
|
2023-10-15 10:10:45 +00:00
|
|
|
_stackPool.Put(st)
|
2022-06-01 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count reports the total number of frames in this stacktrace.
|
|
|
|
// Count DOES NOT change as Next is called.
|
2023-10-15 10:10:45 +00:00
|
|
|
func (st *Stack) Count() int {
|
2022-06-01 17:21:21 +00:00
|
|
|
return len(st.pcs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next frame in the stack trace,
|
|
|
|
// and a boolean indicating whether there are more after it.
|
2023-10-15 10:10:45 +00:00
|
|
|
func (st *Stack) Next() (_ runtime.Frame, more bool) {
|
2022-06-01 17:21:21 +00:00
|
|
|
return st.frames.Next()
|
|
|
|
}
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Take returns a string representation of the current stacktrace.
|
|
|
|
//
|
|
|
|
// skip is the number of frames to skip before recording the stack trace.
|
|
|
|
// skip=0 identifies the caller of Take.
|
|
|
|
func Take(skip int) string {
|
|
|
|
stack := Capture(skip+1, Full)
|
2022-06-01 17:21:21 +00:00
|
|
|
defer stack.Free()
|
|
|
|
|
|
|
|
buffer := bufferpool.Get()
|
|
|
|
defer buffer.Free()
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
stackfmt := NewFormatter(buffer)
|
2022-06-01 17:21:21 +00:00
|
|
|
stackfmt.FormatStack(stack)
|
2021-10-12 19:56:42 +00:00
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// Formatter formats a stack trace into a readable string representation.
|
|
|
|
type Formatter struct {
|
2022-06-01 17:21:21 +00:00
|
|
|
b *buffer.Buffer
|
|
|
|
nonEmpty bool // whehther we've written at least one frame already
|
|
|
|
}
|
|
|
|
|
2023-10-15 10:10:45 +00:00
|
|
|
// NewFormatter builds a new Formatter.
|
|
|
|
func NewFormatter(b *buffer.Buffer) Formatter {
|
|
|
|
return Formatter{b: b}
|
2022-06-01 17:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatStack formats all remaining frames in the provided stacktrace -- minus
|
|
|
|
// the final runtime.main/runtime.goexit frame.
|
2023-10-15 10:10:45 +00:00
|
|
|
func (sf *Formatter) FormatStack(stack *Stack) {
|
2022-06-01 17:21:21 +00:00
|
|
|
// Note: On the last iteration, frames.Next() returns false, with a valid
|
2023-10-15 10:10:45 +00:00
|
|
|
// frame, but we ignore this frame. The last frame is a runtime frame which
|
2022-06-01 17:21:21 +00:00
|
|
|
// adds noise, since it's only either runtime.main or runtime.goexit.
|
|
|
|
for frame, more := stack.Next(); more; frame, more = stack.Next() {
|
|
|
|
sf.FormatFrame(frame)
|
|
|
|
}
|
2021-10-12 19:56:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:21:21 +00:00
|
|
|
// FormatFrame formats the given frame.
|
2023-10-15 10:10:45 +00:00
|
|
|
func (sf *Formatter) FormatFrame(frame runtime.Frame) {
|
2022-06-01 17:21:21 +00:00
|
|
|
if sf.nonEmpty {
|
|
|
|
sf.b.AppendByte('\n')
|
|
|
|
}
|
|
|
|
sf.nonEmpty = true
|
|
|
|
sf.b.AppendString(frame.Function)
|
|
|
|
sf.b.AppendByte('\n')
|
|
|
|
sf.b.AppendByte('\t')
|
|
|
|
sf.b.AppendString(frame.File)
|
|
|
|
sf.b.AppendByte(':')
|
|
|
|
sf.b.AppendInt(int64(frame.Line))
|
2021-10-12 19:56:42 +00:00
|
|
|
}
|