-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
breakpoints.go
237 lines (202 loc) · 5.48 KB
/
breakpoints.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"github.com/aarzilli/gdlv/internal/dlvclient/service/api"
)
type frozenBreakpoint struct {
Bp api.Breakpoint
LineInFunction int
LineContents string
}
var FrozenBreakpoints []frozenBreakpoint
// Saves position information for bp in FrozenBreakpoints
func freezeBreakpoint(out io.Writer, bp *api.Breakpoint) {
if bp == nil || bp.ID < 0 || bp.FunctionName == "" || bp.File == "" {
return
}
var fbp frozenBreakpoint
fbp.Bp = *bp
locs, _, err := client.FindLocation(api.EvalScope{-1, 0, 0}, fbp.Bp.FunctionName, true, nil)
if err != nil || len(locs) != 1 || locs[0].Function == nil || locs[0].Function.Name() != fbp.Bp.FunctionName {
fmt.Fprintf(out, "Function not found while recording breakpoint\n")
return
}
functionLoc := locs[0]
if functionLoc.File != bp.File {
fmt.Fprintf(out, "File mismatch while recording breakpoint\n")
return
}
fbp.LineInFunction = bp.Line - functionLoc.Line
if fbp.LineInFunction > 0 {
fh, err := os.Open(bp.File)
if err != nil {
fmt.Fprintf(out, "Could not open source file while recording breakpoint\n")
return
}
defer fh.Close()
// check for executable staleness
fi, _ := fh.Stat()
lastModExe := client.LastModified()
if fi.ModTime().After(lastModExe) {
// executable is stale
fmt.Fprintf(out, "Breakpoint set on stale executable\n")
return
}
buf := bufio.NewScanner(fh)
lineno := 0
for buf.Scan() {
lineno++
if bp.Line == lineno {
fbp.LineContents = buf.Text()
break
}
}
}
FrozenBreakpoints = append(FrozenBreakpoints, fbp)
saveConfiguration()
}
func removeFrozenBreakpoint(bp *api.Breakpoint) {
if bp == nil {
return
}
for i := range FrozenBreakpoints {
if FrozenBreakpoints[i].Bp.ID == bp.ID {
copy(FrozenBreakpoints[i:], FrozenBreakpoints[i+1:])
FrozenBreakpoints = FrozenBreakpoints[:len(FrozenBreakpoints)-1]
break
}
}
saveConfiguration()
}
// Collect breakpoint configuration of all frozen breakpoints
func updateFrozenBreakpoints() {
for i := range FrozenBreakpoints {
bp, err := client.GetBreakpoint(FrozenBreakpoints[i].Bp.ID)
if err == nil {
FrozenBreakpoints[i].Bp = *bp
}
}
}
// Clears all breakpoints in FrozenBreakpoints
func clearFrozenBreakpoints() {
for _, fbp := range FrozenBreakpoints {
client.ClearBreakpoint(fbp.Bp.ID)
}
}
func restoreFrozenBreakpoints(out io.Writer) {
// Restore frozen breakpoints
for i := range FrozenBreakpoints {
FrozenBreakpoints[i].Restore(out)
}
// Re-freeze breakpoints
FrozenBreakpoints = FrozenBreakpoints[:0]
bps, err := client.ListBreakpoints(false)
if err != nil {
return
}
for _, bp := range bps {
if bp.ID >= 0 {
freezeBreakpoint(out, bp)
}
}
}
func (fbp *frozenBreakpoint) Restore(out io.Writer) {
if fbp.Bp.FunctionName == "" || fbp.Bp.File == "" {
return
}
if fbp.LineInFunction == 0 {
fbp.Bp.Addr = 0
fbp.Bp.File = ""
fbp.Bp.Line = -1
_, err := client.CreateBreakpoint(&fbp.Bp)
if err != nil {
fmt.Fprintf(out, "Could not restore breakpoint at function %s: %v\n", fbp.Bp.FunctionName, err)
}
return
}
locs, _, err := client.FindLocation(api.EvalScope{-1, 0, 0}, fbp.Bp.FunctionName, true, nil)
if err != nil || len(locs) != 1 || locs[0].Function == nil || locs[0].Function.Name() != fbp.Bp.FunctionName {
fmt.Fprintf(out, "Could not restore breakpoint %d, function not found\n", fbp.Bp.ID)
return
}
functionLoc := locs[0]
// Find line closest to startOfFunction + LineInFunction that matches LineContents
// If not found just set it to startOfFunction + LineInFunction
fh, err := os.Open(functionLoc.File)
if err != nil {
return
}
defer fh.Close()
dist := func(lineno int) int {
dist := lineno - (functionLoc.Line + fbp.LineInFunction)
if dist < 0 {
return -dist
}
return dist
}
bestMatch := -1
buf := bufio.NewScanner(fh)
lineno := 0
for buf.Scan() {
lineno++
if buf.Text() == fbp.LineContents {
if bestMatch < 0 || dist(lineno) < dist(bestMatch) {
bestMatch = lineno
}
}
}
if bestMatch < 0 {
bestMatch = functionLoc.Line + fbp.LineInFunction
}
fbp.Bp.Addr = 0
fbp.Bp.FunctionName = ""
fbp.Bp.File = functionLoc.File
fbp.Bp.Line = bestMatch
fbp.Set(out, &functionLoc)
}
func (fbp *frozenBreakpoint) Set(out io.Writer, functionLoc *api.Location) {
bp, err := client.CreateBreakpointWithExpr(&fbp.Bp, fmt.Sprintf("%s:%d", fbp.Bp.File, fbp.Bp.Line), nil, true)
if err != nil {
fmt.Fprintf(out, "Could not restore breakpoint at %s:%d: %v\n", fbp.Bp.File, fbp.Bp.Line, err)
return
}
savedDisabled := fbp.Bp.Disabled
fbp.Bp = *bp
fbp.Bp.Disabled = savedDisabled
if functionLoc != nil {
if bp.FunctionName != functionLoc.Function.Name() {
client.ClearBreakpoint(bp.ID)
fmt.Fprintf(out, "Could not restore breakpoint %d (function name mismatch)\n", fbp.Bp.ID)
}
}
if fbp.Bp.Disabled {
client.AmendBreakpoint(&fbp.Bp)
}
}
func disableBreakpoint(bp *api.Breakpoint) {
for i := range FrozenBreakpoints {
if FrozenBreakpoints[i].Bp.ID == bp.ID {
FrozenBreakpoints[i].Bp.Disabled = true
client.AmendBreakpoint(&FrozenBreakpoints[i].Bp)
break
}
}
saveConfiguration()
refreshState(refreshToSameFrame, clearBreakpoint, nil)
wnd.Changed()
}
func enableBreakpoint(bp *api.Breakpoint) {
for i := range FrozenBreakpoints {
if FrozenBreakpoints[i].Bp.ID == bp.ID {
FrozenBreakpoints[i].Bp.Disabled = false
client.AmendBreakpoint(&FrozenBreakpoints[i].Bp)
break
}
}
saveConfiguration()
refreshState(refreshToSameFrame, clearBreakpoint, nil)
wnd.Changed()
}