forked from sirnewton01/godev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.go
237 lines (198 loc) · 4.7 KB
/
debug.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
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/build"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"code.google.com/p/go.net/websocket"
)
func debugSocket(ws *websocket.Conn) {
url := ws.Request().URL
// Short circuit for "go run" case
if url.Query().Get("run") != "" {
goRun(ws, url.Query().Get("run"))
return
}
debug := url.Query().Get("debug") == "true"
race := url.Query().Get("race") == "true"
cmd := url.Query().Get("cmd")
params := url.Query().Get("params")
rungodbg := false
paramList := strings.Split(params, " ")
if debug {
godbgtest := exec.Command("godbg")
err := godbgtest.Run()
if err == nil {
rungodbg = true
}
}
// clean and install the command to make sure it can be compiled
// and is up-to-date. Godbg will do its own installation afterwards
// but this is a good check here.
cleanCmd := exec.Command("go", "clean", "-i", "-r", cmd)
cleanCmd.Run()
// Ignore errors on clean because recursive clean often fails trying to delete
// files from the Go installation directory, which may not be modifiable by
// the current user. This is a best attempt at this stage.
installCmd := exec.Command("go", "install", cmd)
if race {
installCmd = exec.Command("go", "install", "-race", cmd)
}
err := installCmd.Run()
if err != nil {
ws.Write([]byte("Error installing command:" + err.Error()))
ws.Close()
return
}
if !rungodbg {
commandName := filepath.Base(cmd)
gopaths := filepath.SplitList(build.Default.GOPATH)
foundCommand := false
for _, gopath := range gopaths {
cmdPath := filepath.Join(gopath, "bin", commandName)
_, err := os.Stat(cmdPath)
if err == nil {
cmd = cmdPath
foundCommand = true
break
}
// Try again with windows .exe extension
cmdPath = filepath.Join(gopath, "bin", commandName+".exe")
_, err = os.Stat(cmdPath)
if err == nil {
cmd = cmdPath
foundCommand = true
break
}
}
if !foundCommand {
ws.Write([]byte("Command not found in any GOPATH"))
ws.Close()
return
}
} else {
paramList = append([]string{"-openBrowser=false", cmd}, paramList...)
cmd = "godbg"
}
c := exec.Command(cmd, paramList...)
out, in, err := start(c)
if err != nil {
panic(err)
}
go func() {
for {
buf := make([]byte, 1024, 1024)
n, err := out.Read(buf)
if err != nil {
break
}
n, err = ws.Write(buf[:n])
if err != nil {
break
}
}
ws.Close()
}()
buf := make([]byte, 1024, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
break
}
n, err = in.Write(buf[:n])
if err != nil {
break
}
}
in.Close()
out.Close()
c.Wait()
}
func goRun(ws *websocket.Conn, file string) {
var ospath string
gopaths := filepath.SplitList(build.Default.GOPATH)
for _, gopath := range gopaths {
p := filepath.Join(gopath, "src", file)
_, err := os.Stat(p)
if err == nil {
ospath = p
break
}
}
// File was not found
if ospath == "" {
return
}
c := exec.Command("go", "run", ospath)
out, in, err := start(c)
if err != nil {
panic(err)
}
go func() {
for {
buf := make([]byte, 1024, 1024)
n, err := out.Read(buf)
if err != nil {
break
}
n, err = ws.Write(buf[:n])
if err != nil {
break
}
}
ws.Close()
}()
buf := make([]byte, 1024, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
break
}
n, err = in.Write(buf[:n])
if err != nil {
break
}
}
out.Close()
in.Close()
c.Wait()
}
func debugHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "GET" && len(pathSegs) == 2 && pathSegs[1] == "commands":
commands := []string{}
srcDirs := build.Default.SrcDirs()
for _, srcDir := range srcDirs {
// Skip stuff that is in the GOROOT
if filepath.HasPrefix(srcDir, goroot) {
continue
}
filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
pkg, err := build.Default.ImportDir(path, 0)
if err == nil && pkg.IsCommand() {
commands = append(commands, pkg.ImportPath)
}
}
return nil
})
}
ShowJson(writer, 200, commands)
return true
case req.Method == "GET" && len(pathSegs) == 2 && pathSegs[1] == "debugSupport":
godbgtest := exec.Command("godbg")
err := godbgtest.Run()
if err != nil {
ShowError(writer, 404, "godbg command could not be found on the system path. Install using 'go get github.com/sirnewton01/godbg', install it and add the binary to the path for debugging support.", err)
return true
}
ShowJson(writer, 200, []string{})
return true
}
return false
}