-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.go
406 lines (381 loc) · 10 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package nject
import (
"fmt"
"reflect"
"strings"
"sync"
"sync/atomic"
)
var (
debugLock sync.RWMutex
debug uint32
debugOutput string
debugOutputMu sync.Mutex
)
var (
debuglnHook func(...interface{})
debugfHook func(string, ...interface{})
)
func debugEnabled() bool {
return atomic.LoadUint32(&debug) == 1
}
func debugln(stuff ...interface{}) {
if !debugEnabled() {
return
}
debugOutputMu.Lock()
if debuglnHook != nil {
debuglnHook(stuff...)
} else {
debugOutput += fmt.Sprintln(stuff...)
}
debugOutputMu.Unlock()
}
func debugf(format string, stuff ...interface{}) {
if !debugEnabled() {
return
}
debugOutputMu.Lock()
if debugfHook != nil {
debugfHook(format, stuff...)
} else {
debugOutput += fmt.Sprintf(format+"\n", stuff...)
}
debugOutputMu.Unlock()
}
func captureDoBindDebugging(sc *Collection, invokeF *provider, initF *provider) string {
debugLock.Lock()
if atomic.SwapUint32(&debug, 1) == 1 {
return "already capturing"
}
defer func() {
atomic.StoreUint32(&debug, 0)
debugLock.Unlock()
}()
debugOutputMu.Lock()
debugOutput = ""
debugOutputMu.Unlock()
_ = doBind(sc, invokeF, initF, false)
funcs := make([]*provider, len(sc.contents))
for i, f := range sc.contents {
funcs[i], _ = characterizeFunc(f, charContext{inputsAreStatic: true})
}
reproduce := generateReproduce(funcs, invokeF, initF)
debugOutputMu.Lock()
debugStr := debugOutput + "\n\n\n" + reproduce
debugOutputMu.Unlock()
return debugStr
}
func dumpValueArray(va []reflect.Value, context string, vMap map[typeCode]int) {
if !debugEnabled() {
return
}
if len(vMap) > 0 {
reverseMap := make(map[int]reflectType)
for tc, i := range vMap {
reverseMap[i] = tc.Type()
}
for i, v := range va {
if v.IsValid() {
debugf("value at %s: %d: %s: %s: %v", context, i, reverseMap[i], v.Type(), v.Interface())
} else {
debugf("value at %s: %d: %s: UNINITIALIZED", context, i, reverseMap[i])
}
}
return
}
for i, v := range va {
if v.IsValid() {
debugf("value at %s: %d: %s: %v", context, i, v.Type(), v.Interface())
} else {
debugf("value at %s: %d: UNINITIALIZED", context, i)
}
}
}
/*
func dumpVmap(context string, vMap map[typeCode]int) {
if !debug {
return
}
out := "value map " + context
for tc, i := range vMap {
out += fmt.Sprintf("\n\t%s -> %d", tc.Type(), i)
}
debugln(out)
*/
func dumpF(context string, fm *provider) {
if !debugEnabled() {
return
}
var out string
out += fmt.Sprintf("%s: ID %d: %s", context, fm.id, fm)
out += fmt.Sprintf("\n\tclass: %s\n\tgroup: %s", fm.class, fm.group)
for name, flow := range fm.flows {
if len(flow) > 0 {
out += fmt.Sprintf("\n\t%s flow: %s", flowType(name), formatFlow(flow))
}
}
for upDown, rMap := range map[string]map[typeCode]typeCode{
"up": fm.upRmap,
"down": fm.downRmap,
"bypass": fm.bypassRmap,
} {
if len(rMap) > 0 {
out += fmt.Sprintf("\n\t%s map:", upDown)
for from, to := range rMap {
out += fmt.Sprintf("\n\t\t%s -> %s", from.Type(), to.Type())
}
}
}
if fm.include {
out += "\n\tincluded"
}
if fm.required {
out += "\n\trequired"
} else if fm.wanted {
out += "\n\twanted"
} else if fm.desired {
out += "\n\tdesired"
}
if fm.mustConsume {
out += "\n\tmust consume"
}
if fm.memoized || fm.memoize {
out += fmt.Sprintf("\n\tmemoize %v memoized %v", fm.memoize, fm.memoized)
}
if fm.cannotInclude != nil {
out += fmt.Sprintf("\n\tCANNOT: %s", fm.cannotInclude)
}
for param, users := range fm.d.usesDetail {
for tc, dep := range users {
out += fmt.Sprintf("\n\tUSES: %s (%s %s)", dep, flowType(param), tc)
}
}
for _, dep := range fm.d.usedBy {
out += fmt.Sprintf("\n\tUSED BY: %s", dep)
}
debugln(out)
}
func formatFlow(flow []typeCode) string {
if !debugEnabled() {
return ""
}
var types []string
for _, tc := range flow {
types = append(types, tc.Type().String())
}
return strings.Join(types, ", ")
}
func elem(i interface{}) reflect.Type {
t := reflect.TypeOf(i)
if t.Kind() == reflect.Ptr {
return t.Elem()
}
return t
}
func generateReproduce(funcs []*provider, invokeF *provider, initF *provider) string {
subs := make(map[typeCode]string)
var t string
var f string
f += "\twrapTest(t, func(t *testing.T) {\n"
f += "\t\tcalled := make(map[string]int)\n"
f += "\t\tvar invoker " + funcSig(subs, &t, elem(invokeF.fn)) + "\n"
initName := "nil"
if initF != nil {
f += "\t\tvar initer " + funcSig(subs, &t, elem(initF.fn)) + "\n"
initName = "&initer"
}
f += "\t\trequire.NoError(t,\n"
f += "\t\t\tSequence(\"regression\",\n"
var inCluster int32
for _, fm := range funcs {
if fm == nil {
continue
}
if fm.isSynthetic {
continue
}
if fm.fn == nil {
continue
}
if fm.cluster != 0 {
switch inCluster {
case 0:
f += fmt.Sprintf("\t\t\t\tCluster(\"c%d\",\n", fm.cluster-1)
case fm.cluster:
// do nothing
default:
f += fmt.Sprintf("\t\t\t\t),\n\t\t\t\tCluster(\"c%d\",\n", fm.cluster-1)
}
inCluster = fm.cluster
} else if inCluster != 0 {
f += "\t\t\t\t),\n"
inCluster = 0
}
var extraIndent string
if inCluster != 0 {
extraIndent = "\t"
}
f += "\t\t\t\t" + extraIndent
close := ""
for annotation, active := range map[string]bool{
"NonFinal": fm.nonFinal,
"Cacheable": fm.cacheable,
"MustCache": fm.mustCache,
"Required": fm.required,
"CallsInner": fm.callsInner,
"Memoize": fm.memoize,
"Loose": fm.loose,
"Reorder": fm.reorder,
"OverridesError": fm.overridesError,
"Desired": fm.desired,
"Shun": fm.shun,
"NotCacheable": fm.notCacheable,
"MustConsume": fm.mustConsume,
"ConsumptionOptional": fm.consumptionOptional,
"Singleton": fm.singleton,
} {
if active {
f += annotation + "("
close += ")"
}
}
n := fm.origin
if fm.index != -1 {
n = fmt.Sprintf("%s-%d", fm.origin, fm.index)
}
f += fmt.Sprintf("Provide(%q, ", n)
close += ")"
typ := reflect.TypeOf(fm.fn)
if typ.Kind() == reflect.Func {
f += "func("
skip := 0
if fm.class == wrapperFunc {
f += "inner " + funcSig(subs, &t, typ.In(0))
if len(typesIn(typ)) > 1 {
f += ", "
}
skip = 1
}
f += strings.Join(addVarnames(substituteTypes(subs, &t, typesIn(typ)[skip:])), ", ") + ") "
out := typesOut(typ)
switch len(out) {
case 0:
// nothing
case 1:
f += " " + substituteTypes(subs, &t, out)[0]
default:
f += " (" + strings.Join(substituteTypes(subs, &t, out), ", ") + ")"
}
if fm.class == wrapperFunc {
f += " {\n"
f += fmt.Sprintf("%s\t\t\t\t\tcalled[%q]++\n", extraIndent, n)
f += extraIndent + "\t\t\t\t\tinner(" + strings.Join(substituteDefaults(subs, typesIn(typ.In(0))), ", ") + ")\n"
if len(out) > 0 {
f += extraIndent + "\t\t\t\t\treturn " + strings.Join(substituteDefaults(subs, out), ", ") + "\n"
}
f += extraIndent + "\t\t\t\t}"
} else {
if len(out) > 0 {
f += fmt.Sprintf(" { called[%q]++; return %s }", n, strings.Join(substituteDefaults(subs, out), ", "))
} else {
f += fmt.Sprintf(" { called[%q]++ }", n)
}
}
f += close + ","
if fm.include {
f += " // included"
}
f += "\n"
} else {
tca := substituteTypes(subs, &t, []reflect.Type{typ})
def := substituteDefaults(subs, []reflect.Type{typ})
f += fmt.Sprintf("%s(%s)%s,\n", tca[0], def[0], close)
}
}
if inCluster != 0 {
f += "\t\t\t),\n"
}
f += "\t\t\t).Bind(&invoker, " + initName + "))\n"
if initF != nil {
f += "\t\tiniter(" + strings.Join(substituteDefaults(subs, typesIn(elem(initF.fn))), ", ") + ")\n"
}
f += "\t\tinvoker(" + strings.Join(substituteDefaults(subs, typesIn(elem(invokeF.fn))), ", ") + ")\n"
f += "\t})\n"
f += "}\n"
return "func TestRegression(t *testing.T) {\n" + t + "\n" + f
}
// TODO: take note of which interfaces implement each other and new interfaces that
// follow the same pattern.
func substituteTypes(subs map[typeCode]string, defineTypes *string, types []reflect.Type) []string {
var replacements []string
for _, typ := range types {
tc := getTypeCode(typ)
if subs[tc] == "" {
if strings.HasPrefix(typ.String(), "nject.") {
subs[tc] = strings.TrimPrefix(typ.String(), "nject.")
} else if strings.HasPrefix(typ.String(), "*nject.") {
subs[tc] = "*" + strings.TrimPrefix(typ.String(), "*nject.")
} else if tc == getTypeCode(errorType) {
subs[tc] = "error"
} else if typ.Kind() == reflect.Interface {
if typ.NumMethod() == 0 {
if typ.Name() == "interface {}" {
subs[tc] = "interface {}"
} else {
subs[tc] = fmt.Sprintf("i%03d", tc)
*defineTypes += fmt.Sprintf("type i%03d interface{} // %s\n", tc, tc)
}
} else {
subs[tc] = fmt.Sprintf("i%03d", tc)
*defineTypes += fmt.Sprintf("// %s\ntype i%03d interface{\n\tx%03d()\n}\n", tc, tc, tc)
}
} else {
subs[tc] = fmt.Sprintf("s%03d", tc)
*defineTypes += fmt.Sprintf("\t// %s\n\ttype s%03d int\n", tc, tc)
}
}
replacements = append(replacements, subs[tc])
}
return replacements
}
func substituteDefaults(subs map[typeCode]string, types []reflect.Type) []string {
var def []string
for _, typ := range types {
r := subs[getTypeCode(typ)]
if strings.HasPrefix(r, "i") {
def = append(def, "nil")
} else if strings.HasPrefix(r, "s") {
def = append(def, "0")
} else if r == "InjectorsDebugging" {
def = append(def, `""`)
} else if r == "InjectorsReproduce" {
def = append(def, `""`)
} else {
def = append(def, "nil")
}
}
return def
}
func funcSig(subs map[typeCode]string, defineTypes *string, typ reflect.Type) string {
f := "func("
f += strings.Join(substituteTypes(subs, defineTypes, typesIn(typ)), ", ")
f += ") "
out := typesOut(typ)
switch len(out) {
case 0:
// nothing
case 1:
f += " " + substituteTypes(subs, defineTypes, out)[0]
default:
f += " (" + strings.Join(substituteTypes(subs, defineTypes, out), ", ") + ")"
}
return f
}
func addVarnames(in []string) []string {
var out []string
for _, v := range in {
out = append(out, fmt.Sprintf("_ %s", v))
}
return out
}