-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace.go
149 lines (124 loc) · 3.69 KB
/
backtrace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2022 The logr Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loghelper
import (
"fmt"
"runtime"
)
// numTotalFrames specifies the maximum number of frames that are supported by
// backtrace APIs.
const numTotalFrames = 100
type traceT struct {
buf []byte
all bool
skip int
numframes int
}
func (t traceT) String() string {
return string(t.buf)
}
// BacktraceOption provides functional parameters for Backtrace.
type BacktraceOption func(*traceT)
// BacktraceAll returns backtraces for all goroutines.
// Note: This is incompatible with other options like skip/size.
// Those will be ignored when tracing all goroutines.
func BacktraceAll(all bool) BacktraceOption {
return func(t *traceT) {
t.all = all
}
}
// BacktraceSkip is like Backtrace except that it skips some stack levels.
// BacktraceSkip(0) is equivalent to Backtrace(). Has no effect when
// combined with BacktraceAll(true).
func BacktraceSkip(depth int) BacktraceOption {
return func(t *traceT) {
t.skip = depth
}
}
// BacktraceSize will limit how far the unwinding goes, i.e. specify
// how many stack frames will be printed. Has no effect when
// combined with BacktraceAll(true).
func BacktraceSize(numFrames int) BacktraceOption {
return func(t *traceT) {
if numFrames > 0 {
t.numframes = numFrames
}
}
}
// Backtrace returns an object that as default represents the stack backtrace of the calling
// goroutine. That object can be used as value in a structured logging call.
// It supports printing as string or as structured output via logr.MarshalLog.
// The behavior can be modified via options.
func Backtrace(opts ...BacktraceOption) interface{} {
trace := traceT{skip: 0, numframes: 0}
for _, opt := range opts {
opt(&trace)
}
// 'All' supersedes skip/size etc
if trace.all {
trace.buf = stacks(true)
return trace
}
pc := make([]uintptr, numTotalFrames)
// skip runtime.Callers and the klog.Backtrace API
n := runtime.Callers(trace.skip+2, pc)
if n == 0 {
// No PCs available. This can happen if the first argument to
// runtime.Callers is large.
//
// Return now to avoid processing the zero Frame that would
// otherwise be returned by frames.Next below.
return nil
}
if n > numTotalFrames {
fmt.Printf("error: runtime.Callers returned too many pcs (>%v)\n", numTotalFrames)
return nil
}
// pass only valid pcs to runtime.CallersFrames (remove goexit..)
pc = pc[:n-1]
// Account for "size" parameter
if trace.numframes > 0 && trace.numframes < n {
pc = pc[:trace.numframes]
}
frames := runtime.CallersFrames(pc)
var s string
for {
frame, more := frames.Next()
s += fmt.Sprintf("%s():\n\t%s:%v\n", frame.Function, frame.File, frame.Line)
if !more {
break
}
}
if s != "" {
trace.buf = []byte(s)
}
return trace
}
// stacks is a wrapper for runtime.Stack that attempts to recover the data for all goroutines.
func stacks(all bool) []byte {
// We don't know how big the traces are, so grow a few times if they don't fit. Start large, though.
n := 10000
if all {
n = 100000
}
var trace []byte
for i := 0; i < 5; i++ {
trace = make([]byte, n)
nbytes := runtime.Stack(trace, all)
if nbytes < len(trace) {
return trace[:nbytes]
}
n *= 2
}
return trace
}