0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6274264 6274241 5070281
0N/A * @summary test retransformClasses
0N/A * @author Robert Field, Sun Microsystems
0N/A *
137N/A * @run shell/timeout=240 MakeJAR2.sh RetransformAgent RetransformApp 'Can-Retransform-Classes: true'
0N/A * @run main/othervm -javaagent:RetransformAgent.jar RetransformApp
0N/A */
0N/A
0N/Aimport java.lang.instrument.*;
0N/Aimport java.security.ProtectionDomain;
0N/Aimport java.io.*;
0N/A
0N/Aimport ilib.*;
0N/A
0N/Aclass RetransformAgent {
0N/A
0N/A static ClassFileTransformer t1, t2, t3, t4;
0N/A static Instrumentation inst;
0N/A static boolean succeeded = true;
0N/A static int markCount = 0;
0N/A static int[] markGolden = {30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40, 20,
0N/A 11, 40, 20, 11, 40, 20, 11, 40, 20, 11, 40, 20};
0N/A
0N/A static class Tr implements ClassFileTransformer {
0N/A final String trname;
0N/A final boolean onLoad;
0N/A final int loadIndex;
0N/A final boolean onRedef;
0N/A final int redefIndex;
0N/A final String cname;
0N/A final String nname;
0N/A
0N/A Tr(String trname, boolean onLoad, int loadIndex, boolean onRedef, int redefIndex,
0N/A String cname, String nname) {
0N/A this.trname = trname;
0N/A this.onLoad = onLoad;
0N/A this.loadIndex = loadIndex;
0N/A this.onRedef = onRedef;
0N/A this.redefIndex = redefIndex;
0N/A this.cname = cname;
0N/A this.nname = nname;
0N/A }
0N/A
0N/A public byte[] transform(ClassLoader loader,
0N/A String className,
0N/A Class<?> classBeingRedefined,
0N/A ProtectionDomain protectionDomain,
0N/A byte[] classfileBuffer) {
0N/A boolean redef = classBeingRedefined != null;
0N/A // System.err.println("hook " + trname + ": " + className +
0N/A // (redef? " REDEF" : " LOAD"));
0N/A if ((redef? onRedef : onLoad) && className != null && className.equals(cname)) {
0N/A Options opt = new Options();
0N/A opt.shouldInstrumentIndexed = true;
0N/A opt.shouldInstrumentCall = true;
0N/A opt.targetMethod = nname;
0N/A opt.fixedIndex = redef? redefIndex : loadIndex;
0N/A opt.trackerClassName = "RetransformAgent";
0N/A try {
0N/A byte[] newcf = Inject.instrumentation(opt, loader, className, classfileBuffer);
0N/A /*** debugging ...
0N/A String fname = trname + (redef?"_redef" : "");
0N/A write_buffer(fname + "_before.class", classfileBuffer);
0N/A write_buffer(fname + "_instr.class", newcf);
0N/A ***/
0N/A System.err.println(trname + ": " + className + " index: " + opt.fixedIndex +
0N/A (redef? " REDEF" : " LOAD") +
0N/A " len before: " + classfileBuffer.length +
0N/A " after: " + newcf.length);
0N/A return newcf;
0N/A } catch (Throwable ex) {
0N/A System.err.println("Injection failure: " + ex);
0N/A ex.printStackTrace();
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A static void write_buffer(String fname, byte[]buffer) {
0N/A try {
0N/A FileOutputStream outStream = new FileOutputStream(fname);
0N/A outStream.write(buffer, 0, buffer.length);
0N/A outStream.close();
0N/A } catch (Exception ex) {
0N/A System.err.println("EXCEPTION in write_buffer: " + ex);
0N/A }
0N/A }
0N/A
0N/A public static void premain (String agentArgs, Instrumentation instArg) {
0N/A inst = instArg;
0N/A System.err.println("Premain");
0N/A
0N/A t1 = new Tr("TR1", false, 10, true, 11, "RetransformApp", "foo");
0N/A inst.addTransformer(t1, true);
0N/A t2 = new Tr("TN2", true, 20, true, 21, "RetransformApp", "foo");
0N/A inst.addTransformer(t2, false);
0N/A t3 = new Tr("TR3", true, 30, true, 31, "RetransformApp", "foo");
0N/A inst.addTransformer(t3, true);
0N/A t4 = new Tr("TN4", true, 40, true, 41, "RetransformApp", "foo");
0N/A inst.addTransformer(t4, false);
0N/A }
0N/A
0N/A public static void undo() {
0N/A inst.removeTransformer(t3);
0N/A try {
0N/A System.err.println("RETRANSFORM");
0N/A inst.retransformClasses(new RetransformApp().getClass());
0N/A } catch (Exception ex) {
0N/A System.err.println("EXCEPTION on undo: " + ex);
0N/A }
0N/A }
0N/A
0N/A public static boolean succeeded() {
0N/A return succeeded && markCount == markGolden.length;
0N/A }
0N/A
0N/A public static void callTracker(int mark) {
0N/A System.err.println("got mark " + mark);
0N/A if (markCount >= markGolden.length || mark != markGolden[markCount++]) {
0N/A succeeded = false;
0N/A }
0N/A }
0N/A}