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