]> Witch of Git - ivy/blob - tools/format_trace.py
[Docs] Mention run.py and trace.py in the README
[ivy] / tools / format_trace.py
1 #!/usr/bin/env python3
2 import argparse
3 import subprocess
4 import sys
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
28 def obj(x):
29 if int(x, 16) % 2 == 1:
30 return int(x, 16) >> 1
31 if x in objs:
32 return objs[x]
33 elif x in syms:
34 return syms[x]
35 return x
36
37 for line in lines:
38 if not line.strip():
39 print("Blank line")
40 continue
41 kind, *args = line.split()
42 if kind == "MAKE":
43 objs[args[0]] = syms.get(args[1], f"fn@{args[1]}")
44 if not only_debug:
45 print("MAKE", objs[args[0]])
46 elif kind == "COPY":
47 objs[args[1]] = obj(args[0])
48 elif kind == "APP":
49 if int(args[1], 16) == 0:
50 objs[args[0]] = f"({obj(args[0])})"
51 else:
52 objs[args[0]] = f"({obj(args[0])} {obj(args[1])})"
53 if not only_debug:
54 print("APP", objs[args[0]])
55 elif kind == "DEBUG":
56 if only_debug:
57 print(obj(args[0]))
58 else:
59 print("DEBUG", obj(args[0]))
60 elif kind == "UPD8":
61 pass # who cares :P
62 else:
63 if not only_debug:
64 print(kind, *(obj(x) for x in args))
65
66
67 if __name__ == "__main__":
68 parser = argparse.ArgumentParser(
69 description="Make an Ivy runtime trace more readable",
70 )
71 parser.add_argument("program", help="The program that produced the trace")
72 parser.add_argument("trace", help="A file containing the trace")
73 parser.add_argument(
74 "-d",
75 "--only-debug",
76 action="store_true",
77 help="If set, only print the DEBUG outputs",
78 )
79 if len(sys.argv) == 1:
80 parser.print_help()
81 sys.exit(0)
82 args = parser.parse_args()
83
84 syms = get_syms(args.program)
85 with open(args.trace, "r") as f:
86 lines = [line.strip() for line in f if line.strip()]
87 handle_trace(lines, syms, ags.only_debug)