-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugging_strategy.py
235 lines (196 loc) · 7.74 KB
/
debugging_strategy.py
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
from code import interact
# from sre_parse import State
from oracle import Terminal, Browser
from query import Query
from rich import print
from etnode import ETNode
import json
class DebuggingStrategy:
def __init__(self, exec_tree, interactive, pydd_repl, oracle, tree_formatter):
self.exec_tree = exec_tree
self.interactive = interactive
self.pydd_repl = pydd_repl
self.oracle = oracle
self.tree_formatter = tree_formatter
def single_stepping(self):
if not self.exec_tree.root:
return
queries = []
stack = []
visited = []
root = self.exec_tree.root
stack.append(root)
while stack:
ptr = stack.pop()
visited.append(ptr)
if ptr.left_child:
stack.append(ptr.left_child)
if ptr.right_sibling:
stack.append(ptr.right_sibling)
while visited:
ptr = visited.pop()
print(*self.tree_formatter(self.exec_tree.root, ptr.id))
answer = self.oracle.query(ptr)
if self.interactive and answer == "No":
self.pydd_repl(ptr)
if answer == "No":
ptr.state = ETNode.State.INVALID
print(f"[bold red]Buggy node found at {ptr}[/bold red]")
return
ptr.state = ETNode.State.VALID
def top_down(self):
if not self.exec_tree.root:
return
queries = []
queue = []
ptr = self.exec_tree.root
while ptr:
queue.append(ptr)
ptr = ptr.right_sibling
while queue:
ptr = queue[0]
queue.pop(0)
print(*self.tree_formatter(self.exec_tree.root, ptr.id))
answer = self.oracle.query(ptr)
if self.interactive and answer == "No":
self.pydd_repl(ptr)
if answer == "Yes":
ptr.state = ETNode.State.VALID
if ptr.is_terminal():
print(f"[bold red]Buggy node found at {ptr.parent}[/bold red]")
return
pptr = ptr.parent
pptr.left_child = ptr.right_sibling
ptr = ptr.right_sibling
elif answer == "No":
ptr.state = ETNode.State.INVALID
ptr.right_sibling = None
if ptr.is_leaf():
print(f"[bold red]Buggy node found at {ptr}[/bold red]")
return
else:
self.exec_tree.root = ptr
ptr = ptr.left_child
queue = []
while ptr:
queue.append(ptr)
ptr = ptr.right_sibling
def heaviest_first(self):
if not self.exec_tree.root:
return
queries = []
queue = []
ptr = self.exec_tree.root
weights = {}
while ptr:
queue.append(ptr)
ptr = ptr.right_sibling
while queue:
for node in queue:
weights[str(node.id)] = (node, node.size())
queue = []
if weights:
heaviest = max(weights, key = lambda x : weights[x][1])
ptr, _ = weights[heaviest]
del weights[heaviest] # pop from dictionary
print(*self.tree_formatter(self.exec_tree.root, ptr.id))
answer = self.oracle.query(ptr)
if self.interactive and answer == "No":
self.pydd_repl(ptr)
if answer == "Yes":
ptr.state = ETNode.State.VALID
if ptr.is_terminal():
print(f"[bold red] Buggy node found at {ptr.parent} [/bold red]")
return
pptr = ptr.parent
pptr.fix_tree(ptr)
ptr = ptr.right_sibling
elif answer == "No":
ptr.state = ETNode.State.INVALID
ptr.right_sibling = None
if ptr.is_leaf():
print(f"[bold red] Buggy node found at {ptr} [/bold red]")
return
else:
self.exec_tree.root = ptr
ptr = ptr.left_child
queue = []
weights = {}
while ptr:
queue.append(ptr)
ptr = ptr.right_sibling
def _cleanup(self, weights, node, node_weight):
root = self.exec_tree.root
ptr = node
while ptr != root:
# subtract the node weight from the parent trees (recompute their weights)
weights[ptr.parent.id][1] -= node_weight
ptr = ptr.parent
if not node:
return
stack = []
if node.left_child:
stack = [node.left_child]
while stack:
ptr = stack.pop()
if ptr:
del weights[ptr.id]
if ptr.right_sibling:
stack.append(ptr.right_sibling)
if ptr.left_child:
stack.append(ptr.left_child)
def divide_and_query(self):
if not self.exec_tree.root:
return
queries = []
ptr = self.exec_tree.root
root_id = ptr.id
def get_weights(root, weights):
if not root:
return
weights[root.id] = [root, 1]
lst = get_weights(root.left_child, weights)
rst = get_weights(root.right_sibling, weights)
if lst:
weights[root.id][1] += weights[lst.id][1]
if rst:
weights[root.parent.id][1] += weights[rst.id][1]
return root
weights = {}
get_weights(ptr, weights)
_, total_weight = weights[0]
best_weight = total_weight // 2
# find the nearest value to best_weight by abs of the
# difference between the target value and the node size
best_guess = lambda e: abs(e[1][1] - best_weight)
while True:
if not weights:
return
best = min(weights.items(), key = best_guess) # get best guess
node_id, (node, node_weight) = best
del weights[node_id] # remove from dict not to check it again
print(*self.tree_formatter(self.exec_tree.root, node_id))
answer = self.oracle.query(node)
if self.interactive and answer == "No":
self.pydd_repl(node)
if answer == "Yes":
node.state = ETNode.State.VALID
if node.is_terminal() and node.parent.state == ETNode.State.INVALID:
print(f"[bold red] Buggy node found at {node.parent}[/bold red]")
return
pptr = node.parent
pptr.fix_tree(node) # perform tree adjustment
self._cleanup(weights, node, node_weight)
elif answer == "No":
node.state = ETNode.State.INVALID
node.right_sibling = None
if node.is_leaf():
print(f"[bold red] Buggy node found at {node} [/bold red]")
return
else:
root_id = node.id # set the node to be the new tree root
self.exec_tree.root = node
weights = {}
get_weights(node, weights)
_, total_weight = weights[root_id]
best_weight = total_weight // 2