-
Notifications
You must be signed in to change notification settings - Fork 5
/
debugger.go
552 lines (467 loc) · 12.3 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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package main
import (
"fmt"
"bufio"
"os"
"strings"
"strconv"
"errors"
"sort"
"github.com/gboncoffee/egg/machine"
"github.com/gboncoffee/egg/assembler"
)
func debuggerHelp() {
help := `Commands:
help
Shows this help.
print <expr>[@<length>]
Prints the content of registers and memory.
next
Executes the next instruction, then pauses.
continue
Continue execution until a BREAK call or breakpoint.
break [expr]
With an argument, creates a new breakpoint. With no argument, shows all
breakpoints. Accepts numbers and Assembly labels.
remove <expr>
Removes a breakpoint. Accepts numbers and Assembly labels.
dump <address>@<length> <filename>
Dumps the content of memory to a file.
exit
Terminate debugging session.
quit
Alias to exit.
All commands have a shortcut of it's first letter: h, p, n, etc.
The print command generally follows this rules:
- If the expression is only a register (e.g., x1, t1, zero, ra, etc), it prints
it's contents;
- If the expression is a register with a length (e.g., t1@1, ra@7, etc), it
prints the content of the memory addressed by the content of the register.
The dump command also accepts registers, but always dereference them.
Both print and dump commands accepts the special expression #, which means the
program itself. For example, you may dump the assembled program to a file by
running 'dump # file'.
In the print command, <addr>#<length> means "length instructions after addr".
#<length> is a shortcut to use with the current instruction address.
`
fmt.Print(help)
}
func tokensToString(sym []assembler.DebuggerToken) []string {
str := make([]string, len(sym))
for i, tok := range sym {
var build strings.Builder
build.WriteString(fmt.Sprintf("0x%016x: ", tok.Address))
if tok.Label != "" {
build.WriteString(tok.Label)
build.WriteString(": ")
} else {
build.WriteRune('\t')
}
build.WriteString(tok.Instruction)
build.WriteRune(' ')
build.WriteString(tok.Args)
str[i] = build.String()
}
return str
}
func getPrintHashExpr(m machine.Machine, sym []assembler.DebuggerToken, expr string) ([]string, error) {
fn, sn, _ := strings.Cut(expr, "#")
fn = strings.TrimSpace(fn)
sn = strings.TrimSpace(sn)
var faddr uint64
var l uint64
if fn == "" {
var err error
if sn == "" {
return tokensToString(sym), nil
}
l, err = strconv.ParseUint(sn, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot parse %v as number: %v", sn, err))
}
faddr = m.GetCurrentInstructionAddress()
} else {
var err error
if sn == "" {
return nil, errors.New("Length not supplied")
}
faddr, err = strconv.ParseUint(fn, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot parse %v as address.", fn))
}
l, err = strconv.ParseUint(sn, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v a number.", sn))
}
}
i := uint64(sort.Search(len(sym), func(i int) bool {
return sym[i].Address >= faddr
}))
if i == uint64(len(sym)) {
return nil, errors.New(fmt.Sprintf("No instruction at address 0x%x", faddr))
}
l = l + i
if l >= uint64(len(sym)) {
l = uint64(len(sym))
}
return tokensToString(sym[i:l]), nil
}
func getMemoryContentPrint(m machine.Machine, addr string, length string) ([]uint64, error) {
var a uint64
reg, err := m.GetRegisterNumber(addr)
if err != nil {
a, err = strconv.ParseUint(addr, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a register or address", addr))
}
} else {
a, _ = m.GetRegister(reg)
}
l, err := strconv.ParseUint(length, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a number.", length))
}
mem, err := m.GetMemoryChunk(a, l)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot get memory content: %v", err))
}
// I hope this is somehow "optimized out" to a simple padded copy.
m64 := make([]uint64, l)
for i, v := range mem {
m64[i] = uint64(v)
}
return m64, nil
}
func getPrintExpr(m machine.Machine, expr string) ([]string, error) {
addr, length, has_at := strings.Cut(expr, "@")
addr = strings.TrimSpace(addr)
length = strings.TrimSpace(length)
if !has_at {
reg, err := m.GetRegisterNumber(addr)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot get register content: %v", err))
}
c, _ := m.GetRegister(reg)
return []string{fmt.Sprintf("0x%016x", c)}, nil
}
arr, err := getMemoryContentPrint(m, addr, length)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot get memory content: %v", err))
}
c := make([]string, len(arr))
for i, v := range arr {
c[i] = fmt.Sprintf("0x%02x", v)
}
return c, nil
}
func debuggerPrint(m machine.Machine, sym []assembler.DebuggerToken, args []string) {
if len(args) < 1 {
fmt.Println("print expects one argument: <expr>[@<length>] or [<addr>]#[<length>]")
return
}
expr := args[0]
var res []string
var err error
if strings.IndexRune(expr, '#') >= 0 {
res, err = getPrintHashExpr(m, sym, expr)
if err != nil {
fmt.Printf("%v\n", err)
} else {
for _, v := range res {
fmt.Println(v)
}
}
} else {
res, err = getPrintExpr(m, expr)
if err != nil {
fmt.Printf("%v\n", err)
} else {
for i := 0; i < len(res)-1; i++ {
if i % 8 == 7 {
fmt.Printf("%s\n", res[i])
} else {
fmt.Printf("%s ", res[i])
}
}
fmt.Printf("%s\n", res[len(res)-1])
}
}
}
func ioCall(m machine.Machine, call *machine.Call, in *bufio.Reader) {
if call.Number == machine.SYS_READ {
addr := call.Arg1
size := call.Arg2
fmt.Printf("READ call for address 0x%x with %d bytes:\n", addr, size)
buf := make([]byte, size)
_, err := in.Read(buf)
if err != nil {
fmt.Printf("Error reading stdin: %v\n", err)
return
}
m.SetMemoryChunk(addr, []uint8(buf))
} else {
addr := call.Arg1
size := call.Arg2
buf, _ := m.GetMemoryChunk(addr, size)
fmt.Print(string(buf))
}
}
func debuggerNext(m machine.Machine, sym []assembler.DebuggerToken, in *bufio.Reader) {
call, err := m.NextInstruction()
if err != nil {
fmt.Println(fmt.Sprintf("Instruction execution failed: %v", err))
return
}
pc := m.GetCurrentInstructionAddress()
if call != nil {
if call.Number == machine.SYS_BREAK {
fmt.Printf("BREAK call while stepping at address 0x%x\n", pc)
} else {
ioCall(m, call, in)
}
}
debuggerPrint(m, sym, []string{"#3"})
}
func debuggerContinue(m machine.Machine, sym []assembler.DebuggerToken, breakpoints []uint64, in *bufio.Reader) {
for {
call, err := m.NextInstruction()
if err != nil {
fmt.Println(fmt.Sprintf("Instruction execution failed: %v", err))
return
}
pc := m.GetCurrentInstructionAddress()
if call != nil {
if call.Number == machine.SYS_BREAK {
fmt.Printf("Stopped at BREAK call at address 0x%x\n", pc)
debuggerPrint(m, sym, []string{"#3"})
return
} else {
ioCall(m, call, in)
}
}
_, brk := sort.Find(len(breakpoints), func(i int) int {
// This one MUST be optimized.
if pc >= breakpoints[i] {
if breakpoints[i] != pc {
return 1
} else {
return 0
}
} else {
return -1
}
})
if brk {
fmt.Printf("Stopped at breakpoint at address 0x%x\n", pc)
debuggerPrint(m, sym, []string{"#3"})
return
}
}
}
func printBreakpoints(breakpoints []uint64) {
fmt.Println("Breakpoints:")
for _, b := range breakpoints {
fmt.Printf("0x%016x\n", b)
}
}
func debuggerBreakpoint(m machine.Machine, sym []assembler.DebuggerToken, breakpoints *[]uint64, args []string) {
if len(args) < 1 {
printBreakpoints(*breakpoints)
return
}
var addr uint64
var found bool
for _, s := range sym {
if s.Label == args[0] {
found = true
addr = s.Address
break
}
}
if !found {
var err error
addr, err = strconv.ParseUint(args[0], 0, 64)
if err != nil {
fmt.Printf("%v is not a number.\n", args[0])
return
}
}
n_brks := make([]uint64, len(*breakpoints)+1)
var brk_idx int
for i, p := range *breakpoints {
if p < addr {
n_brks[i] = p
brk_idx++
} else if p == addr {
fmt.Println("Breakpoint already exists")
return
} else {
break
}
}
n_brks[brk_idx] = addr
if brk_idx < len(*breakpoints) {
for i, p := range (*breakpoints)[brk_idx:] {
n_brks[i+brk_idx+1] = p
}
}
*breakpoints = n_brks
fmt.Printf("New breakpoint at address 0x%x\n", addr)
printBreakpoints(*breakpoints)
}
func debuggerRemove(m machine.Machine, sym []assembler.DebuggerToken, breakpoints *[]uint64, args []string) {
if len(args) < 1 {
fmt.Println("remove expects a breakpoint to remove: remove <address>")
return
}
var addr uint64
addr, err := strconv.ParseUint(args[0], 0, 64)
if err != nil {
for _, t := range sym {
if t.Label == args[0] {
addr = t.Address
goto FIND
}
}
fmt.Printf("Cannot parse %v as address.\n", args[0])
return
}
FIND:
var idx int
for i, b := range *breakpoints {
if b == addr {
idx = i
goto FOUND
}
}
fmt.Printf("No breakpoint at address 0x%x\n", addr)
return
FOUND:
n_brks := make([]uint64, len(*breakpoints)-1)
for i := 0; i < idx; i++ {
n_brks[i] = (*breakpoints)[i]
}
for i := idx; i < len(n_brks); i++ {
n_brks[i] = (*breakpoints)[i+1]
}
*breakpoints = n_brks
printBreakpoints(*breakpoints)
}
func getDumpExpr(m machine.Machine, expr string, prog []uint8) ([]uint8, error) {
if strings.IndexRune(expr, '#') >= 0 {
if expr == "#" {
return prog, nil
}
offset, length, _ := strings.Cut(expr, "#")
offset = strings.TrimSpace(offset)
length = strings.TrimSpace(length)
of, err := strconv.ParseUint(offset, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a number.", offset))
}
l, err := strconv.ParseUint(length, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a number.", length))
}
end := of + l
if end > uint64(len(prog)) {
end = uint64(len(prog))
}
return prog[of:end], nil
} else {
addr, length, has_at := strings.Cut(expr, "@")
if !has_at {
return nil, errors.New(fmt.Sprintf("Cannot parse %v as a dump argument", expr))
}
addr = strings.TrimSpace(addr)
length = strings.TrimSpace(length)
ad, err := strconv.ParseUint(addr, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a number.", addr))
}
l, err := strconv.ParseUint(length, 0, 64)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v is not a number.", length))
}
mem, err := m.GetMemoryChunk(ad, l)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error getting memory chunk: %v", err))
}
return mem, nil
}
}
func debuggerDump(m machine.Machine, args []string, prog []uint8) {
if len(args) < 2 {
fmt.Println("dump expects two arguments: (<expr>@<length> or [<addr>]#[<length>]) <file>")
return
}
expr := strings.TrimSpace(args[0])
file := strings.TrimSpace(args[1])
dump, err := getDumpExpr(m, expr, prog)
if err != nil {
fmt.Printf("Cannot get content to dump: %v\n", err)
return
}
f, err := os.OpenFile(file, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0644)
if err != nil {
fmt.Printf("Cannot open %s for write: %v\n", file, err)
return
}
_, err = f.Write(dump)
if err != nil {
fmt.Printf("Error while writing to %s: %v\n", file, err)
}
f.Close()
}
func debugMachine(m machine.Machine, sym []assembler.DebuggerToken, prog []uint8) {
version()
fmt.Println("Type 'help' for a list of commands.")
fmt.Println("Debugging", m.ArchitetureName())
var breakpoints []uint64
in := bufio.NewReader(os.Stdin)
fmt.Print("egg> ")
line, err := in.ReadString('\n')
for err == nil {
wsldirty := strings.Split(strings.TrimRight(line, "\n"), " ")
if len(wsldirty) == 0 {
continue
}
wsl := []string{}
for i := range wsldirty {
if len(wsldirty[i]) != 0 {
wsl = append(wsl, wsldirty[i])
}
}
if len(wsl) > 0 {
switch wsl[0] {
case "help", "h":
debuggerHelp()
case "print", "p":
debuggerPrint(m, sym, wsl[1:])
case "next", "n":
debuggerNext(m, sym, in)
case "continue", "c":
debuggerContinue(m, sym, breakpoints, in)
case "break", "b":
debuggerBreakpoint(m, sym, &breakpoints, wsl[1:])
case "remove", "r":
debuggerRemove(m, sym, &breakpoints, wsl[1:])
case "dump", "d":
debuggerDump(m, wsl[1:], prog)
case "exit", "e", "quit", "q":
goto EXIT
case "ping":
fmt.Println("pong!")
default:
fmt.Printf("no such command: %v\n", wsl[0])
}
} else {
fmt.Println("")
}
fmt.Print("egg> ")
line, err = in.ReadString('\n')
}
fmt.Println("")
EXIT:
fmt.Println("bye!")
}