1 /* 2 * Copyright (c) 2014, 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 package compiler.profiling.spectrapredefineclass; 25 26 import com.sun.tools.attach.VirtualMachine; 27 28 import java.lang.instrument.ClassFileTransformer; 29 import java.lang.instrument.Instrumentation; 30 import java.lang.management.ManagementFactory; 31 import java.security.ProtectionDomain; 32 33 class A { 34 void m() { 35 } 36 } 37 38 class B extends A { 39 void m() { 40 } 41 } 42 43 class C extends A { 44 void m() { 45 } 46 } 47 48 class Test { 49 50 static public void m() throws Exception { 51 for (int i = 0; i < 20000; i++) { 52 m1(a); 53 } 54 for (int i = 0; i < 4; i++) { 55 m1(b); 56 } 57 } 58 59 static boolean m1(A a) { 60 boolean res = Agent.m2(a); 61 return res; 62 } 63 64 static public A a = new A(); 65 static public B b = new B(); 66 static public C c = new C(); 67 } 68 69 public class Agent implements ClassFileTransformer { 70 71 72 static public boolean m2(A a) { 73 boolean res = false; 74 if (a.getClass() == B.class) { 75 a.m(); 76 } else { 77 res = true; 78 } 79 return res; 80 } 81 82 static public void main(String[] args) throws Exception { 83 // Create speculative trap entries 84 Test.m(); 85 86 String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); 87 int p = nameOfRunningVM.indexOf('@'); 88 String pid = nameOfRunningVM.substring(0, p); 89 90 // Make the nmethod go away 91 for (int i = 0; i < 10; i++) { 92 System.gc(); 93 } 94 95 // Redefine class 96 try { 97 VirtualMachine vm = VirtualMachine.attach(pid); 98 vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", ""); 99 vm.detach(); 100 } catch (Exception e) { 101 throw new RuntimeException(e); 102 } 103 104 Test.m(); 105 // GC will hit dead method pointer 106 for (int i = 0; i < 10; i++) { 107 System.gc(); 108 } 109 } 110 111 public synchronized byte[] transform(final ClassLoader classLoader, 112 final String className, 113 Class<?> classBeingRedefined, 114 ProtectionDomain protectionDomain, 115 byte[] classfileBuffer) { 116 System.out.println("Transforming class " + className); 117 return classfileBuffer; 118 } 119 120 public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) { 121 122 try { 123 instrumentation.retransformClasses(to_redefine); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 128 } 129 130 public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { 131 Agent transformer = new Agent(); 132 instrumentation.addTransformer(transformer, true); 133 134 redefine(agentArgs, instrumentation, Test.class); 135 } 136 }