]> Witch of Git - ivy/blob - tools/format_trace.py
[tools] Add tools/run.py to compile and immediately run scripts
[ivy] / tools / format_trace.py
1 import argparse
2 import sys
3 import subprocess
4
5
6 def get_syms(program):
7 result = subprocess.run(
8 ["nm", "-defined-only", "-demangle", program],
9 check=True,
10 capture_output=True,
11 encoding="utf8",
12 )
13 syms = {}
14 for line in result.stdout.split("\n"):
15 if not line.strip():
16 continue
17 addr, _, name = line.split(' ', 2)
18 if name.startswith("ivy_builtin$"):
19 name = name.split("$", 2)[1]
20 syms[addr] = name
21 return syms
22
23
24 def handle_trace(lines, syms={}, only_debug=False):
25 objs = {}
26 def obj(x):
27 if int(x, 16) % 2 == 1:
28 return int(x, 16) >> 1
29 if x in objs:
30 return objs[x]
31 elif x in syms:
32 return syms[x]
33 return x
34
35 for line in lines:
36 if not line.strip():
37 print("Blank line")
38 continue
39 kind, *args = line.split()
40 if kind == "MAKE":
41 objs[args[0]] = syms.get(args[1], f"fn@{args[1]}")
42 if not only_debug:
43 print("MAKE", objs[args[0]])
44 elif kind == "COPY":
45 objs[args[1]] = obj(args[0])
46 elif kind == "APP":
47 if int(args[1], 16) == 0:
48 objs[args[0]] = f"({obj(args[0])})"
49 else:
50 objs[args[0]] = f"({obj(args[0])} {obj(args[1])})"
51 if not only_debug:
52 print("APP", objs[args[0]])
53 elif kind == "DEBUG":
54 if only_debug:
55 print(obj(args[0]))
56 else:
57 print("DEBUG", obj(args[0]))
58 elif kind == "UPD8":
59 pass # who cares :P
60 else:
61 if not only_debug:
62 print(kind, *(obj(x) for x in args))
63
64
65 if __name__ == "__main__":
66 parser = argparse.ArgumentParser(
67 description="Make an Ivy runtime trace more readable",
68 )
69 parser.add_argument("program", help="The program that produced the trace")
70 parser.add_argument("trace", help="A file containing the trace")
71 parser.add_argument(
72 "-d", "--only-debug", action="store_true",
73 help="If set, only print the DEBUG outputs",
74 )
75 if len(sys.argv) == 1:
76 parser.print_help()
77 sys.exit(0)
78 args = parser.parse_args()
79
80 syms = get_syms(args.program)
81 with open(args.trace, "r") as f:
82 lines = [line.strip() for line in f if line.strip()]
83 handle_trace(lines, syms, ags.only_debug)