-
Notifications
You must be signed in to change notification settings - Fork 107
/
crash.go
243 lines (209 loc) · 7.2 KB
/
crash.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Package crash provides a framework for adding probabilistic crash points. The
// package provides a global singleton that can be used to register, configure,
// and trigger crashes.
package crash
import (
"fmt"
"os"
"runtime"
"sync"
"time"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/oasisprotocol/oasis-core/go/common/logging"
"github.com/oasisprotocol/oasis-core/go/common/random"
cmdFlags "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common/flags"
)
var testForceEnable bool
const (
// defaultCLIPrefix is the default CLI prefix used to configure crash points in
// viper and cobra.
defaultCLIPrefix = "debug.crash"
// CfgDefaultCrashPointProbability is the default crash point probability.
CfgDefaultCrashPointProbability = "debug.crash.default"
// CrashDefaultExitCode is the exit code that will be used if program is exited
// due to a debug crash invocation.
CrashDefaultExitCode = 123
)
// RandomProvider interface that provides a Float64 random.
type RandomProvider interface {
Float64() float64
}
// Crasher is a crash controller.
type Crasher struct {
CrashPointConfig *sync.Map
CrashMethod func()
CLIPrefix string
Rand RandomProvider
logger *logging.Logger
// callerSkip is used by the global crasher instance to determine the caller
// of the package level `Here` function.
callerSkip int
}
// CrasherOptions options used to create a new crasher.
type CrasherOptions struct {
CrashMethod func()
CLIPrefix string
Rand RandomProvider
CallerSkip int
}
func newDefaultRandomProvider() RandomProvider {
// Seed randomness using time by default.
return random.NewRand(time.Now().Unix())
}
func defaultCrashMethod() {
os.Exit(CrashDefaultExitCode)
}
var crashGlobal *Crasher
func init() {
crashGlobal = New(CrasherOptions{
// Skip 2 stack frames to get the correct caller information.
// 2 since the global crasher is never invoked directly, but via the
// Here() function.
CallerSkip: 2,
CLIPrefix: defaultCLIPrefix,
})
}
// New creates a new crasher.
func New(options CrasherOptions) *Crasher {
if options.CrashMethod == nil {
options.CrashMethod = defaultCrashMethod
}
if options.Rand == nil {
options.Rand = newDefaultRandomProvider()
}
crasher := &Crasher{
CrashPointConfig: &sync.Map{},
CrashMethod: options.CrashMethod,
Rand: options.Rand,
CLIPrefix: options.CLIPrefix,
logger: logging.GetLogger("crash"),
callerSkip: options.CallerSkip,
}
return crasher
}
// OverrideGlobalOptions overrides global crasher options.
func OverrideGlobalOptions(options CrasherOptions) {
if options.CrashMethod != nil {
crashGlobal.CrashMethod = options.CrashMethod
}
if options.Rand != nil {
crashGlobal.Rand = options.Rand
}
if options.CLIPrefix != "" {
crashGlobal.CLIPrefix = options.CLIPrefix
}
}
// RegisterCrashPoints registers crash points with the global Crasher instance.
func RegisterCrashPoints(crashPointIDs ...string) {
crashGlobal.RegisterCrashPoints(crashPointIDs...)
}
// RegisterCrashPoints registers crash points for a Crasher instance.
func (c *Crasher) RegisterCrashPoints(crashPointIDs ...string) {
for _, crashPointID := range crashPointIDs {
if _, loaded := c.CrashPointConfig.LoadOrStore(crashPointID, 0.0); loaded {
panic(fmt.Sprintf("crash: Crash point '%s' is already registered", crashPointID))
}
}
}
// ListRegisteredCrashPoints lists the registered crash points for the global
// Crasher instance.
func ListRegisteredCrashPoints() []string {
return crashGlobal.ListRegisteredCrashPoints()
}
// ListRegisteredCrashPoints lists the registered crash points for a Crasher instance.
func (c *Crasher) ListRegisteredCrashPoints() []string {
var crashPointIDs []string
c.CrashPointConfig.Range(func(k, v interface{}) bool {
crashPointIDs = append(crashPointIDs, k.(string))
return true
})
return crashPointIDs
}
// Here invokes the global crasher to crash at this point based on the passed in
// crashPointID's probability.
func Here(crashPointID string) {
crashGlobal.Here(crashPointID)
}
// Here crashes at this point based on the passed in crashPointID's probability.
func (c *Crasher) Here(crashPointID string) {
if !cmdFlags.DebugDontBlameOasis() && !testForceEnable {
return
}
_, callerFilename, callerLine, callerInformationIsCorrect := runtime.Caller(c.callerSkip)
cfg, ok := c.CrashPointConfig.Load(crashPointID)
if !ok {
c.logger.Error("Unknown crash point",
"crash_point_id", crashPointID,
"caller_information_is_correct", callerInformationIsCorrect,
"caller_filename", callerFilename,
"caller_line", callerLine,
)
panic(fmt.Errorf(`unknown crash point "%s"`, crashPointID))
}
crashPointProbability, ok := cfg.(float64)
if !ok {
panic(fmt.Errorf("invalid crash point config: %d", cfg))
}
// Do nothing if the probability of crashing is set to a value 0 or less.
if crashPointProbability <= 0 {
return
}
if c.Rand.Float64() <= crashPointProbability {
c.logger.Info("Crashing intentionally",
"crash_point_id", crashPointID,
"crash_point_probability", crashPointProbability,
"caller_information_is_correct", callerInformationIsCorrect,
"caller_filename", callerFilename,
"caller_line", callerLine,
)
c.CrashMethod()
}
}
// Config configure the global crash point values.
func Config(crashPointConfig map[string]float64) {
crashGlobal.Config(crashPointConfig)
}
// Config configures the crash point probabilities.
func (c *Crasher) Config(crashPointConfig map[string]float64) {
for crashPointID, crashProbability := range crashPointConfig {
if _, loaded := c.CrashPointConfig.Load(crashPointID); !loaded {
panic(fmt.Errorf(`attempted to configure unregistered crash point "%s"`, crashPointID))
}
c.CrashPointConfig.Store(crashPointID, crashProbability)
}
}
// InitFlags creates flags from the registered crash points and registers those flags with Viper.
func InitFlags() *flag.FlagSet {
return crashGlobal.InitFlags()
}
// InitFlags creates flags from the registered crash points and registers those flags with Viper.
func (c *Crasher) InitFlags() *flag.FlagSet {
flags := flag.NewFlagSet("", flag.ContinueOnError)
flags.Float64(CfgDefaultCrashPointProbability, 0.0, "Default crash point probability")
_ = flags.MarkHidden(CfgDefaultCrashPointProbability)
for _, crashPointID := range c.ListRegisteredCrashPoints() {
argFlag := fmt.Sprintf("%s.%s", c.CLIPrefix, crashPointID)
helpMessage := fmt.Sprintf(`Crash probability of "%s" crash point`, crashPointID)
flags.Float64(argFlag, 0.0, helpMessage)
_ = flags.MarkHidden(argFlag)
}
_ = viper.BindPFlags(flags)
return flags
}
// LoadViperArgValues loads viper arg values into the crash point config of the
// global crasher.
func LoadViperArgValues() {
crashGlobal.LoadViperArgValues()
}
// LoadViperArgValues loads viper arg values into the crash point config.
func (c *Crasher) LoadViperArgValues() {
defaultProb := viper.GetFloat64(CfgDefaultCrashPointProbability)
for _, crashPointID := range ListRegisteredCrashPoints() {
argFlag := fmt.Sprintf("%s.%s", c.CLIPrefix, crashPointID)
c.CrashPointConfig.Store(crashPointID, defaultProb)
if viper.IsSet(argFlag) {
c.CrashPointConfig.Store(crashPointID, viper.GetFloat64(argFlag))
}
}
}