/
cmd.go
437 lines (365 loc) · 9.23 KB
/
cmd.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
package debug
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
prompt "github.com/c-bata/go-prompt"
"github.com/cilium/ebpf"
"github.com/dylandreimerink/mimic"
"github.com/lithammer/fuzzysearch/fuzzy"
"github.com/mgutz/ansi"
)
type CmdFn func(args []string)
type CompletionFn func(args []string) []prompt.Suggest
type CmdArg struct {
Name string
Required bool
}
type Command struct {
Name string
Summary string
Description string
Aliases []string
Exec CmdFn
Args []CmdArg
Subcommands []Command
CustomCompletion CompletionFn
}
var (
rootCommands []Command
red = ansi.ColorFunc("red")
blue = ansi.ColorFunc("blue")
green = ansi.ColorFunc("green")
yellow = ansi.ColorFunc("yellow")
blueStrike = ansi.ColorFunc("blue+s")
whiteStrike = ansi.ColorFunc("white+s")
)
func init() {
// TODO sort and group root commands by function, the way delve does it.
rootCommands = []Command{
helpCmd,
{
Name: "exit",
Aliases: []string{"q", "quit"},
Summary: "Exits the debugger",
Exec: func(args []string) {
os.Exit(0)
},
},
{
Name: "clear",
Summary: "Clear the screen",
Exec: func(args []string) {
fmt.Print("\033[2J")
},
},
cmdLoad,
cmdCtx,
cmdProgram,
cmdReset,
cmdRegisters,
cmdStepInstruction,
cmdListInstructions,
cmdStep,
cmdList,
cmdMap,
// cmdLocals,
cmdMemory,
cmdBreakpoint,
cmdContinue,
cmdContinueAll,
cmdMacro,
// TODO add `files` command to list all source files of all or a specific program
}
}
// fmt.Printf but in red
func printRed(format string, args ...interface{}) {
if len(args) == 0 {
fmt.Print(red(format))
return
}
fmt.Print(red(fmt.Sprintf(format, args...)))
}
func getCurBTFLine() string {
var (
spec *ebpf.ProgramSpec
inst int
)
if process != nil {
spec = process.Program
inst = process.Registers.PC
}
return getBTFLine(spec, inst)
}
func getBTFLine(spec *ebpf.ProgramSpec, instruction int) string {
if spec == nil {
return ""
}
// Walk up, until we found the nearest not empty line
var line string
for i := instruction; line == "" && i >= 0; i-- {
src := spec.Instructions[i].Source()
if src != nil {
line = src.String()
}
}
return line
}
func getCurBTFFilename() string {
var (
spec *ebpf.ProgramSpec
inst int
)
if process != nil {
spec = process.Program
inst = process.Registers.PC
}
return getBTFFilename(spec, inst)
}
func getBTFFilename(spec *ebpf.ProgramSpec, instruction int) string {
if spec == nil {
return ""
}
type Filenamer interface {
FileName() string
}
// Walk up, until we found the nearest not empty filename
var filename string
for i := instruction; filename == "" && i >= 0; i-- {
src := spec.Instructions[i].Source()
if lineInfo, ok := src.(Filenamer); ok {
filename = lineInfo.FileName()
}
}
return filename
}
func getCurBTFLineNumber() int {
var (
spec *ebpf.ProgramSpec
inst int
)
if process != nil {
spec = process.Program
inst = process.Registers.PC
}
return getBTFLineNumber(spec, inst)
}
func getBTFLineNumber(spec *ebpf.ProgramSpec, instruction int) int {
if spec == nil {
return 0
}
type LineNumberer interface {
LineNumber() uint32
}
var lineNumber int
for i := instruction; lineNumber == 0 && i >= 0; i-- {
src := spec.Instructions[i].Source()
if lineInfo, ok := src.(LineNumberer); ok {
lineNumber = int(lineInfo.LineNumber())
}
}
return lineNumber
}
func startProcess() error {
var ctx mimic.Context
if len(contexts) > curCtx {
ctx = contexts[curCtx]
}
var err error
process, err = vm.NewProcess(entrypoint, ctx)
if err != nil {
return err
}
// TODO make the current CPU ID configurable
err = process.SetCPUID(0)
if err != nil {
return err
}
return nil
}
var lastArgs []string
// TODO add a context mechanism, there are situations which can result in infinite loops which currently block
// user input. The only way to stop the program is externally. All commands should accept a context.Context and check
// it periodically and handle cancels, for example from a ctrl-c which is currently ignored until a command is done.
// This could also be combined with a timeout, commands are not exected to run for more than a few seconds. Which
// is a backup in case the user doesn't know about ctrl-c.
// (how would we handle explicitly long running commands? continue-all for example? runtime/file based config?)
func executor(in string) {
in = strings.TrimSpace(in)
// Split by space, but keep spaces within quotes("")
quoted := false
args := strings.FieldsFunc(in, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == ' '
})
for i, arg := range args {
args[i] = strings.Trim(arg, "\"")
}
// If the input string starts with a comment, don't actually execute
if strings.HasPrefix(in, "#") || strings.HasPrefix(in, "//") {
// But if we are recording a macro, add it to the list of commands
if macroState.rec {
macroState.recCommands = append(macroState.recCommands, in)
}
return
}
// Show help if no args were given and non have been executed before
if len(args) == 0 {
if len(lastArgs) == 0 {
helpCmd.Exec(nil)
return
}
// Repeat the last command, this is really helpful if you have to execute it a bunch of times
args = lastArgs
}
lastArgs = args
var cmd Command
cmdList := rootCommands
// Copy slice header, which we intend to modify
modArgs := args
for {
var found bool
cmd, found = commandMap(cmdList)[modArgs[0]]
if !found {
printRed("'%s' is not a valid command\n\n", strings.Join(args, " "))
fmt.Println("Usage:")
helpExec(args)
return
}
modArgs = modArgs[1:]
// If this command has sub commands and we also have more arguments, continue resolving
if len(cmd.Subcommands) > 0 && len(modArgs) > 0 {
cmdList = cmd.Subcommands
continue
}
// If a command has no Exec, we are not meant to execute it, rater a subcommand, so show help
if cmd.Exec == nil {
printRed("'%s' is missing a {sub-command}\n\n", strings.Join(args, " "))
fmt.Println("Usage:")
helpExec(args)
return
}
// If there are no more arguments or no more sub commands, execute the current command and exit
cmd.Exec(modArgs)
// TODO return an indication from Exec to save as macro or not. So commands which are invalid are not recorded.
// If macro recording is enabled, record the full command.
if macroState.rec {
// FIXME this is a temporary hack, as soon as commands can tell us to ignore a command for recording
// we should use that instread and make `macro start` ignore its own addition to the macro
if !(len(args) >= 2 && args[0] == "macro" && args[1] == "start") {
// Don't record the `macro start` command in the actual macro
macroState.recCommands = append(macroState.recCommands, in)
}
}
return
}
}
func completer(in prompt.Document) []prompt.Suggest {
inText := strings.TrimSpace(in.Text)
if inText == "" {
return nil
}
quoted := false
args := strings.FieldsFunc(inText, func(r rune) bool {
if r == '"' {
quoted = !quoted
}
return !quoted && r == ' '
})
for i, arg := range args {
args[i] = strings.Trim(arg, "\"")
}
var cmd Command
cmdList := rootCommands
// Copy slice header, which we intend to modify
modArgs := args
for {
if len(modArgs) == 0 {
break
}
var found bool
cmd, found = commandMap(cmdList)[modArgs[0]]
if !found {
break
}
// If this command has sub commands and we also have more arguments, continue resolving
if len(cmd.Subcommands) > 0 && len(modArgs) > 0 {
modArgs = modArgs[1:]
cmdList = cmd.Subcommands
continue
}
// If a command has no Exec, we are not meant to execute it, rater a subcommand, so show help
if cmd.CustomCompletion != nil {
return cmd.CustomCompletion(modArgs[1:])
}
break
}
cmds := make([]string, 0, len(cmdList))
for _, cmd := range cmdList {
cmds = append(cmds, cmd.Name)
cmds = append(cmds, cmd.Aliases...)
}
search := ""
if len(modArgs) > 0 {
search = modArgs[0]
}
var suggestions []prompt.Suggest
cmdMap := commandMap(cmdList)
ranks := fuzzy.RankFind(search, cmds)
sort.Sort(ranks)
for _, rank := range ranks {
cmd, found := cmdMap[rank.Target]
if !found {
continue
}
suggestions = append(suggestions, prompt.Suggest{
Text: rank.Target,
Description: cmd.Summary,
})
}
return suggestions
}
func fileCompletion(args []string) []prompt.Suggest {
path := "."
if len(args) > 0 {
path = args[0]
}
pathDir := path
// If it is a directory, show the contents of the directory
if stat, err := os.Stat(path); err != nil || stat.IsDir() {
pathDir = filepath.Dir(path)
}
// pathDir := filepath.Dir(path)
dir, err := os.ReadDir(pathDir)
if err != nil {
return nil
}
fileNames := make([]string, len(dir))
for i, file := range dir {
if file.IsDir() {
fileNames[i] = file.Name() + "/"
} else {
fileNames[i] = file.Name()
}
}
pathDir, file := filepath.Split(path)
ranks := fuzzy.RankFind(file, fileNames)
sort.Sort(ranks)
var suggestion []prompt.Suggest
for _, rank := range ranks {
var text string
if pathDir == "" {
text = rank.Target
} else {
text = fmt.Sprintf("%s%s", pathDir, rank.Target)
}
suggestion = append(suggestion, prompt.Suggest{
Text: text,
})
}
return suggestion
}