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
/
smali_trace.py
90 lines (74 loc) · 3.01 KB
/
smali_trace.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
81
82
83
84
85
86
87
88
89
90
import os
# directory path of the .smali files
directory = "./"
ignored_packages = [
"google", "android",
"kotlin", "kotlinx",
"java", "javax",
]
ignored_methods = [
"handleMessage",
"executeGLThreadJobs",
"isFinishing",
"access$300"
]
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:
filename = os.path.join(root, filename_)
if "trace" in filename:
continue
print(filename)
package_name = ""
if filename.endswith(".smali"):
filepath = os.path.join(directory, filename)
with open(filepath, "r") as file:
lines = file.readlines()
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
def is_ignored_method(signature):
return signature is None or \
any(x in signature for x in ignored_methods)
class_name = None
trace_added = False
method_name = None
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") and not trace_added:
if "traceLastMethodCall" not in l_i:
file.write("\n" + static_void_trace + "\n" + lines[i])
else:
file.write(lines[i])
i += 1
trace_added = True
continue
elif l_i.startswith(".method"):
try:
method_name = l_i.split()[-1].split("(")[0]
except IndexError:
pass
if l_i.startswith(".locals") and method_predicate(strip_line(i - 1)) and not is_ignored_method(method_name):
locals_count = int(l_i.split()[1])
file.write("\n .locals {}\n".format(locals_count))
if class_name is not None:
file.write(f" invoke-static {{}}, L{class_name};->traceLastMethodCall()V\n")
i += 1
continue
file.write(lines[i])
i += 1