This repository has been archived by the owner on Mar 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace_cleaner.py
80 lines (63 loc) · 2.56 KB
/
trace_cleaner.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
import os
directory = "./"
ignored_packages = [
"google", "android",
"kotlin", "kotlinx",
"java", "javax",
]
ignored_methods = set()
with open("./trace.smali", "r") as f:
static_void_trace = f.read()
for root, dir_, filenames in os.walk(directory):
if any(x in root for x in ignored_packages):
print("package ignored:", root)
continue
for filename_ in filenames:
if "trace" in filename_:
continue
filename = os.path.join(root, filename_)
print(filename)
package_name = ""
if filename.endswith(".smali"):
filepath = os.path.join(directory, filename)
with open(filepath, "r") as file:
lines = [x+"\n" for x in file.read().replace(static_void_trace, "\n").split("\n")]
with open(filepath, "w") as file:
i = 0
def strip_line(i_):
return lines[i_].strip()
def method_predicate(signature):
return not "abstract" in signature \
and not "native" in signature
class_name = None
trace_added = False
method_name = None
trace_line = "traceLastMethodCall"
while i < len(lines):
l_i = strip_line(i)
if l_i.startswith(".class"):
class_name = l_i.split()[-1][1:-1]
package_name = ".".join(class_name.split(".")[:-1])
print("CLASS: ", class_name, package_name)
file.write(lines[i])
i += 1
continue
if l_i.startswith(".method"):
file.write(l_i)
i += 1
trace_added = True
try:
method_name = l_i.split()[-1].split("()")[0]
except IndexError:
pass
print("METHOD: ", method_name)
continue
if method_name not in ignored_methods \
and trace_line in lines[i]:
print("trace removed:", class_name, method_name)
i += 1
elif method_name in ignored_methods \
and trace_line in lines[i]:
print("method skipped:", class_name, method_name)
file.write(lines[i])
i += 1