Package alt.jiapi.reflect

Examples of alt.jiapi.reflect.InstructionList


     */
    public void instrument(InstructionList il) {
        ArrayList al = new ArrayList();
        JiapiMethod m = il.getDeclaringMethod();
        Instrumentation instrumentation = getInstrumentation();
        InstructionList methodInstructions = m.getInstructionList();

//         TryBlock[] tryBlocks = m.getTryBlocks();
//         for (int i = 0; i < tryBlocks.length; i++) {
//             ExceptionHandler[] eh = tryBlocks[i].getExceptionHandlers();

View Full Code Here


    // (Exception in this case). Subsequent Instrumentors
    // might need this piece of code.
    // Other analyzations may be added later.
    private boolean analyze(InstructionList il,
                            Instrumentation instrumentation) {
        InstructionList targetCode = null;
        int index = 0;

        if (il.size() > 0) {
            index = il.indexOf(OpcodeGroups.REFERENCE_STORE_INSTRUCTIONS,0);
            log.debug("First instruction during analyze is " + il.get(0));
View Full Code Here

    public void instrument(InstructionList il) {
        log.info("Instrumenting " + getCurrentClass().getName() + "." +
                 il.getDeclaringMethod().getName());
       

        InstructionList view = null;

        if (instructionCount > il.size()) {
            log.warn("List size exceeded: instructions needed : " +
                     instructionCount + ", available " + il.size() +
                     ". Using all that is available.");
View Full Code Here

        /**
         * Actual copying of instructions to hotspot.
         */
        private void copyInstructions(HotSpot hs) {
            InstructionList am_il = adviceMethod.getInstructionList();
            InstructionList il = hs.getInstructionList();
      Instruction firstHotSpotInstruction = il.get(0);

            //
            // NOTE: We must handle local variables correctly
            // NOTE: should be done in InstructionList.add/insert(Instruction)
            am_il = changeLocalVars(am_il, il.getDeclaringMethod().getMaxLocals()-1);

            int idx = indexOfDoHotSpot();
                       
            if (idx == -1) {
                log.debug("Replacing hotspot with advice-method");
                il.clear();
                il.add(am_il);
                return;
            }
            else {
                InstructionList before = am_il.createView(0, idx - 1);

      // If first hs instruction is branch target, we
      // will have to change this to first instruction of
      // before-list, so that we do not introduce dead-code.
    if (before.size() > 0) {
        checkForBranchTarget(firstHotSpotInstruction, il.getDeclaringMethod().getInstructionList(), before.get(0));
    }

                InstructionList after = null;
                if (idx+1 < am_il.size()) {
                    // Strip 'return' statement
                    after = am_il.createView(idx + 1, am_il.size() - 1);
                }
               
View Full Code Here

        JiapiClass clazz = getCurrentClass();
        JiapiMethod[] methods = clazz.getDeclaredMethods();

        for (int i = 0; i < methods.length; i++) {
            log.debug("dispatching for: " + methods[i].getName());
            InstructionList _il = methods[i].getInstructionList();
            forward(_il);
        }
    }
View Full Code Here

         * Anyway, at the moment, instrumented class will not pass bytecode
         * verifier(I think), since there will be a non-existent call to
         * super.doHotSpot().
         */
        private int indexOfDoHotSpot() {
            InstructionList il = adviceMethod.getInstructionList();
            HotSpotLocator hsl = new HotSpotLocator(il, Opcodes.INVOKEVIRTUAL);

            HotSpot[] dohsCandidates = hsl.getHotSpots();
            for (int i = 0; i < dohsCandidates.length; i++) {
                Invocation inv =
                    (Invocation)dohsCandidates[i].getHotSpotInstruction();

                if (!inv.getClassName().equals(adviceMethod.getDeclaringClass().getName())) {
                    // Process only calls to HotSpotAdvice class
                    continue;
                }

                if ("doHotSpot".equals(inv.getMethodName())) {
//                     if (inv.getAttribute("synthetic") != null) {
//                         // Found it
//                         return il.indexOf(inv);
//                     }
//                     continue;
                    return il.indexOf(inv);
                }
            }
           
            return -1;
        }
View Full Code Here

            // following could be added, if no longs/doubles are in target
            // list; they reserve two slots for local vars. Another
            // silly stuff for longs/doubles in JVMs
            // maxLocalsInOtherList--;
           
            InstructionList newList = new InstructionList();
           
            InstructionFactory factory = new InstructionFactory();
            for(int i = 0; i < advice.size(); i++) {
                Instruction ins = advice.get(i);
                newList.add(ins);
                switch(ins.getOpcode()) {
                    // -- ALOAD family --------------------------------------
                    case Opcodes.ALOAD_0:
                        newList.replace(i, factory.aload(maxLocalsInOtherList));
                        break;
                    case Opcodes.ALOAD_1:
                        newList.replace(i, factory.aload(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.ALOAD_2:
                        newList.replace(i, factory.aload(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.ALOAD_3:
                        newList.replace(i, factory.aload(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.ALOAD:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                    case Opcodes.ASTORE_0:
                        newList.replace(i, factory.astore(maxLocalsInOtherList));
                        break;
                    case Opcodes.ASTORE_1:
                        newList.replace(i, factory.astore(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.ASTORE_2:
                        newList.replace(i, factory.astore(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.ASTORE_3:
                        newList.replace(i, factory.astore(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.ASTORE:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;

                    // -- ILOAD family --------------------------------------
                    case Opcodes.ILOAD_0:
                        newList.replace(i, factory.iload(maxLocalsInOtherList));
                        break;
                    case Opcodes.ILOAD_1:
                        newList.replace(i, factory.iload(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.ILOAD_2:
                        newList.replace(i, factory.iload(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.ILOAD_3:
                        newList.replace(i, factory.iload(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.ILOAD:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                    case Opcodes.ISTORE_0:
                        newList.replace(i, factory.istore(maxLocalsInOtherList));
                        break;
                    case Opcodes.ISTORE_1:
                        newList.replace(i, factory.istore(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.ISTORE_2:
                        newList.replace(i, factory.istore(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.ISTORE_3:
                        newList.replace(i, factory.istore(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.ISTORE:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;

                    // -- DLOAD family --------------------------------------
                    case Opcodes.DLOAD_0:
                        newList.replace(i, factory.dload(maxLocalsInOtherList));
                        break;
                    case Opcodes.DLOAD_1:
                        newList.replace(i, factory.dload(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.DLOAD_2:
                        newList.replace(i, factory.dload(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.DLOAD_3:
                        newList.replace(i, factory.dload(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.DLOAD:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                    case Opcodes.DSTORE_0:
                        newList.replace(i, factory.dstore(maxLocalsInOtherList));
                        break;
                    case Opcodes.DSTORE_1:
                        newList.replace(i, factory.dstore(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.DSTORE_2:
                        newList.replace(i, factory.dstore(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.DSTORE_3:
                        newList.replace(i, factory.dstore(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.DSTORE:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;

                    // -- LLOAD family --------------------------------------
                    case Opcodes.LLOAD_0:
                        newList.replace(i, factory.lload(maxLocalsInOtherList));
                        break;
                    case Opcodes.LLOAD_1:
                        newList.replace(i, factory.lload(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.LLOAD_2:
                        newList.replace(i, factory.lload(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.LLOAD_3:
                        newList.replace(i, factory.lload(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.LLOAD:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                    case Opcodes.LSTORE_0:
                        newList.replace(i, factory.lstore(maxLocalsInOtherList));
                        break;
                    case Opcodes.LSTORE_1:
                        newList.replace(i, factory.lstore(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.LSTORE_2:
                        newList.replace(i, factory.lstore(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.LSTORE_3:
                        newList.replace(i, factory.lstore(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.LSTORE:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                       
                    // -- FLOAD family --------------------------------------
                    case Opcodes.FLOAD_0:
                        newList.replace(i, factory.fload(maxLocalsInOtherList));
                        break;
                    case Opcodes.FLOAD_1:
                        newList.replace(i, factory.fload(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.FLOAD_2:
                        newList.replace(i, factory.fload(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.FLOAD_3:
                        newList.replace(i, factory.fload(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.FLOAD:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
                        ins.getBytes()[1] += (byte)maxLocalsInOtherList;
                        break;
                    case Opcodes.FSTORE_0:
                        newList.replace(i, factory.fstore(maxLocalsInOtherList));
                        break;
                    case Opcodes.FSTORE_1:
                        newList.replace(i, factory.fstore(maxLocalsInOtherList + 1));
                        break;
                    case Opcodes.FSTORE_2:
                        newList.replace(i, factory.fstore(maxLocalsInOtherList + 2));
                        break;
                    case Opcodes.FSTORE_3:
                        newList.replace(i, factory.fstore(maxLocalsInOtherList + 3));
                        break;
                    case Opcodes.FSTORE:
                        // Handle this differently. This
                        // form of handling does not break exception table
                        // So, we minimize damage by handling this differently
View Full Code Here

        super(ii);
        this.handler = handler;
    }

    public void instrument(JiapiMethod jm) {
        InstructionList il = jm.getInstructionList();

        if ("<clinit>".equals(jm.getName())) {
            return;
        }

        InstructionFactory factory = il.getInstructionFactory();
        JiapiClass ii = getEventProducer();
        JiapiMethod invokeMethod = null;

        try {
            invokeMethod =
                ii.getDeclaredMethod("invokeMethod",
                                     new String[] { "java.lang.Object",
                                                    "java.lang.String",
                                                    "java.lang.Object[]",
                                                    "java.lang.String"});
        }
        catch(Exception e) {
            e.printStackTrace();
        }


        int idx = -1;
        while ((idx = il.indexOf(OpcodeGroups.INVOKE_INSTRUCTIONS, idx+1)) != -1) {
            Invocation ins = (Invocation)il.get(idx);
            if (!match(ins.getClassName() + "." + ins.getMethodName())) {
                continue;
            }


//             if (System.getProperty("no-lfix") == null) {
//                 // --- bug workaround  for long/doubles in method params
//                 boolean bailOutForLongDoubleSyndrome = false;
//                 String[] i_params = ins.getParameterTypes();
//                 for (int i = 0; i < i_params.length; i++) {
//                     if ("double".equals(i_params[i]) ||
//                         "long".equals(i_params[i])) {
//                         bailOutForLongDoubleSyndrome = true;
//                         break;
//                     }
//                 }
//                 if (bailOutForLongDoubleSyndrome) {
//                     log.warn("Will not instrument invocations to methods with long or double as parameter: In " + jm.getDeclaringClass().getName() + "#" + jm +
//                              ", a call to " + ins + ". This is a workaround for bug in jiapi.");
//                     continue;
//                 }
//                 // --- bug workaround  for long/doubles in method params
//             }

            // We support only these methods at the moment.
            if (ins.getOpcode() != Opcodes.INVOKESTATIC &&
                ins.getOpcode() != Opcodes.INVOKEVIRTUAL) {
                continue;
            }

            JiapiField interceptor = getEventProducerField();
       
//              InstructionList nList = il.createEmptyList();
            InstructionList nList = new InstructionList();


            // each entry in pList holds creation of one argument
            // to method invocation
//             InstructionList[] pLists = createArgumentLists(il, idx);
            ArgumentList al = createArgumentLists(il, idx);
            InstructionList[] pLists = al.arguments;
            int paramIdx = al.paramIndex;

            short opCode = ins.getOpcode();
            if (opCode == Opcodes.INVOKEVIRTUAL ||
                opCode == Opcodes.INVOKESPECIAL) {
                paramIdx--; // Include object ref
            }

            // Generate code, that replaces invocation with a new
            // invocation to InvokeHandler
            switch(opCode) {
            case Opcodes.INVOKEVIRTUAL:
            case Opcodes.INVOKESTATIC:

                nList.add(factory.getField(interceptor)); // Interceptor
                if (opCode == Opcodes.INVOKESTATIC) {
//                     addClassForNameInstructions(jm.getDeclaringClass().getName(), nList);
                    addClassForNameInstructions(ins.getClassName(), nList);
                   
                }
                else {
                    nList.add(il.get(paramIdx)); // objref
                }

                String mName = ins.getMethodName();
                //nList.add(factory.getField(rField));
                nList.add(factory.pushConstant(mName));
                nList.add(factory.newArray("java.lang.Object",
                                           ins.getParameterTypes().length));

                String[] i_params = ins.getParameterTypes();
                // Populate Object array with call parameters
                for (int i = 0; i < pLists.length; i++) {
                    if ("long".equals(i_params[i])) {
                        nList.add(factory.dup());
                    }
                    else if ("double".equals(i_params[i])) {
                        nList.add(factory.dup());
                    }
                    else {
                        nList.add(factory.dup());
                    }
                    nList.add(factory.pushConstant(i));
                    nList.add(pLists[i]);
                    nList.add(factory.aastore());
                }

                // Add Methods signature
//                 nList.add(factory.pushConstant(ins.getDescriptor()));
                // Add cache-key
                nList.add(factory.pushConstant(ins.getClassName() + ins.getMethodName() + ins.getDescriptor()));

                // call Interceptor
                nList.add(factory.invoke(invokeMethod));

                handleReturnValue(nList, ins);


                // Replace invocation and its parameters with new
                // instruction list
                il.replace(paramIdx, idx + 1, nList);

                break;
            }

            // Next index. Skip Instructions created above.
            //idx += nList.size() - (idx - paramIdx) - 1;
            idx = paramIdx + nList.size() - 1;
        }
    }
View Full Code Here

        int pIdx = invocationIndex - 1;

//         for (int i = 0; i < argList.length; i++) {
         for (int i = argList.length - 1; i >= 0; i--) {
            int stackUsage = ins.stackConsumption();
            InstructionList pList = il.createEmptyList();

            boolean primitive = SignatureUtil.isPrimitive(paramTypes[i]);
            Instruction pr_ins = null;

            if (primitive) {
                pr_ins = handlePrimitiveType(paramTypes[i], pList);
            }

            int insertIdx = pList.size();
            // When stack usage is 1, we have reached an argument in stack
//             while (stackUsage != 1) {
             while ((stackUsage > 0 && ins.getOpcode() == Opcodes.INVOKESTATIC)||
                    (stackUsage > 1 && ins.getOpcode() == Opcodes.INVOKEVIRTUAL)) {
                Instruction pIns = il.get(pIdx);
//                  System.out.println(">> pIdx: " + pIdx + ": " + pIns +
//                                     ", stackusage: " + stackUsage);
                stackUsage -= pIns.stackUsage();

                // Insert pIns allways to same index. We are scanning
                // Instructions backwards!
//                  System.out.println("Adding " + pIns + " to plist");
                pList.insert(insertIdx, pIns);
                pIdx--;
            }

            if (primitive) {
                pList.add(pr_ins); // Call constructor of primitive wrapper
            }

            argList[i] = pList;
         }
        
View Full Code Here


    private void addClassForNameInstructions(String name, InstructionList il) {
        InstructionFactory f = il.getInstructionFactory();

        InstructionList nl = il.createEmptyList();
       
        // NOTE: We do not create exception handlers for
        //       Class.forName(...) invocation.
        //       However, we use this to get Class of the running object,
        //       so its Class is allways found.
        try {
            nl.add(f.pushConstant(name));
            nl.add(f.invoke(Modifier.STATIC, "java.lang.Class",
                            "forName", new Signature("java.lang.Class",
                                                     new String[] {"java.lang.String"})));
        }
        catch(Exception e) {
            e.printStackTrace();
View Full Code Here

TOP

Related Classes of alt.jiapi.reflect.InstructionList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.