/
cmd_list_instructions.go
104 lines (86 loc) · 1.89 KB
/
cmd_list_instructions.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
package debug
import (
"fmt"
"strconv"
"strings"
"github.com/cilium/ebpf"
)
var cmdListInstructions = Command{
Name: "list-instructions",
Aliases: []string{"li"},
Summary: "Lists the instructions of the program",
Exec: listInstructionExec,
}
func listInstructionExec(args []string) {
var program *ebpf.ProgramSpec
// If we have a running process, use its current program
if process != nil {
program = process.Program
} else {
programs := vm.GetPrograms()
if entrypoint < len(programs) {
program = programs[entrypoint]
}
}
if program == nil {
printRed("Invalid entrypoint or no programs loaded yet\n")
return
}
pc := 0
if process != nil {
pc = process.Registers.PC
}
const windowsize = 9
start := pc - windowsize
end := pc + windowsize
var err error
if len(args) >= 1 {
start, err = strconv.Atoi(args[0])
if err != nil {
printRed("invalid start: %s\n", err)
return
}
}
if len(args) >= 2 {
end, err = strconv.Atoi(args[1])
if err != nil {
printRed("invalid end: %s\n", err)
return
}
}
if start < 0 {
start = 0
}
if end > len(program.Instructions) {
end = len(program.Instructions)
}
if end <= start {
printRed("'end' must be bigger than 'start'\n")
return
}
var lastLine string
indexPadSize := len(strconv.Itoa(end))
for i := start; i < end; i++ {
inst := program.Instructions[i]
if inst.Symbol() != "" {
fmt.Print("<", yellow(inst.Symbol()), ">:\n")
}
var curLine string
if src := inst.Source(); src != nil && src.String() != "" {
curLine = src.String()
}
if curLine != lastLine && curLine != "" {
line := strings.TrimSpace(curLine)
fmt.Print(" ", strings.Repeat(" ", indexPadSize), "; ")
fmt.Println(green(line))
lastLine = curLine
}
if i == pc {
fmt.Print(yellow(" => "))
} else {
fmt.Print(" ")
}
fmt.Print(blue(fmt.Sprintf("%*d ", indexPadSize, i)))
fmt.Println(inst)
}
}