package jdk.jfr.internal;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.org.objectweb.asm.Type;
import jdk.internal.org.objectweb.asm.commons.Method;
import jdk.jfr.Event;
import jdk.jfr.EventType;
import jdk.jfr.SettingControl;
import jdk.jfr.ValueDescriptor;
import jdk.jfr.internal.EventInstrumentation.FieldInfo;
import jdk.jfr.internal.EventInstrumentation.SettingInfo;
import jdk.jfr.internal.handlers.EventHandler;
final class EventHandlerCreator {
private static final int CLASS_VERSION = 52;
private static final String SUFFIX = "_" + System.currentTimeMillis() + "-" + JVM.getJVM().getPid();
private static final String FIELD_EVENT_TYPE = "platformEventType";
private static final String FIELD_PREFIX_STRING_POOL = "stringPool";
private final static Type TYPE_STRING_POOL = Type.getType(StringPool.class);
private final static Type TYPE_EVENT_WRITER = Type.getType(EventWriter.class);
private final static Type TYPE_PLATFORM_EVENT_TYPE = Type.getType(PlatformEventType.class);
private final static Type TYPE_EVENT_HANDLER = Type.getType(EventHandler.class);
private final static Type TYPE_SETTING_CONTROL = Type.getType(SettingControl.class);
private final static Type TYPE_EVENT_TYPE = Type.getType(EventType.class);
private final static Type TYPE_EVENT_CONTROL = Type.getType(EventControl.class);
private final static String DESCRIPTOR_EVENT_HANDLER = "(" + Type.BOOLEAN_TYPE.getDescriptor() + TYPE_EVENT_TYPE.getDescriptor() + TYPE_EVENT_CONTROL.getDescriptor() + ")V";
private final static Method METHOD_GET_EVENT_WRITER = new Method("getEventWriter", "()" + TYPE_EVENT_WRITER.getDescriptor());
private final static Method METHOD_EVENT_HANDLER_CONSTRUCTOR = new Method("<init>", DESCRIPTOR_EVENT_HANDLER);
private final static Method METHOD_RESET = new Method("reset", "()V");
private final ClassWriter classWriter;
private final String className;
private final String internalClassName;
private final List<SettingInfo> settingInfos;
private final List<FieldInfo> fields;
public EventHandlerCreator(long id, List<SettingInfo> settingInfos, List<FieldInfo> fields) {
this.classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
this.className = makeEventHandlerName(id);
this.internalClassName = ASMToolkit.getInternalName(className);
this.settingInfos = settingInfos;
this.fields = fields;
}
public static String makeEventHandlerName(long id) {
return EventHandler.class.getName() + id + SUFFIX;
}
public EventHandlerCreator(long id, List<SettingInfo> settingInfos, EventType type, Class<? extends jdk.internal.event.Event> eventClass) {
this(id, settingInfos, createFieldInfos(eventClass, type));
}
private static List<FieldInfo> createFieldInfos(Class<? extends jdk.internal.event.Event> eventClass, EventType type) throws Error {
List<FieldInfo> fieldInfos = new ArrayList<>();
for (ValueDescriptor v : type.getFields()) {
if (v != TypeLibrary.STACK_TRACE_FIELD && v != TypeLibrary.THREAD_FIELD) {
String fieldName = PrivateAccess.getInstance().getFieldName(v);
String fieldDescriptor = ASMToolkit.getDescriptor(v.getTypeName());
Class<?> c = eventClass;
String internalName = null;
while (c != Event.class) {
try {
Field field = c.getDeclaredField(fieldName);
if (c == eventClass || !Modifier.isPrivate(field.getModifiers())) {
internalName = ASMToolkit.getInternalName(c.getName());
break;
}
} catch (NoSuchFieldException | SecurityException e) {
}
c = c.getSuperclass();
}
if (internalName != null) {
fieldInfos.add(new FieldInfo(fieldName, fieldDescriptor, internalName));
} else {
throw new InternalError("Could not locate field " + fieldName + " for event type" + type.getName());
}
}
}
return fieldInfos;
}
public Class<? extends EventHandler> makeEventHandlerClass() {
buildClassInfo();
buildConstructor();
buildWriteMethod();
byte[] bytes = classWriter.toByteArray();
ASMToolkit.logASM(className, bytes);
return SecuritySupport.defineClass(EventHandler.class, bytes).asSubclass(EventHandler.class);
}
public static EventHandler instantiateEventHandler(Class<? extends EventHandler> handlerClass, boolean registered, EventType eventType, EventControl eventControl) throws Error {
final Constructor<?> cc;
try {
cc = handlerClass.getDeclaredConstructors()[0];
} catch (Exception e) {
throw (Error) new InternalError("Could not get handler constructor for " + eventType.getName()).initCause(e);
}
SecuritySupport.setAccessible(cc);
try {
List<SettingInfo> settingInfos = eventControl.getSettingInfos();
Object[] arguments = new Object[3 + settingInfos.size()];
arguments[0] = registered;
arguments[1] = eventType;
arguments[2] = eventControl;
for (SettingInfo si : settingInfos) {
arguments[si.index + 3] = si.settingControl;
}
return (EventHandler) cc.newInstance(arguments);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw (Error) new InternalError("Could not instantiate event handler for " + eventType.getName() + ". " + e.getMessage()).initCause(e);
}
}
private void buildConstructor() {
MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PRIVATE, METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), makeConstructorDescriptor(settingInfos), null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ILOAD, 1);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 3);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(EventHandler.class), METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), METHOD_EVENT_HANDLER_CONSTRUCTOR.getDescriptor(), false);
for (SettingInfo si : settingInfos) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, si.index + 4);
mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, si.fieldName, TYPE_SETTING_CONTROL.getDescriptor());
}
int fieldIndex = 0;
for (FieldInfo field : fields) {
if (field.isString()) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(EventHandler.class), "createStringFieldWriter", "()" + TYPE_STRING_POOL.getDescriptor(), false);
mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, FIELD_PREFIX_STRING_POOL + fieldIndex, TYPE_STRING_POOL.getDescriptor());
}
fieldIndex++;
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
private void buildClassInfo() {
String internalSuperName = ASMToolkit.getInternalName(EventHandler.class.getName());
classWriter.visit(CLASS_VERSION, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, internalClassName, null, internalSuperName, null);
for (SettingInfo si : settingInfos) {
classWriter.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, si.fieldName, TYPE_SETTING_CONTROL.getDescriptor(), null, null);
}
int fieldIndex = 0;
for (FieldInfo field : fields) {
if (field.isString()) {
classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, FIELD_PREFIX_STRING_POOL+ fieldIndex, TYPE_STRING_POOL.getDescriptor(), null, null);
}
fieldIndex++;
}
}
private void visitMethod(final MethodVisitor mv, final int opcode, final Type type, final Method method) {
mv.visitMethodInsn(opcode, type.getInternalName(), method.getName(), method.getDescriptor(), false);
}
private void buildWriteMethod() {
int argIndex = 0;
int slotIndex = 1;
int fieldIndex = 0;
Method desc = ASMToolkit.makeWriteMethod(fields);
Type[] argumentTypes = Type.getArgumentTypes(desc.getDescriptor());
MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PUBLIC, desc.getName(), desc.getDescriptor(), null, null);
mv.visitCode();
Label start = new Label();
Label endTryBlock = new Label();
Label exceptionHandler = new Label();
mv.visitTryCatchBlock(start, endTryBlock, exceptionHandler, "java/lang/Throwable");
mv.visitLabel(start);
visitMethod(mv, Opcodes.INVOKESTATIC, TYPE_EVENT_WRITER, METHOD_GET_EVENT_WRITER);
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, TYPE_EVENT_HANDLER.getInternalName(), FIELD_EVENT_TYPE, TYPE_PLATFORM_EVENT_TYPE.getDescriptor());
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.BEGIN_EVENT.asASM());
Label recursive = new Label();
mv.visitJumpInsn(Opcodes.IFEQ, recursive);
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(argumentTypes[argIndex].getOpcode(Opcodes.ILOAD), slotIndex);
slotIndex += argumentTypes[argIndex++].getSize();
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.PUT_LONG.asASM());
fieldIndex++;
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(argumentTypes[argIndex].getOpcode(Opcodes.ILOAD), slotIndex);
slotIndex += argumentTypes[argIndex++].getSize();
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.PUT_LONG.asASM());
fieldIndex++;
mv.visitInsn(Opcodes.DUP);
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.PUT_EVENT_THREAD.asASM());
mv.visitInsn(Opcodes.DUP);
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.PUT_STACK_TRACE.asASM());
while (fieldIndex < fields.size()) {
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(argumentTypes[argIndex].getOpcode(Opcodes.ILOAD), slotIndex);
slotIndex += argumentTypes[argIndex++].getSize();
FieldInfo field = fields.get(fieldIndex);
if (field.isString()) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, this.internalClassName, FIELD_PREFIX_STRING_POOL + fieldIndex, TYPE_STRING_POOL.getDescriptor());
}
EventWriterMethod eventMethod = EventWriterMethod.lookupMethod(field);
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, eventMethod.asASM());
fieldIndex++;
}
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, EventWriterMethod.END_EVENT.asASM());
mv.visitJumpInsn(Opcodes.IFEQ, start);
mv.visitLabel(endTryBlock);
Label end = new Label();
mv.visitJumpInsn(Opcodes.GOTO, end);
mv.visitLabel(exceptionHandler);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"});
visitMethod(mv, Opcodes.INVOKESTATIC, TYPE_EVENT_WRITER, METHOD_GET_EVENT_WRITER);
mv.visitInsn(Opcodes.DUP);
Label rethrow = new Label();
mv.visitJumpInsn(Opcodes.IFNULL, rethrow);
mv.visitInsn(Opcodes.DUP);
visitMethod(mv, Opcodes.INVOKEVIRTUAL, TYPE_EVENT_WRITER, METHOD_RESET);
mv.visitLabel(rethrow);
mv.visitFrame(Opcodes.F_SAME, 0, null, 2, new Object[] {"java/lang/Throwable", TYPE_EVENT_WRITER.getInternalName()});
mv.visitInsn(Opcodes.POP);
mv.visitInsn(Opcodes.ATHROW);
mv.visitLabel(recursive);
mv.visitFrame(Opcodes.F_SAME, 0, null, 1, new Object[] { TYPE_EVENT_WRITER.getInternalName()} );
mv.visitInsn(Opcodes.POP);
mv.visitLabel(end);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
private static String makeConstructorDescriptor(List<SettingInfo> settingsInfos) {
StringJoiner constructordescriptor = new StringJoiner("", "(", ")V");
constructordescriptor.add(Type.BOOLEAN_TYPE.getDescriptor());
constructordescriptor.add(Type.getType(EventType.class).getDescriptor());
constructordescriptor.add(Type.getType(EventControl.class).getDescriptor());
for (int i = 0; i < settingsInfos.size(); i++) {
constructordescriptor.add(TYPE_SETTING_CONTROL.getDescriptor());
}
return constructordescriptor.toString();
}
}