-
Notifications
You must be signed in to change notification settings - Fork 9
/
watchpoint.go
314 lines (271 loc) · 7.08 KB
/
watchpoint.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
// Copyright 2013 Unknown
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package beewatch
import (
"bytes"
"fmt"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"github.com/Unknwon/com"
)
var debuggerMutex = sync.Mutex{}
type WatchPoint struct {
disabled bool
watchLevel debugLevel
offset int
}
// Trace debug level.
func Trace() *WatchPoint {
return setlevel(LevelTrace)
}
// Info debug level.
func Info() *WatchPoint {
return setlevel(LevelInfo)
}
// Critical debug level.
func Critical() *WatchPoint {
return setlevel(LevelCritical)
}
func setlevel(wl debugLevel) *WatchPoint {
disabled := false
if !App.WatchEnabled || watchLevel >= LevelInfo {
disabled = true
}
return &WatchPoint{
disabled: disabled,
watchLevel: wl,
offset: 1,
}
}
// Display sends variable name,value pairs to the debugger. Values are formatted using %#v.
// The parameter 'nameValuePairs' must be even sized.
func (wp *WatchPoint) Display(nameValuePairs ...interface{}) *WatchPoint {
if wp.disabled {
return wp
}
_, file, line, ok := runtime.Caller(wp.offset)
cmd := command{
Action: "DISPLAY",
Level: levelToStr(wp.watchLevel),
}
if ok {
cmd.addParam("go.file", file)
cmd.addParam("go.line", fmt.Sprint(line))
}
l := len(nameValuePairs)
if l%2 == 0 {
for i := 0; i < l; i += 2 {
k := nameValuePairs[i]
v := nameValuePairs[i+1]
cmd.addParam(fmt.Sprint(k), fmt.Sprintf("%#v", v))
}
} else {
com.ColorLog("[WARN] BW: Missing variable for Display(...) in: %v:%v.\n", file, line)
wp.disabled = true
return wp
}
channelExchangeCommands(wp.watchLevel, cmd)
return wp
}
// Break halts the execution of the program and waits for an instruction from the debugger (e.g. Resume).
// Break is only effective if all (if any) conditions are true. The program will resume otherwise.
func (wp *WatchPoint) Break(conditions ...bool) *WatchPoint {
if wp.disabled {
return wp
}
suspend(wp, conditions...)
return wp
}
// suspend will create a new Command and send it to the browser.
func suspend(wp *WatchPoint, conditions ...bool) {
for _, condition := range conditions {
if !condition {
return
}
}
_, file, line, ok := runtime.Caller(wp.offset)
cmd := command{
Action: "BREAK",
Level: levelToStr(wp.watchLevel),
}
if ok {
cmd.addParam("go.file", file)
cmd.addParam("go.line", fmt.Sprint(line))
if App.PrintStack {
cmd.addParam("go.stack", trimStack(string(debug.Stack())))
}
if !App.CmdMode {
cmd.WatchVars = formatWatchVars()
}
}
channelExchangeCommands(wp.watchLevel, cmd)
}
// Peel off the part of the stack that lives in hopwatch
func trimStack(stack string) string {
lines := strings.Split(stack, "\n")
c := 0
for _, line := range lines {
// means no function in this package.
if strings.Index(line, "/beewatch") == -1 {
break
}
c++
}
return strings.Join(lines[c:], "\n")
}
// Printf formats according to a format specifier and writes to the debugger screen.
func (wp *WatchPoint) Printf(format string, params ...interface{}) *WatchPoint {
if wp.disabled {
return wp
}
wp.offset += 1
var content string
if len(params) == 0 {
content = format
} else {
content = fmt.Sprintf(format, params...)
}
return wp.printcontent(content)
}
// Printf formats according to a format specifier and writes to the debugger screen.
func (wp *WatchPoint) printcontent(content string) *WatchPoint {
_, file, line, ok := runtime.Caller(wp.offset)
cmd := command{
Action: "PRINT",
Level: levelToStr(wp.watchLevel),
}
if ok {
cmd.addParam("go.file", file)
cmd.addParam("go.line", fmt.Sprint(line))
}
cmd.addParam("PRINT", content)
channelExchangeCommands(wp.watchLevel, cmd)
return wp
}
// Put a command on the browser channel and wait for the reply command.
func channelExchangeCommands(wl debugLevel, toCmd command) {
// synchronize command exchange ; break only one goroutine at a time.
debuggerMutex.Lock()
defer debuggerMutex.Unlock()
if !App.WatchEnabled || wl < watchLevel {
return
}
if App.CmdMode {
cmdExchange(toCmd)
} else {
if toCmd.Action == "BREAK" {
toCmd.addParam("go.SKIP_SUSPEND", fmt.Sprint(App.SkipSuspend))
toCmd.addParam("go.PRINT_STACK", fmt.Sprint(App.PrintStack))
toCmd.addParam("go.PRINT_SOURCE", fmt.Sprint(App.PrintSource))
}
toBrowserChannel <- toCmd
if !App.SkipSuspend {
<-fromBrowserChannel
}
}
}
func cmdExchange(cmd command) {
switch cmd.Action {
case "PRINT", "DISPLAY":
com.ColorLog("[%s] DEBUG( %s ) --> %s\n", levelToCmdFormat(cmd.Level),
getTitle(cmd), watchParametersToStr(cmd.Parameters))
case "BREAK":
if App.PrintStack {
com.ColorLog("[%s] BREAK:\n# %s #", levelToCmdFormat(cmd.Level),
cmd.Parameters["go.stack"])
if !App.SkipSuspend && App.PrintSource {
fmt.Print("press ENTER to view source...")
fmt.Scanln()
}
} else {
com.ColorLog("[%s] BREAK: 'print_stack' disenabled.\n",
levelToCmdFormat(cmd.Level))
}
if App.PrintSource {
line, _ := strconv.Atoi(cmd.Parameters["go.line"])
fmt.Println()
com.ColorLog("[%s] Source( %s ):\n%s", levelToCmdFormat(cmd.Level),
cmd.Parameters["go.file"], getFileSource(cmd.Parameters["go.file"], line))
}
if !App.SkipSuspend {
fmt.Print("press ENTER to continue...")
fmt.Scanln()
}
}
}
func levelToCmdFormat(level string) string {
switch level {
case "TRACE":
return "TRAC"
case "INFO":
return level
case "CRITICAL":
return "WARN"
default:
return level
}
}
func getTitle(cmd command) string {
fp := cmd.Parameters["go.file"]
i := strings.LastIndex(fp, "/") + 1
return fp[i:] + ":" + cmd.Parameters["go.line"]
}
func watchParametersToStr(parameters map[string]string) string {
buf := new(bytes.Buffer)
multiline := false
for n, v := range parameters {
if !strings.HasPrefix(n, "go.") {
if multiline {
buf.WriteString(", ")
}
buf.WriteString(n + " => " + v)
multiline = true
}
}
return buf.String()
}
func getFileSource(path string, line int) string {
b, err := loadFile(path)
if err != nil {
colorLog("[ERRO] BW: Fail to load source file[ %s ]\n", err)
}
s := strings.Split(string(b), "\n")
buf := new(bytes.Buffer)
for i, v := range s {
if i+1 <= line-10 {
continue
} else if i+1 >= line+10 {
break
}
buf.WriteString(fmt.Sprint(i + 1))
buf.WriteString(computeSpaces(i+1, len(s)))
v = strings.Replace(v, "\t", " ", -1)
if i != line-1 {
buf.WriteString(v)
} else {
if v[len(v)-1] == ',' || v[len(v)-1] == '.' {
line++
}
buf.WriteString("# " + v + " #")
}
buf.WriteString("\n")
}
return buf.String()
}
func computeSpaces(line, total int) string {
return strings.Repeat(" ", (total/10)-(line/10)+1)
}