This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
repl.go
192 lines (182 loc) · 5.02 KB
/
repl.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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
const (
GreenColor = "\u001b[32m"
GrayColor = "\u001b[38;5;245m"
ResetColor = "\u001b[0m"
)
type Command struct {
Name string
Args []string
}
func parseCmd(userInput string) (*Command, error) {
data := strings.Split(userInput, " ")
if len(data) == 0 {
return nil, errors.New("unknown command")
}
name := data[0]
var args []string
if len(data) > 1 {
args = append(args, data[1:]...)
}
return &Command{Name: name, Args: args}, nil
}
func repl(userInput string) bool {
cmd, err := parseCmd(userInput)
if err != nil {
fmt.Println(err.Error())
return true
}
switch cmd.Name {
case "setBreakpoint", "sb":
if len(cmd.Args) < 2 {
fmt.Println("sb filename linenumber")
return true
}
if line, err := strconv.Atoi(cmd.Args[1]); err != nil {
fmt.Printf("Cannot convert %s to line number\n", cmd.Args[1])
} else {
err := dbg.SetBreakpoint(cmd.Args[0], line)
if err != nil {
fmt.Println(err.Error())
}
}
case "clearBreakpoint", "cb":
if len(cmd.Args) < 2 {
fmt.Println("cb filename linenumber")
return true
}
if line, err := strconv.Atoi(cmd.Args[1]); err != nil {
fmt.Printf("Cannot convert %s to line number\n", cmd.Args[1])
} else {
err := dbg.ClearBreakpoint(cmd.Args[0], line)
if err != nil {
fmt.Println(err.Error())
}
}
case "breakpoints", "b":
breakpoints, err := dbg.Breakpoints()
if err != nil {
fmt.Println(err.Error())
} else {
for filename := range breakpoints {
fmt.Printf("Breakpoint on %s:%v\n", filename, breakpoints[filename])
}
}
// case "run", "r":
// // Works like continue, only if the activation reason is program start ("s")
// if dbg.PC() == 0 {
// return false
// } else {
// fmt.Println("Error: only works if program is not started")
// }
case "next", "n":
err = dbg.Next()
if err != nil {
// fmt.Println(err.Error())
return false
}
case "cont", "continue", "c":
return false
case "step", "s":
err = dbg.StepIn()
if err != nil {
return false
}
case "exec", "e":
val, err := dbg.Exec(strings.Join(cmd.Args, " "))
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
break
}
fmt.Printf("< %s\n", val)
case "print", "p":
val, err := dbg.Print(strings.Join(cmd.Args, ""))
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
break
}
fmt.Printf("< %s\n", val)
case "list", "l":
lines, err := dbg.List()
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
break
}
currentLine := dbg.Line()
lineIndex := currentLine - 1
var builder strings.Builder
for idx, lineContents := range lines {
if inRange(lineIndex, idx-4, idx+4) {
lineNumber := idx + 1
totalPadding := 6
digitCount := countDigits(lineNumber)
if digitCount >= totalPadding {
totalPadding = digitCount + 1
}
if currentLine == lineNumber {
padding := strings.Repeat(" ", totalPadding-digitCount)
builder.Write([]byte(fmt.Sprintf("%s>%s %d%s%s\n", GreenColor, ResetColor, currentLine, padding, lines[lineIndex])))
} else {
padding := strings.Repeat(" ", totalPadding-digitCount)
builder.Write([]byte(fmt.Sprintf("%s %d%s%s%s\n", GrayColor, lineNumber, padding, lineContents, ResetColor)))
}
}
}
fmt.Println(builder.String())
case "backtrace", "bt":
stack := runtime.CaptureCallStack(0, nil)
var backtrace bytes.Buffer
backtrace.WriteRune('\n')
for _, frame := range stack {
frame.Write(&backtrace)
backtrace.WriteRune('\n')
}
fmt.Println(backtrace.String())
case "help", "h":
fmt.Println(help)
case "quit", "q":
os.Exit(0)
default:
fmt.Printf("Unknown command, `%s`. You can use `h` to print available commands\n", userInput)
}
return true
}
func inRange(i, min, max int) bool {
if (i >= min) && (i <= max) {
return true
} else {
return false
}
}
func countDigits(number int) int {
if number < 10 {
return 1
} else {
return 1 + countDigits(number/10)
}
}
var help = `
setBreakpoint, sb Set a breakpoint on a given file and line
clearBreakpoint, cb Clear a breakpoint on a given file and line
breakpoints, b List all known breakpoints
run, r Run program until a breakpoint/debugger statement if program is not started
(ProgramStartActivation is disabled, so run doesn't work for now)
next, n Continue to next line in current file
cont, c Resume execution until next debugger line
step, s Step into, potentially entering a function
out, o Step out, leaving the current function (not implemented yet)
exec, e Evaluate the expression and print the value
list, l Print the source around the current line where execution is currently paused
print, p Print the provided variable's value
backtrace, bt Print the current backtrace
help, h Print this very help message
quit, q Exit debugger and quit (Ctrl+C)
`[1:] // this removes the first new line