-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.go
534 lines (433 loc) · 12.4 KB
/
debugger.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package gestalt
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
"sync/atomic"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/fatih/color"
)
type commandResult string
const (
retryResult commandResult = "retry"
continueResult commandResult = "continue"
quitResult commandResult = "quit"
)
var (
errQuit = errors.New("debugger quit command issued")
)
type debugHandler struct {
// stop before execution.
breakpoints []string
// stop after execution if failed.
failpoints []string
in io.Reader
out io.Writer
interrupt uint32
quitting bool
}
func newDebugHandler(in io.Reader, out io.Writer) *debugHandler {
return &debugHandler{in: in, out: out}
}
func (h *debugHandler) Interrupt() uint32 {
return atomic.AddUint32(&h.interrupt, 1)
}
func (h *debugHandler) shouldInterrupt() bool {
return atomic.SwapUint32(&h.interrupt, 0) > 0
}
func (h *debugHandler) AddBreakpoint(expr string) error {
h.breakpoints = append(h.breakpoints, expr)
return nil
}
func (h *debugHandler) AddFailpoint(expr string) error {
h.failpoints = append(h.failpoints, expr)
return nil
}
type debuggerState struct {
err error
}
func (h *debugHandler) Eval(e Evaluator, node Component) error {
state := &debuggerState{}
if h.shouldBreak(e.Path(), h.breakpoints, "break") || h.shouldInterrupt() {
if h.runBreakConsole(e, node, state) == quitResult {
return state.err
}
}
for {
state.err = node.Eval(e)
interrupt := h.shouldInterrupt()
if !interrupt && state.err == nil {
break
}
if !interrupt && !h.shouldBreak(e.Path(), h.failpoints, "fail") {
break
}
if h.runFailureConsole(e, node, state) != retryResult {
break
}
}
return state.err
}
func (h *debugHandler) runBreakConsole(e Evaluator, node Component, state *debuggerState) commandResult {
return h.runDebugger(e, node, h.makeBreakApp, state)
}
func (h *debugHandler) runFailureConsole(e Evaluator, node Component, state *debuggerState) commandResult {
return h.runDebugger(e, node, h.makeFailureApp, state)
}
func (h *debugHandler) printDBGHeader(e Evaluator, state *debuggerState) {
errors := h.curErrors(e, state)
errc := len(errors)
clr := color.New()
if errc > 0 {
clr.Add(color.FgRed)
} else {
clr.Add(color.FgCyan)
}
clr.Fprintf(h.out, "\n[%v: %v errors]\n", e.Path(), errc)
}
func (h *debugHandler) fprintErr(fmt string, args ...interface{}) {
fprintErr(h.out, fmt, args...)
}
func (h *debugHandler) runDebugger(
e Evaluator,
node Component,
appBuilder func() *debugApp,
state *debuggerState) commandResult {
h.quitting = false
for i := 0; ; i++ {
app := appBuilder()
h.printDBGHeader(e, state)
cmd, err := h.readCommand(app.app)
if err == io.EOF {
return continueResult
}
check := func(clause *kingpin.CmdClause, cmd string) bool {
return clause != nil && clause.FullCommand() == cmd
}
switch {
// control flow
case check(app.cmdContinue, cmd):
fmt.Fprintf(h.out, "continuing...\n")
return continueResult
case check(app.cmdRetry, cmd):
fmt.Fprintf(h.out, "retrying...\n")
return retryResult
case check(app.cmdQuit, cmd):
fmt.Fprintf(h.out, "quitting...\n")
h.quitting = true
state.err = errQuit
return quitResult
// errors
case check(app.cmdErrorsList, cmd):
h.showErrors(e, node, state)
case check(app.cmdErrorsDel, cmd):
h.clearErrors(e, node, state)
// vars
case check(app.cmdVarsList, cmd):
h.showVars(e, node)
case check(app.cmdVarsAdd, cmd):
h.addVars(e, node, *app.cmdVarsAddEntries)
h.showVars(e, node)
case check(app.cmdVarsDel, cmd):
h.delVars(e, node, *app.cmdVarsDelEntries)
h.showVars(e, node)
// breakpoints
case check(app.cmdBPList, cmd):
h.showPoints(h.breakpoints)
case check(app.cmdBPAdd, cmd):
h.breakpoints = append(h.breakpoints, *app.cmdBPAddEntries...)
h.showPoints(h.breakpoints)
case check(app.cmdBPDel, cmd):
h.breakpoints = h.delPoints(h.breakpoints, *app.cmdBPDelEntries)
h.showPoints(h.breakpoints)
// failpoints
case check(app.cmdFPList, cmd):
h.showPoints(h.failpoints)
case check(app.cmdFPAdd, cmd):
h.failpoints = append(h.failpoints, *app.cmdFPAddEntries...)
h.showPoints(h.failpoints)
case check(app.cmdFPDel, cmd):
h.failpoints = h.delPoints(h.failpoints, *app.cmdFPDelEntries)
h.showPoints(h.failpoints)
case check(app.cmdList, cmd):
h.listComponents(e, node)
}
}
return continueResult
}
func (h *debugHandler) shouldBreak(path string, points []string, prefix string) bool {
if h.quitting {
return false
}
if idx := h.matchPath(path, points); idx >= 0 {
fmt := "\n%vpoint %v at %v\n"
color.New(color.FgYellow).Fprintf(h.out, fmt, prefix, idx, path)
return true
}
return false
}
func (h *debugHandler) matchPath(path string, points []string) int {
for idx, point := range points {
if strings.HasSuffix(path, point) {
return idx
}
}
return -1
}
func (h *debugHandler) readCommand(app *kingpin.Application) (string, error) {
color.New(color.FgWhite, color.Bold).Fprintf(h.out, "\n> ")
buf := bufio.NewReader(h.in)
line, err := buf.ReadBytes('\n')
fmt.Fprint(h.out, "\n")
if err != nil {
return "", err
}
line = bytes.TrimRight(line, "\n")
if len(line) == 0 {
return "", nil
}
args := strings.Fields(string(line))
cmd, err := app.Parse(args)
if err != nil {
h.fprintErr("invalid command: '%v'\n\n", strings.Join(args, " "))
app.Usage([]string{})
}
return cmd, err
}
type debugApp struct {
app *kingpin.Application
cmdContinue *kingpin.CmdClause
cmdRetry *kingpin.CmdClause
cmdQuit *kingpin.CmdClause
// errors
cmdErrors *kingpin.CmdClause
cmdErrorsList *kingpin.CmdClause
cmdErrorsDel *kingpin.CmdClause
// vars
cmdVars *kingpin.CmdClause
cmdVarsList *kingpin.CmdClause
cmdVarsAdd *kingpin.CmdClause
cmdVarsAddEntries *[]string
cmdVarsDel *kingpin.CmdClause
cmdVarsDelEntries *[]string
// breakpoint
cmdBP *kingpin.CmdClause
cmdBPList *kingpin.CmdClause
cmdBPAdd *kingpin.CmdClause
cmdBPAddEntries *[]string
cmdBPDel *kingpin.CmdClause
cmdBPDelEntries *[]uint
// failpoint
cmdFP *kingpin.CmdClause
cmdFPList *kingpin.CmdClause
cmdFPAdd *kingpin.CmdClause
cmdFPAddEntries *[]string
cmdFPDel *kingpin.CmdClause
cmdFPDelEntries *[]uint
cmdList *kingpin.CmdClause
}
func (h *debugHandler) makeBreakApp() *debugApp {
kapp := h.makeBaseApp()
app := &debugApp{app: kapp}
app.cmdContinue = kapp.
Command("continue", "continue execution").Alias("c")
app.cmdQuit = kapp.
Command("quit", "quit advancing; unwind execution").Alias("q")
app.cmdErrors = kapp.
Command("errors", "manage errors").Alias("e")
app.cmdErrorsList = app.cmdErrors.
Command("show", "show all errors").Default()
app.cmdErrorsDel = app.cmdErrors.
Command("clear", "clear all errors")
app.cmdVars = kapp.
Command("vars", "manage variables").Alias("v")
app.cmdVarsList = app.cmdVars.
Command("list", "list all vars").Default()
app.cmdVarsAdd = app.cmdVars.
Command("set", "set variable value(s)")
app.cmdVarsAddEntries = app.cmdVarsAdd.Arg("name=val", "name and value of variable to set").Strings()
app.cmdVarsDel = app.cmdVars.
Command("unset", "delete variable(s)")
app.cmdVarsDelEntries = app.cmdVarsDel.Arg("name", "name of variable to unset").Strings()
// breakpoint commands
app.cmdBP = kapp.
Command("breakpoint", "manipulate breakpoints").Alias("b")
app.cmdBPList = app.cmdBP.
Command("list", "show current breakpoints").Alias("l").Default()
app.cmdBPAdd = app.cmdBP.
Command("add", "add breakpoints").Alias("a")
app.cmdBPAddEntries = app.cmdBPAdd.
Arg("pattern", "breakpoint pattern to add").
Strings()
app.cmdBPDel = app.cmdBP.
Command("del", "delete breakpoints").Alias("d")
app.cmdBPDelEntries = app.cmdBPDel.
Arg("index", "breakpoint numbers to delete").
Uints()
// failpoint commands
app.cmdFP = kapp.
Command("failpoint", "manipulate failpoints").Alias("f")
app.cmdFPList = app.cmdFP.
Command("list", "show current failpoints").Alias("l").Default()
app.cmdFPAdd = app.cmdFP.
Command("add", "add failpoint").Alias("a")
app.cmdFPAddEntries = app.cmdFPAdd.
Arg("pattern", "failpoint pattern to add").
Strings()
app.cmdFPDel = app.cmdFP.
Command("del", "delete failpoints").Alias("d")
app.cmdFPDelEntries = app.cmdFPDel.
Arg("index", "failpoint numbers to delete").
Uints()
app.cmdList = kapp.Command("list", "list components").Alias("l")
return app
}
func (h *debugHandler) makeFailureApp() *debugApp {
app := h.makeBreakApp()
app.cmdRetry = app.app.
Command("retry", "retry component").Alias("r")
return app
}
func (h *debugHandler) makeBaseApp() *kingpin.Application {
app := kingpin.New("debugger", "gestalt debugger").
Terminate(func(n int) {}).
UsageTemplate(usageTemplate).
Writer(h.out)
app.HelpFlag = app.HelpFlag.Hidden()
return app
}
func (h *debugHandler) curErrors(e Evaluator, state *debuggerState) []error {
errors := e.Errors()
if state.err != nil {
errors = append(errors, state.err)
}
return errors
}
func (h *debugHandler) showErrors(e Evaluator, _ Component, state *debuggerState) {
errors := h.curErrors(e, state)
fmt.Fprintf(h.out, "%v errors\n", len(errors))
for _, err := range errors {
fmt.Fprintf(h.out, "[%v]\n", e.Path())
fmt.Fprintf(h.out, "%v\n", err)
if errd, ok := err.(ErrorWithDetail); ok {
fmt.Fprintf(h.out, "%v\n", errd.Detail())
}
}
}
func (h *debugHandler) clearErrors(e Evaluator, _ Component, state *debuggerState) {
e.ClearError()
state.err = nil
}
func (h *debugHandler) showVars(e Evaluator, _ Component) {
vars := e.Vars()
for _, k := range vars.Keys() {
fmt.Fprintf(h.out, "%v=%v\n", k, vars.Get(k))
}
}
func (h *debugHandler) addVars(e Evaluator, _ Component, entries []string) {
for _, entry := range entries {
pieces := strings.SplitN(entry, "=", 2)
if len(pieces) != 2 {
continue
}
e.Vars().Put(pieces[0], pieces[1])
}
}
func (h *debugHandler) delVars(e Evaluator, _ Component, entries []string) {
for _, entry := range entries {
e.Vars().Unset(entry)
}
}
func (h *debugHandler) showPoints(points []string) {
for i, point := range points {
fmt.Fprintf(h.out, "[%v]\t%v\n", i, point)
}
}
func (h *debugHandler) delPoints(current []string, indexes []uint) []string {
points := make([]string, 0)
for i, point := range current {
keep := true
for _, j := range indexes {
if j == uint(i) {
keep = false
break
}
}
if keep {
points = append(points, point)
}
}
return points
}
func (h *debugHandler) listComponents(e Evaluator, node Component) {
curpath := e.Path()
TraversePaths(e.Root(), func(path string) {
highlight := false
fmt.Fprintf(h.out, " ")
if path == curpath {
highlight = true
color.New(color.FgGreen).Fprintf(h.out, "*")
} else {
fmt.Fprintf(h.out, " ")
}
if idx := h.matchPath(path, h.breakpoints); idx >= 0 {
highlight = true
color.New(color.FgYellow).Fprintf(h.out, "*")
} else {
fmt.Fprintf(h.out, " ")
}
if idx := h.matchPath(path, h.failpoints); idx >= 0 {
highlight = true
color.New(color.FgRed).Fprintf(h.out, "*")
} else {
fmt.Fprintf(h.out, " ")
}
fmt.Fprintf(h.out, " ")
if highlight {
color.New(color.FgWhite).Fprintf(h.out, "%v\n", path)
} else {
fmt.Fprintf(h.out, "%v\n", path)
}
})
}
var usageTemplate = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}} {{if .Aliases}}(alias: {{index .Aliases 0}}){{end}}\
{{end}}\
{{define "FormatCommandList"}}\
{{range .}}\
{{if not .Hidden}}\
{{.Depth|Indent}}{{.Name}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{end}}\
{{template "FormatCommandList" .Commands}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
{{if .Context.SelectedCommand}}\
usage: {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}}
{{if .Context.Flags}}\
Flags:
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand.Commands}}\
Commands:
{{.Context.SelectedCommand}}
{{template "FormatCommandList" .Context.SelectedCommand.Commands}}
{{end}}\
{{else if .App.Commands}}\
Commands:
{{template "FormatCommandList" .App.Commands}}
{{end}}\
`