-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_debugger.py
More file actions
40 lines (29 loc) · 1.08 KB
/
live_debugger.py
File metadata and controls
40 lines (29 loc) · 1.08 KB
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
import time
from graphviz import Digraph
class LiveDebugger:
def __init__(self):
self.graph = Digraph()
self.nodes = {}
def add_nodes(self, program):
for i, instr in enumerate(program):
nid = f"N{i}"
self.nodes[i] = nid
self.graph.node(nid, instr)
for i in range(len(program) - 1):
self.graph.edge(self.nodes[i], self.nodes[i + 1])
def highlight(self, active_index, filename="live_cfg"):
g = Digraph()
for i, nid in self.nodes.items():
if i == active_index:
g.node(nid, style="filled", fillcolor="yellow")
else:
g.node(nid)
for i in range(len(self.nodes) - 1):
g.edge(self.nodes[i], self.nodes[i + 1])
g.render(f"{filename}_{active_index}", format="png", cleanup=True)
def animate(self, trace, program):
self.add_nodes(program)
for step, instr, regs in trace:
print(f"STEP {step}: {instr} | REG: {regs}")
self.highlight(step)
time.sleep(0.4)