示例#1
0
 public static void redefine(Collection<ClassDefinition> classDefinitions) {
   try {
     instrumentation.redefineClasses(classDefinitions.toArray(new ClassDefinition[0]));
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  public static void doAgent(String agentArgs, Instrumentation inst) {
    HashMap<String, HashSet<String>> def = null;
    try {
      def = getDefinitionFile(agentArgs);
    } catch (Exception e) {
    }

    ExecutionRecordInnovator innovator = new ExecutionRecordInnovator(def);
    ArrayList<ClassDefinition> classDefList = new ArrayList<ClassDefinition>();
    for (String className : def.keySet()) {
      try {
        byte[] classBytes = innovator.innovate(className);
        if (classBytes == null) {
          continue;
        }

        ClassDefinition classDef =
            new ClassDefinition(Class.forName(className.replace('/', '.')), classBytes);
        classDefList.add(classDef);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    HashMap<String, ClassInnovator> innovatePlan = new HashMap<String, ClassInnovator>();
    String statClass = "oracle/jdbc/driver/OraclePreparedStatement";
    innovatePlan.put(statClass, new JDBCStatementInnovator(statClass));
    statClass = "oracle/jdbc/driver/T4CPreparedStatement";
    innovatePlan.put(statClass, new JDBCStatementInnovator(statClass));

    for (String className : innovatePlan.keySet()) {
      ClassInnovator transformer1 = innovatePlan.get(className);
      byte[] classBytes = getClassBytes(className);
      if (classBytes != null) {
        classBytes = transformer1.innovate(className);
        try {
          ClassDefinition classDef =
              new ClassDefinition(Class.forName(className.replace('/', '.')), classBytes);
          classDefList.add(classDef);
        } catch (Exception e) {

        }
      }
    }

    if (!classDefList.isEmpty()) {
      ClassDefinition[] classDefArr = new ClassDefinition[classDefList.size()];
      try {
        inst.redefineClasses(classDefList.toArray(classDefArr));
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (UnmodifiableClassException e) {
        e.printStackTrace();
      }
    }
  }
示例#3
0
  public static void premain(String args, Instrumentation inst) throws Exception {
    try {
      String[] agentArgs;
      if (args == null) agentArgs = new String[] {""};
      else agentArgs = args.split(",");
      if (!agentArgs[0].equals("instrumenting")) jarFileName = agentArgs[0];
      BaseClassTransformer rct = null;
      rct = new BaseClassTransformer();
      if (agentArgs[0].equals("instrumenting")) {
        initVMClasses = new HashSet<String>();
        for (Class<?> c : inst.getAllLoadedClasses()) {
          ((Set<String>) initVMClasses).add(c.getName());
        }
      }
      if (!agentArgs[0].equals("instrumenting")) {

        inst.addTransformer(rct);
        Tracer.setLocals(new CounterThreadLocal());
        Tracer.overrideAll(true);
        for (Class<?> c : inst.getAllLoadedClasses()) {
          try {
            if (c.isInterface()) continue;
            if (c.isArray()) continue;
            byte[] bytes = rct.getBytes(c.getName());
            if (bytes == null) {
              continue;
            }
            inst.redefineClasses(new ClassDefinition[] {new ClassDefinition(c, bytes)});
          } catch (Throwable e) {
            synchronized (System.err) {
              System.err.println("" + c + " failed...");
              e.printStackTrace();
            }
          }
        }
        Runtime.getRuntime()
            .addShutdownHook(
                new Thread() {
                  public void run() {
                    Tracer.mark();
                    try {
                      PrintStream ps = new PrintStream("bailout.txt");
                      ps.println("Bailouts: " + Tracer.getBailoutCount());
                      ps.close();
                    } catch (Exception e) {
                    }
                    Tracer.unmark();
                  }
                });
        if ("true".equals(System.getProperty("bci.observerOn"))) Tracer.overrideAll(false);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }