diff --git a/src/main/java/org/spongepowered/asm/mixin/Debug.java b/src/main/java/org/spongepowered/asm/mixin/Debug.java new file mode 100644 index 00000000..cfea8ddd --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/Debug.java @@ -0,0 +1,64 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.mixin; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Anotation used to decorate items you might wish to examine after mixin + * application. + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface Debug { + + /** + * Only applicable for classes, use this to decorate mixins that you wish + * to export even if mixin.debug.export is not enabled. This is + * useful if you wish to export only the target of the mixin you are working + * on without enabling export globally. + * + *

Note that if mixin.debug.export is not true + * then the decompiler is not initialised (even if present on the + * classpath) and thus you must set mixin.debug.export.decompile to + * true in order to have force-exported classes decompiled.

+ * + * @return whether to export the decorated target class + */ + public boolean export() default false; + + /** + * Print the method bytecode to the console after mixin application. This + * setting is only used if the mixin.debug.verbose option is + * enabled. + * + * @return whether to print the class or method to the console. + */ + public boolean print() default false; + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java index 78b18a23..0eb44d32 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java @@ -232,6 +232,8 @@ public class MixinApplicator { throw new InvalidMixinException(current, "Unexpecteded " + ex.getClass().getSimpleName() + " whilst applying the mixin class: " + ex.getMessage(), ex); } + + this.context.processDebugTasks(); } /** @@ -875,7 +877,7 @@ public class MixinApplicator { } for (AnnotationNode annotation : from) { - if (annotation.desc.startsWith("L" + Constants.MIXIN_PACKAGE_REF)) { + if (!this.isMergeableAnnotation(annotation)) { continue; } @@ -895,6 +897,13 @@ public class MixinApplicator { return to; } + private boolean isMergeableAnnotation(AnnotationNode annotation) { + if (annotation.desc.startsWith("L" + Constants.MIXIN_PACKAGE_REF)) { + return annotation.desc.endsWith("Debug;"); + } + return true; + } + /** * Find the first insn node with a matching opcode in the specified method * diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index bf61409d..64586be2 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -26,7 +26,6 @@ package org.spongepowered.asm.mixin.transformer; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; @@ -113,12 +112,6 @@ public class MixinTargetContext implements IReferenceMapperContext { */ private final List mergedMethods = new ArrayList(); - /** - * Information about methods in the target class, used to keep track of - * transformations we apply - */ - private final Map targetMethods = new HashMap(); - /** * Injector groups */ @@ -755,17 +748,7 @@ public class MixinTargetContext implements IReferenceMapperContext { * @return new or existing target handle for the supplied method */ public Target getTargetMethod(MethodNode method) { - if (!this.targetClass.getMethods().contains(method)) { - throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()"); - } - - String targetName = method.name + method.desc; - Target target = this.targetMethods.get(targetName); - if (target == null) { - target = new Target(this.targetClass.getClassNode(), method); - this.targetMethods.put(targetName, target); - } - return target; + return this.targetClass.getTargetMethod(method); } /** diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index c6f35631..2f5c63f8 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -699,7 +699,7 @@ public class MixinTransformer extends TreeTransformer { } } - return this.writeClass(transformedName, passThroughClass); + return this.writeClass(transformedName, passThroughClass, false); } /** @@ -717,7 +717,7 @@ public class MixinTransformer extends TreeTransformer { } catch (ValidationFailedException ex) { this.logger.info(ex.getMessage()); // If verify is enabled and failed, write out the bytecode to allow us to inspect it - if (MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) { + if (context.isExportForced() || MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_EXPORT)) { this.writeClass(context); } } @@ -840,18 +840,18 @@ public class MixinTransformer extends TreeTransformer { } private byte[] writeClass(TargetClassContext context) { - return this.writeClass(context.getClassName(), context.getClassNode()); + return this.writeClass(context.getClassName(), context.getClassNode(), context.isExportForced()); } - private byte[] writeClass(String transformedName, ClassNode targetClass) { + private byte[] writeClass(String transformedName, ClassNode targetClass, boolean forceExport) { // Collapse tree to bytes byte[] bytes = this.writeClass(targetClass); // Export transformed class for debugging purposes MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment(); - if (environment.getOption(Option.DEBUG_EXPORT)) { + if (forceExport || environment.getOption(Option.DEBUG_EXPORT)) { String filter = environment.getOptionValue(Option.DEBUG_EXPORT_FILTER); - if (filter == null || this.applyFilter(filter, transformedName)) { + if (forceExport || filter == null || this.applyFilter(filter, transformedName)) { File outputFile = this.dumpClass(transformedName.replace('.', '/'), bytes); if (this.decompiler != null) { this.decompiler.decompile(outputFile); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java index eb8757d4..48f6f8b9 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/TargetClassContext.java @@ -24,18 +24,21 @@ */ package org.spongepowered.asm.mixin.transformer; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.mixin.Debug; +import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.MixinEnvironment.Option; +import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; +import org.spongepowered.asm.mixin.injection.struct.Target; +import org.spongepowered.asm.util.ASMHelper; + import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.SortedSet; -import org.spongepowered.asm.lib.tree.AnnotationNode; -import org.spongepowered.asm.lib.tree.ClassNode; -import org.spongepowered.asm.lib.tree.MethodNode; -import org.spongepowered.asm.mixin.MixinEnvironment; -import org.spongepowered.asm.mixin.MixinEnvironment.Option; -import org.spongepowered.asm.mixin.injection.struct.InjectionInfo; - /** * Struct for containing target class information during mixin application */ @@ -78,6 +81,12 @@ class TargetClassContext { */ private final Map injectorMethodIndices = new HashMap(); + /** + * Information about methods in the target class, used to keep track of + * transformations we apply + */ + private final Map targetMethods = new HashMap(); + /** * Do not remap injector handler methods (debug option) */ @@ -87,6 +96,12 @@ class TargetClassContext { * True once mixins have been applied to this class */ private boolean applied; + + /** + * True if this class is decorated with an {@link Debug} annotation which + * instructs an export + */ + private boolean forceExport; TargetClassContext(String sessionId, String name, ClassNode classNode, SortedSet mixins) { this.sessionId = sessionId; @@ -97,6 +112,14 @@ class TargetClassContext { this.disableHandlerRemap = MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_DISABLE_HANDLER_REMAP); } + public boolean isApplied() { + return this.applied; + } + + public boolean isExportForced() { + return this.forceExport; + } + /** * Get the transformer session ID */ @@ -145,6 +168,26 @@ class TargetClassContext { public SortedSet getMixins() { return this.mixins; } + + /** + * Get a target method handle from the target class + * + * @param method method to get a target handle for + * @return new or existing target handle for the supplied method + */ + public Target getTargetMethod(MethodNode method) { + if (!this.classNode.methods.contains(method)) { + throw new IllegalArgumentException("Invalid target method supplied to getTargetMethod()"); + } + + String targetName = method.name + method.desc; + Target target = this.targetMethods.get(targetName); + if (target == null) { + target = new Target(this.classNode, method); + this.targetMethods.put(targetName, target); + } + return target; + } public String getHandlerName(AnnotationNode annotation, MethodNode method, boolean surrogate) { if (this.disableHandlerRemap) { @@ -180,5 +223,30 @@ class TargetClassContext { } return new MixinApplicator(this); } + + + /** + * Process {@link Debug) annotations on the class after application + */ + public void processDebugTasks() { + if (!MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE)) { + return; + } + + AnnotationNode classDebugAnnotation = ASMHelper.getVisibleAnnotation(this.classNode, Debug.class); + if (classDebugAnnotation != null) { + this.forceExport = Boolean.TRUE.equals(ASMHelper.getAnnotationValue(classDebugAnnotation, "export")); + if (Boolean.TRUE.equals(ASMHelper.getAnnotationValue(classDebugAnnotation, "print"))) { + ASMHelper.textify(this.classNode, System.err); + } + } + + for (MethodNode method : this.classNode.methods) { + AnnotationNode methodDebugAnnotation = ASMHelper.getVisibleAnnotation(method, Debug.class); + if (methodDebugAnnotation != null && Boolean.TRUE.equals(ASMHelper.getAnnotationValue(methodDebugAnnotation, "print"))) { + ASMHelper.textify(method, System.err); + } + } + } } diff --git a/src/main/java/org/spongepowered/asm/util/ASMHelper.java b/src/main/java/org/spongepowered/asm/util/ASMHelper.java index 41130e2d..9aca0025 100644 --- a/src/main/java/org/spongepowered/asm/util/ASMHelper.java +++ b/src/main/java/org/spongepowered/asm/util/ASMHelper.java @@ -27,6 +27,31 @@ package org.spongepowered.asm.util; import static com.google.common.base.Preconditions.*; import static org.spongepowered.asm.lib.ClassWriter.*; +import com.google.common.primitives.Ints; +import org.spongepowered.asm.lib.ClassReader; +import org.spongepowered.asm.lib.ClassWriter; +import org.spongepowered.asm.lib.MethodVisitor; +import org.spongepowered.asm.lib.Opcodes; +import org.spongepowered.asm.lib.Type; +import org.spongepowered.asm.lib.tree.AbstractInsnNode; +import org.spongepowered.asm.lib.tree.AnnotationNode; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.lib.tree.FieldInsnNode; +import org.spongepowered.asm.lib.tree.FieldNode; +import org.spongepowered.asm.lib.tree.InsnList; +import org.spongepowered.asm.lib.tree.InsnNode; +import org.spongepowered.asm.lib.tree.IntInsnNode; +import org.spongepowered.asm.lib.tree.JumpInsnNode; +import org.spongepowered.asm.lib.tree.LabelNode; +import org.spongepowered.asm.lib.tree.LdcInsnNode; +import org.spongepowered.asm.lib.tree.LineNumberNode; +import org.spongepowered.asm.lib.tree.MethodInsnNode; +import org.spongepowered.asm.lib.tree.MethodNode; +import org.spongepowered.asm.lib.tree.VarInsnNode; +import org.spongepowered.asm.lib.util.CheckClassAdapter; +import org.spongepowered.asm.lib.util.TraceClassVisitor; + +import java.io.OutputStream; import java.io.PrintWriter; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -35,15 +60,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import org.spongepowered.asm.lib.ClassReader; -import org.spongepowered.asm.lib.ClassWriter; -import org.spongepowered.asm.lib.Opcodes; -import org.spongepowered.asm.lib.Type; -import org.spongepowered.asm.lib.tree.*; -import org.spongepowered.asm.lib.util.CheckClassAdapter; - -import com.google.common.primitives.Ints; - /** * Utility methods for working with ASM */ @@ -373,6 +389,32 @@ public class ASMHelper { clazz.methods.add(method); } + /** + * Runs textifier on the specified class node and dumps the output to the + * specified output stream + * + * @param classNode class to textify + * @param out output stream + */ + public static void textify(ClassNode classNode, OutputStream out) { + classNode.accept(new TraceClassVisitor(new PrintWriter(out))); + } + + /** + * Runs textifier on the specified method node and dumps the output to the + * specified output stream + * + * @param methodNode method to textify + * @param out output stream + */ + public static void textify(MethodNode methodNode, OutputStream out) { + TraceClassVisitor trace = new TraceClassVisitor(new PrintWriter(out)); + MethodVisitor mv = trace.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, + methodNode.exceptions.toArray(new String[0])); + methodNode.accept(mv); + trace.visitEnd(); + } + /** * Dumps the output of CheckClassAdapter.verify to System.out * @@ -393,6 +435,19 @@ public class ASMHelper { ClassReader cr = new ClassReader(bytes); CheckClassAdapter.verify(cr, true, new PrintWriter(System.out)); } + + /** + * Prints a representation of a method's instructions to stderr + * + * @param method Method to print + */ + public static void printMethodWithOpcodeIndices(MethodNode method) { + System.err.printf("%s%s\n", method.name, method.desc); + int i = 0; + for (Iterator iter = method.instructions.iterator(); iter.hasNext();) { + System.err.printf("[%4d] %s\n", i++, ASMHelper.getNodeDescriptionForDebug(iter.next())); + } + } /** * Prints a representation of a method's instructions to stderr