forked from itchyny/gojq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
212 lines (198 loc) · 4.13 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
//go:build gojq_debug
// +build gojq_debug
package gojq
import (
"fmt"
"io"
"os"
"strconv"
"strings"
)
var (
debug bool
debugOut io.Writer
)
func init() {
if out := os.Getenv("GOJQ_DEBUG"); out != "" {
debug = true
if out == "stdout" {
debugOut = os.Stdout
} else {
debugOut = os.Stderr
}
}
}
type codeinfo struct {
name string
pc int
}
func (c *compiler) appendCodeInfo(x any) {
if !debug {
return
}
var name string
switch x := x.(type) {
case string:
name = x
default:
name = fmt.Sprint(x)
}
var diff int
if c.codes[len(c.codes)-1] != nil && c.codes[len(c.codes)-1].op == opret && strings.HasPrefix(name, "end of ") {
diff = -1
}
c.codeinfos = append(c.codeinfos, codeinfo{name, len(c.codes) + diff})
}
func (c *compiler) deleteCodeInfo(name string) {
for i := 0; i < len(c.codeinfos); i++ {
if strings.HasSuffix(c.codeinfos[i].name, name) {
copy(c.codeinfos[i:], c.codeinfos[i+1:])
c.codeinfos = c.codeinfos[:len(c.codeinfos)-1]
i--
}
}
}
func (env *env) lookupInfoName(pc int) string {
var name string
for _, ci := range env.codeinfos {
if ci.pc == pc {
if name != "" {
name += ", "
}
name += ci.name
}
}
return name
}
func (env *env) debugCodes() {
if !debug {
return
}
for i, c := range env.codes {
pc := i
switch c.op {
case opcall, opcallrec:
if x, ok := c.v.(int); ok {
pc = x
}
case opjump:
x := c.v.(int)
if x > 0 && env.codes[x-1].op == opscope {
pc = x - 1
}
}
var s string
if name := env.lookupInfoName(pc); name != "" {
switch c.op {
case opcall, opcallrec, opjump:
if !strings.HasPrefix(name, "module ") {
s = "\t## call " + name
break
}
fallthrough
default:
s = "\t## " + name
}
}
fmt.Fprintf(debugOut, "\t%d\t%s%s%s\n", i, formatOp(c.op, false), debugOperand(c), s)
}
fmt.Fprintln(debugOut, "\t"+strings.Repeat("-", 40)+"+")
}
func (env *env) debugState(pc int, backtrack bool) {
if !debug {
return
}
var sb strings.Builder
c := env.codes[pc]
fmt.Fprintf(&sb, "\t%d\t%s%s\t|", pc, formatOp(c.op, backtrack), debugOperand(c))
var xs []int
for i := env.stack.index; i >= 0; i = env.stack.data[i].next {
xs = append(xs, i)
}
for i := len(xs) - 1; i >= 0; i-- {
sb.WriteString("\t")
sb.WriteString(debugValue(env.stack.data[xs[i]].value))
}
switch c.op {
case opcall, opcallrec:
if x, ok := c.v.(int); ok {
pc = x
}
case opjump:
x := c.v.(int)
if x > 0 && env.codes[x-1].op == opscope {
pc = x - 1
}
}
if name := env.lookupInfoName(pc); name != "" {
switch c.op {
case opcall, opcallrec, opjump:
if !strings.HasPrefix(name, "module ") {
sb.WriteString("\t\t\t## call " + name)
break
}
fallthrough
default:
sb.WriteString("\t\t\t## " + name)
}
}
fmt.Fprintln(debugOut, sb.String())
}
func formatOp(c opcode, backtrack bool) string {
if backtrack {
return c.String() + " <backtrack>" + strings.Repeat(" ", 13-len(c.String()))
}
return c.String() + strings.Repeat(" ", 25-len(c.String()))
}
func (env *env) debugForks(pc int, op string) {
if !debug {
return
}
var sb strings.Builder
for i, v := range env.forks {
if i > 0 {
sb.WriteByte('\t')
}
if i == len(env.forks)-1 {
sb.WriteByte('<')
}
fmt.Fprintf(&sb, "%d, %s", v.pc, debugValue(env.stack.data[v.stackindex].value))
if i == len(env.forks)-1 {
sb.WriteByte('>')
}
}
fmt.Fprintf(debugOut, "\t-\t%s%s%d\t|\t%s\n", op, strings.Repeat(" ", 22), pc, sb.String())
}
func debugOperand(c *code) string {
switch c.op {
case opcall, opcallrec:
switch v := c.v.(type) {
case int:
return strconv.Itoa(v)
case [3]any:
return fmt.Sprintf("%s/%d", v[2], v[1])
default:
panic(c)
}
default:
return debugValue(c.v)
}
}
func debugValue(v any) string {
switch v := v.(type) {
case Iter:
return fmt.Sprintf("gojq.Iter(%#v)", v)
case []pathValue:
return fmt.Sprintf("[]gojq.pathValue(%v)", v)
case [2]int:
return fmt.Sprintf("[%d,%d]", v[0], v[1])
case [3]int:
return fmt.Sprintf("[%d,%d,%d]", v[0], v[1], v[2])
case [3]any:
return fmt.Sprintf("[%v,%v,%v]", v[0], v[1], v[2])
case allocator:
return fmt.Sprintf("%v", v)
default:
return Preview(v)
}
}