1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ACC_FINAL;
  25 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ACC_PRIVATE;
  26 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ACC_PUBLIC;
  27 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ACC_STATIC;
  28 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ACC_SUPER;
  29 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.ILOAD;
  30 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.INVOKESPECIAL;
  31 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.INVOKESTATIC;
  32 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.IRETURN;
  33 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.RETURN;
  34 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.V1_6;
  35 
  36 import java.lang.instrument.ClassDefinition;
  37 import java.lang.instrument.Instrumentation;
  38 import java.lang.reflect.Method;
  39 import java.lang.reflect.Modifier;
  40 import java.net.InetAddress;
  41 
  42 import com.sun.xml.internal.ws.org.objectweb.asm.ClassWriter;
  43 import com.sun.xml.internal.ws.org.objectweb.asm.MethodVisitor;
  44 import com.sun.xml.internal.ws.org.objectweb.asm.Type;
  45 import sun.misc.IoTrace;
  46 
  47 public class IoTraceAgent {
  48 
  49     private static IoTraceListener listener;
  50 
  51     public static void setListener(IoTraceListener l) {
  52         listener = l;
  53     }
  54 
  55     public static Object socketReadBegin(InetAddress address, int port,
  56             int timeout) {
  57         IoTraceListener l = listener;
  58         if (l != null) {
  59             return l.socketReadBegin(address, port, timeout);
  60         }
  61         return null;
  62     }
  63 
  64     public static void socketReadEnd(Object context, long bytesRead) {
  65         IoTraceListener l = listener;
  66         if (l != null) {
  67             l.socketReadEnd(context, bytesRead);
  68         }
  69     }
  70 
  71     public static Object socketWriteBegin(InetAddress address, int port) {
  72         IoTraceListener l = listener;
  73         if (l != null) {
  74             return l.socketWriteBegin(address, port);
  75         }
  76         return null;
  77     }
  78 
  79     public static void socketWriteEnd(Object context, long bytesWritten) {
  80         IoTraceListener l = listener;
  81         if (l != null) {
  82             l.socketWriteEnd(context, bytesWritten);
  83         }
  84     }
  85 
  86     public static Object fileReadBegin(String path) {
  87         IoTraceListener l = listener;
  88         if (l != null) {
  89             return l.fileReadBegin(path);
  90         }
  91         return null;
  92     }
  93 
  94     public static void fileReadEnd(Object context, long bytesRead) {
  95         IoTraceListener l = listener;
  96         if (l != null) {
  97             l.fileReadEnd(context, bytesRead);
  98         }
  99     }
 100 
 101     public static Object fileWriteBegin(String path) {
 102         IoTraceListener l = listener;
 103         if (l != null) {
 104             return l.fileWriteBegin(path);
 105         }
 106         return null;
 107     }
 108 
 109     public static void fileWriteEnd(Object context, long bytesWritten) {
 110         IoTraceListener l = listener;
 111         if (l != null) {
 112             l.fileWriteEnd(context, bytesWritten);
 113         }
 114     }
 115 
 116     public static void premain(String agentArgs, Instrumentation inst)
 117             throws Exception {
 118         ClassDefinition cd = new ClassDefinition(IoTrace.class,
 119                 generateClassAsm());
 120         inst.redefineClasses(cd);
 121     }
 122 
 123     private static byte[] generateClassAsm() {
 124         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
 125         cw.visit(V1_6, ACC_PUBLIC | ACC_SUPER | ACC_FINAL, "sun/misc/IoTrace",
 126                 null, "java/lang/Object", null);
 127 
 128         // for all methods in the existing IoTrace class
 129         // we want to create a method in the new version of it which call
 130         // IoTraceAgent
 131         //
 132         // 0: aload_0
 133         // 1: iload_1
 134         // 2: iload_2
 135         // 3: invokestatic #16 // Method
 136         // IoTraceAgent.socketReadBegin:(Ljava/net/InetAddress;II)Ljava/lang/Object;
 137         // 6: areturn
 138 
 139         for (Method om : IoTrace.class.getDeclaredMethods()) {
 140             if (!Modifier.isStatic(om.getModifiers())) {
 141                 continue;
 142             }
 143 
 144             // create a method with the same signature as the
 145             // original method
 146             MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC,
 147                     om.getName(), Type.getMethodDescriptor(om), null, null);
 148             mv.visitCode();
 149 
 150             // get the return type and argument list types
 151             Type[] argTypes = Type.getArgumentTypes(om);
 152             Type retType = Type.getReturnType(om);
 153 
 154             // load all the arguments
 155             int i = 0;
 156             for (Type t : argTypes) {
 157                 mv.visitVarInsn(t.getOpcode(ILOAD), i++);
 158             }
 159 
 160             // call a method with the same signature (but in a different class)
 161             // with all the arguments
 162             mv.visitMethodInsn(INVOKESTATIC, "IoTraceAgent", om.getName(),
 163                     Type.getMethodDescriptor(om));
 164 
 165             // return the value from the called method
 166             mv.visitInsn(retType.getOpcode(IRETURN));
 167             mv.visitEnd();
 168         }
 169 
 170         // empty private constructor
 171         MethodVisitor mv = cw.visitMethod(ACC_PRIVATE, "<init>", "()V", null,
 172                 null);
 173         mv.visitCode();
 174         mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
 175         mv.visitInsn(RETURN);
 176         mv.visitEnd();
 177 
 178         cw.visitEnd();
 179         return cw.toByteArray();
 180     }
 181 }