author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 52698 | ca6b58b8ffc5 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.internal; |
|
27 |
||
28 |
import java.lang.reflect.Field; |
|
29 |
import java.lang.reflect.Modifier; |
|
30 |
import java.lang.reflect.Parameter; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.HashSet; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.function.Consumer; |
|
36 |
||
37 |
import jdk.internal.org.objectweb.asm.ClassReader; |
|
38 |
import jdk.internal.org.objectweb.asm.ClassWriter; |
|
39 |
import jdk.internal.org.objectweb.asm.Label; |
|
40 |
import jdk.internal.org.objectweb.asm.MethodVisitor; |
|
41 |
import jdk.internal.org.objectweb.asm.Opcodes; |
|
42 |
import jdk.internal.org.objectweb.asm.Type; |
|
43 |
import jdk.internal.org.objectweb.asm.commons.Method; |
|
44 |
import jdk.internal.org.objectweb.asm.tree.AnnotationNode; |
|
45 |
import jdk.internal.org.objectweb.asm.tree.ClassNode; |
|
46 |
import jdk.internal.org.objectweb.asm.tree.FieldNode; |
|
47 |
import jdk.internal.org.objectweb.asm.tree.MethodNode; |
|
48 |
import jdk.jfr.Enabled; |
|
49 |
import jdk.jfr.Event; |
|
50 |
import jdk.jfr.Name; |
|
51 |
import jdk.jfr.Registered; |
|
52 |
import jdk.jfr.SettingControl; |
|
53 |
import jdk.jfr.SettingDefinition; |
|
54 |
import jdk.jfr.internal.handlers.EventHandler; |
|
55 |
||
56 |
/** |
|
57 |
* Class responsible for adding instrumentation to a subclass of {@link Event}. |
|
58 |
* |
|
59 |
*/ |
|
60 |
public final class EventInstrumentation { |
|
61 |
static final class SettingInfo { |
|
62 |
private String methodName; |
|
63 |
private String internalSettingName; |
|
64 |
private String settingDescriptor; |
|
65 |
final String fieldName; |
|
66 |
final int index; |
|
67 |
// Used when instantiating Setting |
|
68 |
SettingControl settingControl; |
|
69 |
||
70 |
public SettingInfo(String fieldName, int index) { |
|
71 |
this.fieldName = fieldName; |
|
72 |
this.index = index; |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
static final class FieldInfo { |
|
77 |
private final static Type STRING = Type.getType(String.class); |
|
78 |
final String fieldName; |
|
79 |
final String fieldDescriptor; |
|
80 |
final String internalClassName; |
|
81 |
||
82 |
public FieldInfo(String fieldName, String fieldDescriptor, String internalClassName) { |
|
83 |
this.fieldName = fieldName; |
|
84 |
this.fieldDescriptor = fieldDescriptor; |
|
85 |
this.internalClassName = internalClassName; |
|
86 |
} |
|
87 |
||
88 |
public boolean isString() { |
|
89 |
return STRING.getDescriptor().equals(fieldDescriptor); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
public static final String FIELD_EVENT_THREAD = "eventThread"; |
|
94 |
public static final String FIELD_STACK_TRACE = "stackTrace"; |
|
95 |
public static final String FIELD_DURATION = "duration"; |
|
96 |
||
97 |
static final String FIELD_EVENT_HANDLER = "eventHandler"; |
|
98 |
static final String FIELD_START_TIME = "startTime"; |
|
99 |
||
100 |
private static final Type ANNOTATION_TYPE_NAME = Type.getType(Name.class); |
|
101 |
private static final Type ANNOTATION_TYPE_REGISTERED = Type.getType(Registered.class); |
|
102 |
private static final Type ANNOTATION_TYPE_ENABLED = Type.getType(Enabled.class); |
|
103 |
private static final Type TYPE_EVENT_HANDLER = Type.getType(EventHandler.class); |
|
104 |
private static final Type TYPE_SETTING_CONTROL = Type.getType(SettingControl.class); |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
105 |
private static final Type TYPE_OBJECT = Type.getType(Object.class); |
50113 | 106 |
private static final Method METHOD_COMMIT = new Method("commit", Type.VOID_TYPE, new Type[0]); |
107 |
private static final Method METHOD_BEGIN = new Method("begin", Type.VOID_TYPE, new Type[0]); |
|
108 |
private static final Method METHOD_END = new Method("end", Type.VOID_TYPE, new Type[0]); |
|
109 |
private static final Method METHOD_IS_ENABLED = new Method("isEnabled", Type.BOOLEAN_TYPE, new Type[0]); |
|
110 |
private static final Method METHOD_TIME_STAMP = new Method("timestamp", Type.LONG_TYPE, new Type[0]); |
|
111 |
private static final Method METHOD_EVENT_SHOULD_COMMIT = new Method("shouldCommit", Type.BOOLEAN_TYPE, new Type[0]); |
|
112 |
private static final Method METHOD_EVENT_HANDLER_SHOULD_COMMIT = new Method("shouldCommit", Type.BOOLEAN_TYPE, new Type[] { Type.LONG_TYPE }); |
|
113 |
private static final Method METHOD_DURATION = new Method("duration", Type.LONG_TYPE, new Type[] { Type.LONG_TYPE }); |
|
114 |
||
115 |
private final ClassNode classNode; |
|
116 |
private final List<SettingInfo> settingInfos; |
|
117 |
private final List<FieldInfo> fieldInfos;; |
|
118 |
private final Method writeMethod; |
|
119 |
private final String eventHandlerXInternalName; |
|
120 |
private final String eventName; |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
121 |
private final boolean untypedEventHandler; |
50113 | 122 |
private boolean guardHandlerReference; |
123 |
private Class<?> superClass; |
|
124 |
||
125 |
EventInstrumentation(Class<?> superClass, byte[] bytes, long id) { |
|
126 |
this.superClass = superClass; |
|
127 |
this.classNode = createClassNode(bytes); |
|
128 |
this.settingInfos = buildSettingInfos(superClass, classNode); |
|
129 |
this.fieldInfos = buildFieldInfos(superClass, classNode); |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
130 |
this.untypedEventHandler = hasUntypedHandler(); |
50113 | 131 |
this.writeMethod = makeWriteMethod(fieldInfos); |
132 |
this.eventHandlerXInternalName = ASMToolkit.getInternalName(EventHandlerCreator.makeEventHandlerName(id)); |
|
133 |
String n = annotationValue(classNode, ANNOTATION_TYPE_NAME.getDescriptor(), String.class); |
|
134 |
this.eventName = n == null ? classNode.name.replace("/", ".") : n; |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
135 |
} |
50113 | 136 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
137 |
private boolean hasUntypedHandler() { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
138 |
for (FieldNode field : classNode.fields) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
139 |
if (FIELD_EVENT_HANDLER.equals(field.name)) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
140 |
return field.desc.equals(TYPE_OBJECT.getDescriptor()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
141 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
142 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
143 |
throw new InternalError("Class missing handler field"); |
50113 | 144 |
} |
145 |
||
146 |
public String getClassName() { |
|
147 |
return classNode.name.replace("/","."); |
|
148 |
} |
|
149 |
||
150 |
private ClassNode createClassNode(byte[] bytes) { |
|
151 |
ClassNode classNode = new ClassNode(); |
|
152 |
ClassReader classReader = new ClassReader(bytes); |
|
153 |
classReader.accept(classNode, 0); |
|
154 |
return classNode; |
|
155 |
} |
|
156 |
||
157 |
boolean isRegistered() { |
|
158 |
Boolean result = annotationValue(classNode, ANNOTATION_TYPE_REGISTERED.getDescriptor(), Boolean.class); |
|
159 |
if (result != null) { |
|
160 |
return result.booleanValue(); |
|
161 |
} |
|
162 |
if (superClass != null) { |
|
163 |
Registered r = superClass.getAnnotation(Registered.class); |
|
164 |
if (r != null) { |
|
165 |
return r.value(); |
|
166 |
} |
|
167 |
} |
|
168 |
return true; |
|
169 |
} |
|
170 |
||
171 |
boolean isEnabled() { |
|
172 |
Boolean result = annotationValue(classNode, ANNOTATION_TYPE_ENABLED.getDescriptor(), Boolean.class); |
|
173 |
if (result != null) { |
|
174 |
return result.booleanValue(); |
|
175 |
} |
|
176 |
if (superClass != null) { |
|
177 |
Enabled e = superClass.getAnnotation(Enabled.class); |
|
178 |
if (e != null) { |
|
179 |
return e.value(); |
|
180 |
} |
|
181 |
} |
|
182 |
return true; |
|
183 |
} |
|
184 |
||
185 |
@SuppressWarnings("unchecked") |
|
186 |
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) { |
|
187 |
if (classNode.visibleAnnotations != null) { |
|
188 |
for (AnnotationNode a : classNode.visibleAnnotations) { |
|
189 |
if (typeDescriptor.equals(a.desc)) { |
|
190 |
List<Object> values = a.values; |
|
191 |
if (values != null && values.size() == 2) { |
|
192 |
Object key = values.get(0); |
|
193 |
Object value = values.get(1); |
|
194 |
if (key instanceof String && value != null) { |
|
195 |
if (type == value.getClass()) { |
|
196 |
String keyName = (String) key; |
|
197 |
if ("value".equals(keyName)) { |
|
198 |
return (T) value; |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
return null; |
|
207 |
} |
|
208 |
||
209 |
private static List<SettingInfo> buildSettingInfos(Class<?> superClass, ClassNode classNode) { |
|
210 |
Set<String> methodSet = new HashSet<>(); |
|
211 |
List<SettingInfo> settingInfos = new ArrayList<>(); |
|
212 |
String settingDescriptor = Type.getType(SettingDefinition.class).getDescriptor(); |
|
213 |
for (MethodNode m : classNode.methods) { |
|
214 |
if (m.visibleAnnotations != null) { |
|
215 |
for (AnnotationNode an : m.visibleAnnotations) { |
|
216 |
// We can't really validate the method at this |
|
217 |
// stage. We would need to check that the parameter |
|
218 |
// is an instance of SettingControl. |
|
219 |
if (settingDescriptor.equals(an.desc)) { |
|
220 |
Type returnType = Type.getReturnType(m.desc); |
|
221 |
if (returnType.equals(Type.getType(Boolean.TYPE))) { |
|
222 |
Type[] args = Type.getArgumentTypes(m.desc); |
|
223 |
if (args.length == 1) { |
|
224 |
Type paramType = args[0]; |
|
225 |
String fieldName = EventControl.FIELD_SETTING_PREFIX + settingInfos.size(); |
|
226 |
int index = settingInfos.size(); |
|
227 |
SettingInfo si = new SettingInfo(fieldName, index); |
|
228 |
si.methodName = m.name; |
|
229 |
si.settingDescriptor = paramType.getDescriptor(); |
|
230 |
si.internalSettingName = paramType.getInternalName(); |
|
231 |
methodSet.add(m.name); |
|
232 |
settingInfos.add(si); |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
239 |
for (Class<?> c = superClass; c != jdk.internal.event.Event.class; c = c.getSuperclass()) { |
50113 | 240 |
for (java.lang.reflect.Method method : c.getDeclaredMethods()) { |
241 |
if (!methodSet.contains(method.getName())) { |
|
242 |
// skip private method in base classes |
|
243 |
if (!Modifier.isPrivate(method.getModifiers())) { |
|
244 |
if (method.getReturnType().equals(Boolean.TYPE)) { |
|
245 |
if (method.getParameterCount() == 1) { |
|
246 |
Parameter param = method.getParameters()[0]; |
|
247 |
Type paramType = Type.getType(param.getType()); |
|
248 |
String fieldName = EventControl.FIELD_SETTING_PREFIX + settingInfos.size(); |
|
249 |
int index = settingInfos.size(); |
|
250 |
SettingInfo si = new SettingInfo(fieldName, index); |
|
251 |
si.methodName = method.getName(); |
|
252 |
si.settingDescriptor = paramType.getDescriptor(); |
|
253 |
si.internalSettingName = paramType.getInternalName(); |
|
254 |
methodSet.add(method.getName()); |
|
255 |
settingInfos.add(si); |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
return settingInfos; |
|
263 |
} |
|
264 |
||
265 |
private static List<FieldInfo> buildFieldInfos(Class<?> superClass, ClassNode classNode) { |
|
266 |
Set<String> fieldSet = new HashSet<>(); |
|
267 |
List<FieldInfo> fieldInfos = new ArrayList<>(classNode.fields.size()); |
|
268 |
// These two field are added by native as transient so they will be |
|
269 |
// ignored by the loop below. |
|
270 |
// The benefit of adding them manually is that we can |
|
271 |
// control in which order they occur and we can add @Name, @Description |
|
272 |
// in Java, instead of in native. It also means code for adding implicit |
|
273 |
// fields for native can be reused by Java. |
|
274 |
fieldInfos.add(new FieldInfo("startTime", Type.LONG_TYPE.getDescriptor(), classNode.name)); |
|
275 |
fieldInfos.add(new FieldInfo("duration", Type.LONG_TYPE.getDescriptor(), classNode.name)); |
|
276 |
for (FieldNode field : classNode.fields) { |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
277 |
if (!fieldSet.contains(field.name) && isValidField(field.access, Type.getType(field.desc).getClassName())) { |
50113 | 278 |
FieldInfo fi = new FieldInfo(field.name, field.desc, classNode.name); |
279 |
fieldInfos.add(fi); |
|
280 |
fieldSet.add(field.name); |
|
281 |
} |
|
282 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
283 |
for (Class<?> c = superClass; c != jdk.internal.event.Event.class; c = c.getSuperclass()) { |
50113 | 284 |
for (Field field : c.getDeclaredFields()) { |
285 |
// skip private field in base classes |
|
286 |
if (!Modifier.isPrivate(field.getModifiers())) { |
|
287 |
if (isValidField(field.getModifiers(), field.getType().getName())) { |
|
288 |
String fieldName = field.getName(); |
|
289 |
if (!fieldSet.contains(fieldName)) { |
|
290 |
Type fieldType = Type.getType(field.getType()); |
|
291 |
String internalClassName = ASMToolkit.getInternalName(c.getName()); |
|
292 |
fieldInfos.add(new FieldInfo(fieldName, fieldType.getDescriptor(), internalClassName)); |
|
293 |
fieldSet.add(fieldName); |
|
294 |
} |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
} |
|
299 |
return fieldInfos; |
|
300 |
} |
|
301 |
||
302 |
public static boolean isValidField(int access, String className) { |
|
303 |
if (Modifier.isTransient(access) || Modifier.isStatic(access)) { |
|
304 |
return false; |
|
305 |
} |
|
306 |
return jdk.jfr.internal.Type.isValidJavaFieldType(className); |
|
307 |
} |
|
308 |
||
309 |
public byte[] buildInstrumented() { |
|
310 |
makeInstrumented(); |
|
311 |
return toByteArray(); |
|
312 |
} |
|
313 |
||
314 |
private byte[] toByteArray() { |
|
315 |
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); |
|
316 |
classNode.accept(cw); |
|
317 |
cw.visitEnd(); |
|
318 |
byte[] result = cw.toByteArray(); |
|
319 |
Utils.writeGeneratedASM(classNode.name, result); |
|
320 |
return result; |
|
321 |
} |
|
322 |
||
323 |
public byte[] builUninstrumented() { |
|
324 |
makeUninstrumented(); |
|
325 |
return toByteArray(); |
|
326 |
} |
|
327 |
||
328 |
private void makeInstrumented() { |
|
329 |
// MyEvent#isEnabled() |
|
330 |
updateMethod(METHOD_IS_ENABLED, methodVisitor -> { |
|
331 |
Label nullLabel = new Label(); |
|
332 |
if (guardHandlerReference) { |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
333 |
getEventHandler(methodVisitor); |
50113 | 334 |
methodVisitor.visitJumpInsn(Opcodes.IFNULL, nullLabel); |
335 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
336 |
getEventHandler(methodVisitor); |
50113 | 337 |
ASMToolkit.invokeVirtual(methodVisitor, TYPE_EVENT_HANDLER.getInternalName(), METHOD_IS_ENABLED); |
338 |
methodVisitor.visitInsn(Opcodes.IRETURN); |
|
339 |
if (guardHandlerReference) { |
|
340 |
methodVisitor.visitLabel(nullLabel); |
|
341 |
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); |
|
342 |
methodVisitor.visitInsn(Opcodes.ICONST_0); |
|
343 |
methodVisitor.visitInsn(Opcodes.IRETURN); |
|
344 |
} |
|
345 |
}); |
|
346 |
||
347 |
// MyEvent#begin() |
|
348 |
updateMethod(METHOD_BEGIN, methodVisitor -> { |
|
349 |
methodVisitor.visitIntInsn(Opcodes.ALOAD, 0); |
|
350 |
ASMToolkit.invokeStatic(methodVisitor, TYPE_EVENT_HANDLER.getInternalName(), METHOD_TIME_STAMP); |
|
351 |
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, getInternalClassName(), FIELD_START_TIME, "J"); |
|
352 |
methodVisitor.visitInsn(Opcodes.RETURN); |
|
353 |
}); |
|
354 |
||
355 |
// MyEvent#end() |
|
356 |
updateMethod(METHOD_END, methodVisitor -> { |
|
357 |
methodVisitor.visitIntInsn(Opcodes.ALOAD, 0); |
|
358 |
methodVisitor.visitIntInsn(Opcodes.ALOAD, 0); |
|
359 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_START_TIME, "J"); |
|
360 |
ASMToolkit.invokeStatic(methodVisitor, TYPE_EVENT_HANDLER.getInternalName(), METHOD_DURATION); |
|
361 |
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, getInternalClassName(), FIELD_DURATION, "J"); |
|
362 |
methodVisitor.visitInsn(Opcodes.RETURN); |
|
363 |
methodVisitor.visitMaxs(0, 0); |
|
364 |
}); |
|
365 |
||
366 |
updateMethod(METHOD_COMMIT, methodVisitor -> { |
|
58863 | 367 |
// if (!isEnable()) { |
368 |
// return; |
|
369 |
// } |
|
370 |
methodVisitor.visitCode(); |
|
371 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
372 |
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getInternalClassName(), METHOD_IS_ENABLED.getName(), METHOD_IS_ENABLED.getDescriptor(), false); |
|
373 |
Label l0 = new Label(); |
|
374 |
methodVisitor.visitJumpInsn(Opcodes.IFNE, l0); |
|
375 |
methodVisitor.visitInsn(Opcodes.RETURN); |
|
376 |
methodVisitor.visitLabel(l0); |
|
377 |
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); |
|
378 |
// if (startTime == 0) { |
|
379 |
// startTime = EventWriter.timestamp(); |
|
380 |
// } else { |
|
381 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
382 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_START_TIME, "J"); |
|
383 |
methodVisitor.visitInsn(Opcodes.LCONST_0); |
|
384 |
methodVisitor.visitInsn(Opcodes.LCMP); |
|
385 |
Label durationalEvent = new Label(); |
|
386 |
methodVisitor.visitJumpInsn(Opcodes.IFNE, durationalEvent); |
|
387 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
388 |
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, TYPE_EVENT_HANDLER.getInternalName(), METHOD_TIME_STAMP.getName(), METHOD_TIME_STAMP.getDescriptor(), false); |
|
389 |
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, getInternalClassName(), FIELD_START_TIME, "J"); |
|
390 |
Label commit = new Label(); |
|
391 |
methodVisitor.visitJumpInsn(Opcodes.GOTO, commit); |
|
392 |
// if (duration == 0) { |
|
393 |
// duration = EventWriter.timestamp() - startTime; |
|
394 |
// } |
|
395 |
// } |
|
396 |
methodVisitor.visitLabel(durationalEvent); |
|
397 |
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); |
|
398 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
399 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_DURATION, "J"); |
|
400 |
methodVisitor.visitInsn(Opcodes.LCONST_0); |
|
401 |
methodVisitor.visitInsn(Opcodes.LCMP); |
|
402 |
methodVisitor.visitJumpInsn(Opcodes.IFNE, commit); |
|
403 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
404 |
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, TYPE_EVENT_HANDLER.getInternalName(), METHOD_TIME_STAMP.getName(), METHOD_TIME_STAMP.getDescriptor(), false); |
|
405 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
406 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_START_TIME, "J"); |
|
407 |
methodVisitor.visitInsn(Opcodes.LSUB); |
|
408 |
methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, getInternalClassName(), FIELD_DURATION, "J"); |
|
409 |
methodVisitor.visitLabel(commit); |
|
410 |
// if (shouldCommit()) { |
|
411 |
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); |
|
412 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
|
413 |
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getInternalClassName(), METHOD_EVENT_SHOULD_COMMIT.getName(), METHOD_EVENT_SHOULD_COMMIT.getDescriptor(), false); |
|
414 |
Label end = new Label(); |
|
415 |
// eventHandler.write(...); |
|
416 |
// } |
|
417 |
methodVisitor.visitJumpInsn(Opcodes.IFEQ, end); |
|
418 |
getEventHandler(methodVisitor); |
|
419 |
||
420 |
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, eventHandlerXInternalName); |
|
421 |
for (FieldInfo fi : fieldInfos) { |
|
50113 | 422 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
58863 | 423 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, fi.internalClassName, fi.fieldName, fi.fieldDescriptor); |
424 |
} |
|
50113 | 425 |
|
58863 | 426 |
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, eventHandlerXInternalName, writeMethod.getName(), writeMethod.getDescriptor(), false); |
427 |
methodVisitor.visitLabel(end); |
|
428 |
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null); |
|
429 |
methodVisitor.visitInsn(Opcodes.RETURN); |
|
430 |
methodVisitor.visitEnd(); |
|
431 |
}); |
|
50113 | 432 |
|
433 |
// MyEvent#shouldCommit() |
|
434 |
updateMethod(METHOD_EVENT_SHOULD_COMMIT, methodVisitor -> { |
|
435 |
Label fail = new Label(); |
|
52698
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
436 |
if (guardHandlerReference) { |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
437 |
getEventHandler(methodVisitor); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
438 |
methodVisitor.visitJumpInsn(Opcodes.IFNULL, fail); |
ca6b58b8ffc5
8214287: SpecJbb2005StressModule got uncaught exception
mgronlun
parents:
52334
diff
changeset
|
439 |
} |
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
440 |
// if (!eventHandler.shouldCommit(duration) goto fail; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
441 |
getEventHandler(methodVisitor); |
50113 | 442 |
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); |
443 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_DURATION, "J"); |
|
444 |
ASMToolkit.invokeVirtual(methodVisitor, TYPE_EVENT_HANDLER.getInternalName(), METHOD_EVENT_HANDLER_SHOULD_COMMIT); |
|
445 |
methodVisitor.visitJumpInsn(Opcodes.IFEQ, fail); |
|
446 |
for (SettingInfo si : settingInfos) { |
|
447 |
// if (!settingsMethod(eventHandler.settingX)) goto fail; |
|
448 |
methodVisitor.visitIntInsn(Opcodes.ALOAD, 0); |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
449 |
if (untypedEventHandler) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
450 |
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, getInternalClassName(), FIELD_EVENT_HANDLER, TYPE_OBJECT.getDescriptor()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
451 |
} else { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
452 |
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, getInternalClassName(), FIELD_EVENT_HANDLER, Type.getDescriptor(EventHandler.class)); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
453 |
} |
50113 | 454 |
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, eventHandlerXInternalName); |
455 |
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, eventHandlerXInternalName, si.fieldName, TYPE_SETTING_CONTROL.getDescriptor()); |
|
456 |
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, si.internalSettingName); |
|
457 |
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getInternalClassName(), si.methodName, "(" + si.settingDescriptor + ")Z", false); |
|
458 |
methodVisitor.visitJumpInsn(Opcodes.IFEQ, fail); |
|
459 |
} |
|
460 |
// return true |
|
461 |
methodVisitor.visitInsn(Opcodes.ICONST_1); |
|
462 |
methodVisitor.visitInsn(Opcodes.IRETURN); |
|
463 |
// return false |
|
464 |
methodVisitor.visitLabel(fail); |
|
465 |
methodVisitor.visitInsn(Opcodes.ICONST_0); |
|
466 |
methodVisitor.visitInsn(Opcodes.IRETURN); |
|
467 |
}); |
|
468 |
} |
|
469 |
||
58863 | 470 |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
471 |
private void getEventHandler(MethodVisitor methodVisitor) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
472 |
if (untypedEventHandler) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
473 |
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, getInternalClassName(), FIELD_EVENT_HANDLER, TYPE_OBJECT.getDescriptor()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
474 |
methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, TYPE_EVENT_HANDLER.getInternalName()); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
475 |
} else { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
476 |
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, getInternalClassName(), FIELD_EVENT_HANDLER, Type.getDescriptor(EventHandler.class)); |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
477 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
478 |
} |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
479 |
|
50113 | 480 |
private void makeUninstrumented() { |
481 |
updateExistingWithReturnFalse(METHOD_EVENT_SHOULD_COMMIT); |
|
482 |
updateExistingWithReturnFalse(METHOD_IS_ENABLED); |
|
483 |
updateExistingWithEmptyVoidMethod(METHOD_COMMIT); |
|
484 |
updateExistingWithEmptyVoidMethod(METHOD_BEGIN); |
|
485 |
updateExistingWithEmptyVoidMethod(METHOD_END); |
|
486 |
} |
|
487 |
||
488 |
private final void updateExistingWithEmptyVoidMethod(Method voidMethod) { |
|
489 |
updateMethod(voidMethod, methodVisitor -> { |
|
490 |
methodVisitor.visitInsn(Opcodes.RETURN); |
|
491 |
}); |
|
492 |
} |
|
493 |
||
494 |
private final void updateExistingWithReturnFalse(Method voidMethod) { |
|
495 |
updateMethod(voidMethod, methodVisitor -> { |
|
496 |
methodVisitor.visitInsn(Opcodes.ICONST_0); |
|
497 |
methodVisitor.visitInsn(Opcodes.IRETURN); |
|
498 |
}); |
|
499 |
} |
|
500 |
||
501 |
private MethodNode getMethodNode(Method method) { |
|
502 |
for (MethodNode m : classNode.methods) { |
|
503 |
if (m.name.equals(method.getName()) && m.desc.equals(method.getDescriptor())) { |
|
504 |
return m; |
|
505 |
} |
|
506 |
} |
|
507 |
return null; |
|
508 |
} |
|
509 |
||
510 |
private final void updateMethod(Method method, Consumer<MethodVisitor> code) { |
|
511 |
MethodNode old = getMethodNode(method); |
|
512 |
int index = classNode.methods.indexOf(old); |
|
513 |
classNode.methods.remove(old); |
|
514 |
MethodVisitor mv = classNode.visitMethod(old.access, old.name, old.desc, null, null); |
|
515 |
mv.visitCode(); |
|
516 |
code.accept(mv); |
|
517 |
mv.visitMaxs(0, 0); |
|
518 |
MethodNode newMethod = getMethodNode(method); |
|
519 |
classNode.methods.remove(newMethod); |
|
520 |
classNode.methods.add(index, newMethod); |
|
521 |
} |
|
522 |
||
523 |
public static Method makeWriteMethod(List<FieldInfo> fields) { |
|
524 |
StringBuilder sb = new StringBuilder(); |
|
525 |
sb.append("("); |
|
526 |
for (FieldInfo v : fields) { |
|
527 |
sb.append(v.fieldDescriptor); |
|
528 |
} |
|
529 |
sb.append(")V"); |
|
530 |
return new Method("write", sb.toString()); |
|
531 |
} |
|
532 |
||
533 |
private String getInternalClassName() { |
|
534 |
return classNode.name; |
|
535 |
} |
|
536 |
||
537 |
public List<SettingInfo> getSettingInfos() { |
|
538 |
return settingInfos; |
|
539 |
} |
|
540 |
||
541 |
public List<FieldInfo> getFieldInfos() { |
|
542 |
return fieldInfos; |
|
543 |
} |
|
544 |
||
545 |
public String getEventName() { |
|
546 |
return eventName; |
|
547 |
} |
|
548 |
||
549 |
public void setGuardHandler(boolean guardHandlerReference) { |
|
550 |
this.guardHandlerReference = guardHandlerReference; |
|
551 |
} |
|
552 |
} |