/
debug.go
190 lines (173 loc) · 3.81 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
package export
import (
"bytes"
"os"
"reflect"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"strconv"
"time"
)
func readOSArgs() []byte {
var reply []byte
for _, arg := range os.Args {
if reply != nil {
reply = append(reply, ' ')
}
hasspace := false
for i := 0; i < len(arg); i++ {
if arg[i] == ' ' {
hasspace = true
break
}
}
if hasspace {
reply = append(reply, '"')
reply = append(reply, arg...)
reply = append(reply, '"')
} else {
reply = append(reply, arg...)
}
}
return reply
}
func readNumGoroutine() []byte {
return strconv.AppendInt(nil, int64(runtime.NumGoroutine()), 10)
}
func readRuntimeStack() []byte {
n := 1 << 14
var buf []byte
for n <= 64<<20 {
buf = make([]byte, n)
n = runtime.Stack(buf, true)
if n < len(buf) {
break
}
n += n
}
return buf
}
func readMemStats() []byte {
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
return structToJSON(make([]byte, 0, 128), nil, reflect.ValueOf(&mem).Elem())
}
func handleGCStats(v string) []byte {
if v == "gc" {
runtime.GC()
}
var st debug.GCStats
debug.ReadGCStats(&st)
return structToJSON(make([]byte, 0, 64), nil, reflect.ValueOf(&st).Elem())
}
func handleGOMAXPROCS(v string) []byte {
n := 0
if v != "" {
n, _ = strconv.Atoi(v)
}
return strconv.AppendInt(nil, int64(runtime.GOMAXPROCS(n)), 10)
}
func handleSetMaxThreads(v string) []byte {
n := 0
if v != "" {
n, _ = strconv.Atoi(v)
}
return strconv.AppendInt(nil, int64(debug.SetMaxThreads(n)), 10)
}
func handleSetGCPercent(v string) []byte {
n := 0
if v != "" {
n, _ = strconv.Atoi(v)
}
return strconv.AppendInt(nil, int64(debug.SetGCPercent(n)), 10)
}
func listProfiles() []byte {
var reply []byte
for _, p := range pprof.Profiles() {
reply = append(reply, p.Name()...)
reply = append(reply, ' ')
reply = strconv.AppendInt(reply, int64(p.Count()), 10)
reply = append(reply, '\n')
}
return reply
}
func fetchProfile(args []string) []byte {
if len(args) == 0 {
return nil
}
debug := 1
if len(args) >= 2 {
debug, _ = strconv.Atoi(args[1])
}
p := pprof.Lookup(args[0])
if p == nil {
return nil
}
var buf bytes.Buffer
p.WriteTo(&buf, debug)
return buf.Bytes()
}
func onCmdPprof(args []string) []byte {
if len(args) == 0 {
return listProfiles()
}
return fetchProfile(args)
}
func onCmdTrace(args []string) []byte {
if len(args) == 0 || args[0] != "start" {
return []byte("start [seconds:1]\n")
}
var sec int64
if len(args) == 2 {
sec, _ = strconv.ParseInt(args[1], 10, 64)
}
if sec <= 0 {
sec = 1
}
var buf bytes.Buffer
err := trace.Start(&buf)
if err != nil {
return append([]byte(err.Error()), '\n')
}
time.Sleep(time.Duration(sec) * time.Second)
trace.Stop()
return buf.Bytes()
}
func onCmdProfile(args []string) []byte {
if len(args) == 0 || args[0] != "start" {
return []byte("start [seconds:30]\n")
}
var sec int64
if len(args) == 2 {
sec, _ = strconv.ParseInt(args[1], 10, 64)
}
if sec <= 0 {
sec = 30
}
var buf bytes.Buffer
err := pprof.StartCPUProfile(&buf)
if err != nil {
return append([]byte(err.Error()), '\n')
}
time.Sleep(time.Duration(sec) * time.Second)
pprof.StopCPUProfile()
return buf.Bytes()
}
func init() {
vars["os.Args"] = GetOnlyFunc(readOSArgs)
vars["runtime.GOOS"] = runtime.GOOS
vars["runtime.GOARCH"] = runtime.GOARCH
vars["runtime.Version"] = runtime.Version()
vars["runtime.NumGoroutine"] = GetOnlyFunc(readNumGoroutine)
vars["runtime.Stack"] = GetOnlyFunc(readRuntimeStack)
vars["runtime.MemStats"] = GetOnlyFunc(readMemStats)
vars["runtime.GOMAXPROCS"] = VarFunc(handleGOMAXPROCS)
vars["debug.GCStats"] = VarFunc(handleGCStats)
vars["debug.SetMaxThreads"] = VarFunc(handleSetMaxThreads)
vars["debug.SetGCPercent"] = VarFunc(handleSetGCPercent)
cmds["PPROF"] = onCmdPprof
cmds["TRACE"] = onCmdTrace
cmds["PROFILE"] = onCmdProfile
}