This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
202 lines (164 loc) · 3.91 KB
/
command.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
// Package command implements functions for responding to user
// input and dispatching to appropriate backend commands.
package command
import (
"bufio"
"debug/gosym"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/hitzhangjie/golang-debugger-dev/proctl"
)
// debugger commands associated hanlders
type cmdfunc func(proc *proctl.DebuggedProcess, args ...string) error
type Commands struct {
cmds map[string]cmdfunc
}
// Returns a Commands struct with default commands defined.
func DebugCommands() *Commands {
cmds := map[string]cmdfunc{
"break": breakpoint,
"clear": clear,
"step": step,
"continue": cont,
"next": next,
"": nullCommand,
}
return &Commands{cmds}
}
// Register custom commands. Expects cf to be a func of type cmdfunc,
// returning only an error.
func (c *Commands) Register(cmdstr string, cf cmdfunc) {
c.cmds[cmdstr] = cf
}
// Find will look up the command function for the given command input.
// If it cannot find the command it will defualt to noCmdAvailable().
// If the command is an empty string it will replay the last command.
func (c *Commands) Find(cmdstr string) cmdfunc {
cmd, ok := c.cmds[cmdstr]
if !ok {
return noCmdAvailable
}
// Allow <enter> to replay last command
c.cmds[""] = cmd
return cmd
}
func CommandFunc(fn func() error) cmdfunc {
return func(p *proctl.DebuggedProcess, args ...string) error {
return fn()
}
}
func noCmdAvailable(p *proctl.DebuggedProcess, ars ...string) error {
return fmt.Errorf("command not available")
}
func nullCommand(p *proctl.DebuggedProcess, ars ...string) error {
return nil
}
// cmd continue
func cont(p *proctl.DebuggedProcess, ars ...string) error {
err := p.Continue()
if err != nil {
return err
}
return printcontext(p)
}
// cmd step
func step(p *proctl.DebuggedProcess, args ...string) error {
err := p.Step()
if err != nil {
return err
}
return printcontext(p)
}
// cmd next
func next(p *proctl.DebuggedProcess, args ...string) error {
err := p.Next()
if err != nil {
return err
}
return printcontext(p)
}
// cmd clear
func clear(p *proctl.DebuggedProcess, args ...string) error {
fname := args[0]
fn := p.GoSymTable.LookupFunc(fname)
if fn == nil {
return fmt.Errorf("No function named %s", fname)
}
bp, err := p.Clear(fn.Entry)
if err != nil {
return err
}
fmt.Printf("Breakpoint cleared at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)
return nil
}
// cmd break
func breakpoint(p *proctl.DebuggedProcess, args ...string) error {
var (
fn *gosym.Func
pc uint64
fname = args[0]
)
if strings.ContainsRune(fname, ':') {
// add breakpoint in location file:lineno
fl := strings.Split(fname, ":")
f, err := filepath.Abs(fl[0])
if err != nil {
return err
}
l, err := strconv.Atoi(fl[1])
if err != nil {
return err
}
pc, fn, err = p.GoSymTable.LineToPC(f, l)
if err != nil {
return err
}
} else {
// add breakpoint in location package.funcName
fn = p.GoSymTable.LookupFunc(fname)
if fn == nil {
return fmt.Errorf("No function named %s", fname)
}
pc = fn.Entry
}
bp, err := p.Break(uintptr(pc))
if err != nil {
return err
}
fmt.Printf("Breakpoint set at %#v for %s %s:%d\n", bp.Addr, bp.FunctionName, bp.File, bp.Line)
return nil
}
// print context when executing cmds: step, continue, next
func printcontext(p *proctl.DebuggedProcess) error {
var context []string
regs, err := p.Registers()
if err != nil {
return err
}
f, l, _ := p.GoSymTable.PCToLine(regs.PC())
fmt.Printf("Stopped at: %s:%d\n", f, l)
file, err := os.Open(f)
if err != nil {
return err
}
defer file.Close()
buf := bufio.NewReader(file)
for i := 1; i <= l+5; i++ {
line, err := buf.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
if i >= (l - 5) {
if i == l {
line = "=>" + line
}
context = append(context, line)
}
}
fmt.Println(strings.Join(context, " "))
return nil
}