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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
package kotlin.proxy;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* Central call log for kotlin-proxy stubs.
*
* Every stub method/constructor and every stub class load is recorded here,
* in order, so you can see exactly which stubs AGP/KGP touch.
*
* System properties:
* kotlin.proxy.log log file path (default: <user.dir>/build/kotlin-proxy.log)
* kotlin.proxy.log.mode "all" (default) | "classes" (class loads only) | "off"
*/
public final class KotlinProxyLog {
private static BufferedWriter writer;
private static boolean inited = false;
private static final String MODE = System.getProperty("kotlin.proxy.log.mode", "all");
private KotlinProxyLog() {
}
private static synchronized void ensure() {
if (inited) {
return;
}
inited = true;
try {
String path = System.getProperty("kotlin.proxy.log",
System.getProperty("user.dir") + "/build/kotlin-proxy.log");
File f = new File(path);
if (f.getParentFile() != null) {
f.getParentFile().mkdirs();
}
writer = new BufferedWriter(new FileWriter(f, true));
writer.write("# kotlin-proxy log started; mode=" + MODE + "\n");
} catch (Exception e) {
writer = null;
}
}
/** Called from a stub class static initializer. Proves the stub shadows the real class. */
public static void loaded(Class<?> stubClass) {
if ("off".equals(MODE)) {
return;
}
String from = "?";
try {
if (stubClass.getProtectionDomain().getCodeSource() != null
&& stubClass.getProtectionDomain().getCodeSource().getLocation() != null) {
from = stubClass.getProtectionDomain().getCodeSource().getLocation().toString();
}
} catch (Throwable t) {
// ignore
}
log("LOAD " + stubClass.getName() + " <- " + from);
}
/** Called from every stub method/constructor body. */
public static void call(String member, Class<?> stubClass) {
if ("off".equals(MODE) || "classes".equals(MODE)) {
return;
}
log("CALL " + stubClass.getName() + "." + member);
}
/**
* String-based overload used by the bytecode-generated stubs (they pass
* the dotted class name rather than a Class literal). Resolves the
* already-loaded class to recover its code source location.
*/
public static void loaded(String dotName) {
if ("off".equals(MODE)) {
return;
}
String from = "?";
try {
Class<?> c = Class.forName(dotName, false,
KotlinProxyLog.class.getClassLoader());
if (c.getProtectionDomain().getCodeSource() != null
&& c.getProtectionDomain().getCodeSource().getLocation() != null) {
from = c.getProtectionDomain().getCodeSource().getLocation().toString();
}
} catch (Throwable t) {
// ignore
}
log("LOAD " + dotName + " <- " + from);
}
public static void call(String member, String dotName) {
if ("off".equals(MODE) || "classes".equals(MODE)) {
return;
}
log("CALL " + dotName + "." + member);
}
/**
* 3-arg overload used by the bytecode stubs to log the boundary: the stub
* passes its first (reference) parameter - typically the compiler
* K2JVMCompilerArguments - so we can see exactly what AGP/KGP handed the
* (replaced) compiler.
*/
public static void call(String member, String dotName, Object arg) {
if ("off".equals(MODE) || "classes".equals(MODE)) {
return;
}
log("CALL " + dotName + "." + member + " arg=" + dump(arg));
}
/**
* Called by stub methods that declare a reference return type (other than
* String). Reflectively instantiates the real, un-stubbed class of that
* return type so the caller (the daemon) can keep going: e.g.
* K2JVMCompiler.createArguments() returns a genuine K2JVMCompilerArguments,
* which lets parseCommandLineArguments run and doExecute be reached with the
* fully-populated args (the boundary we want to log). Returns null when the
* type has no usable no-arg constructor (interfaces, enums, abstract, etc.).
*/
public static Object newInstance(String dotName) {
try {
Class<?> c = Class.forName(dotName, false,
KotlinProxyLog.class.getClassLoader());
return c.getDeclaredConstructor().newInstance();
} catch (Throwable t) {
log("newInstance FAIL " + dotName + " : " + t
+ " | loader=" + KotlinProxyLog.class.getClassLoader());
return null;
}
}
/** Reflectively summarize an arbitrary arg object for the log. */
private static String dump(Object o) {
if (o == null) {
return "null";
}
try {
// Compiler-argument types expose toCommonList(); show it verbatim.
try {
java.lang.reflect.Method m = o.getClass().getMethod("toCommonList");
Object list = m.invoke(o);
if (list != null) {
return list.toString();
}
} catch (Throwable ignore) {
}
// Otherwise dump the non-null declared fields across the hierarchy.
StringBuilder sb = new StringBuilder(o.getClass().getSimpleName()).append('{');
for (Class<?> c = o.getClass(); c != null && c != Object.class;
c = c.getSuperclass()) {
for (java.lang.reflect.Field f : c.getDeclaredFields()) {
if (java.lang.reflect.Modifier.isStatic(f.getModifiers())) {
continue;
}
try {
f.setAccessible(true);
Object v = f.get(o);
if (v == null) {
continue;
}
sb.append(f.getName()).append('=').append(v).append(' ');
} catch (Throwable ignore) {
}
}
}
return sb.append('}').toString();
} catch (Throwable t) {
try {
return o.toString();
} catch (Throwable t2) {
return "<unprintable>";
}
}
}
private static void log(String line) {
ensure();
BufferedWriter w = writer;
if (w != null) {
try {
w.write(Thread.currentThread().getName() + " | " + line);
w.newLine();
w.flush(); // survive JVM exit without a full buffer
} catch (IOException e) {
// logging must never break the build
}
}
// Fixed-path marker: works even when the daemon JVM has no
// -Dkotlin.proxy.log, so we can prove the stub was actually loaded
// in the out-of-process daemon regardless of its working directory.
try {
String marker = System.getProperty("kotlin.proxy.marker", "/tmp/kotlin-proxy-marker.txt");
synchronized (KotlinProxyLog.class) {
BufferedWriter mw = new BufferedWriter(new java.io.FileWriter(marker, true));
mw.write(Thread.currentThread().getName() + " | " + line + "\n");
mw.close();
}
} catch (Exception e) {
// ignore
}
}
}
|