Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share callstack b/w Caller and Stack #808

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,27 @@ func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
return ce
}

// If a stack trace was requested, capture the entire stack.
stackLimit := 0

// For historical reasons, stack traces reported by Zap don't respect
// CallerSkip. See also #727.
stackSkip := callerSkipOffset

if !addStack {
stackLimit = 1
stackSkip += log.callerSkip
}

programCounters := newProgramCounters()
defer programCounters.Release()

pcs := programCounters.Callers(callerSkipOffset)
pcs := programCounters.Callers(stackSkip, stackLimit)
if log.addCaller {
callerIdx := log.callerSkip
if len(pcs) <= callerIdx {
if len(pcs) == 0 {
ce.Entry.Caller = zapcore.NewEntryCaller(0, "", 0, false)
} else {
frame, _ := runtime.CallersFrames(pcs[callerIdx:]).Next()
frame, _ := runtime.CallersFrames(pcs).Next()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are adding caller and stack, then the stackSkip passed in is 0 (e.g., no caller skip), so the caller value used here would be incorrect right, since we wouldn't be respecting the log.callerSkip

ce.Entry.Caller = zapcore.NewEntryCaller(frame.PC, frame.File, frame.Line, true)
}

Expand Down
33 changes: 27 additions & 6 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func takeStacktrace() string {
pcs := newProgramCounters()
defer pcs.Release()

return makeStacktrace(pcs.Callers(1))
return makeStacktrace(pcs.Callers(1 /* skip */, 0 /* limit */))
}

func makeStacktrace(pcs []uintptr) string {
Expand Down Expand Up @@ -113,19 +113,40 @@ func (pcs *programCounters) Release() {
_stacktracePool.Put(pcs)
}

func (pcs *programCounters) Callers(skip int) []uintptr {
// Callers captures program counters of callers of the current function.
//
// skip specifies the number of functions to skip.
// limit specifies how deep into the stack we go, zero meaning unlimited.
func (pcs *programCounters) Callers(skip int, limit int) []uintptr {
pc := pcs.pcs
if limit > 0 {
// The shortened slice will never be placed back into the
// pool, but the longer one will.
if limit < len(pc) {
pc = pc[:limit]
} else {
pc = make([]uintptr, limit)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case never gets hit in prod right? anyway we can refactor the change to avoid adding a case that's not necessary in prod?

pcs.pcs = pc
}
}

var numFrames int
for {
// Skip the call to runtime.Callers and programCounter.Callers
// so that the program counters start at our caller.
numFrames = runtime.Callers(skip+2, pcs.pcs)
if numFrames < len(pcs.pcs) {
return pcs.pcs[:numFrames]
numFrames = runtime.Callers(skip+2, pc)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do numFrames := since we don't use the value outside of the for loop and always replace it


// All required frames have been captured if the limit was
// non-zero or if the stack depth is smaller than the size of
// the slice.
if limit > 0 || numFrames < len(pc) {
return pc[:numFrames]
}

// Don't put the too-short counter slice back into the pool; this lets
// the pool adjust if we consistently take deep stacktraces.
pcs.pcs = make([]uintptr, len(pcs.pcs)*2)
pc = make([]uintptr, len(pcs.pcs)*2)
pcs.pcs = pc
}
}

Expand Down