Path: blob/master/test/hotspot/jtreg/compiler/profiling/spectrapredefineclass/Agent.java
41153 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.profiling.spectrapredefineclass;2425import com.sun.tools.attach.VirtualMachine;26import jdk.test.lib.Utils;2728import java.lang.instrument.ClassFileTransformer;29import java.lang.instrument.Instrumentation;30import java.nio.file.Paths;31import java.security.ProtectionDomain;3233class A {34void m() {35}36}3738class B extends A {39void m() {40}41}4243class C extends A {44void m() {45}46}4748class Test {4950static public void m() throws Exception {51for (int i = 0; i < 20000; i++) {52m1(a);53}54for (int i = 0; i < 4; i++) {55m1(b);56}57}5859static boolean m1(A a) {60boolean res = Agent.m2(a);61return res;62}6364static public A a = new A();65static public B b = new B();66static public C c = new C();67}6869public class Agent implements ClassFileTransformer {70public static final String AGENT_JAR = Paths.get(Utils.TEST_CLASSES, "agent.jar").toString();71static public boolean m2(A a) {72boolean res = false;73if (a.getClass() == B.class) {74a.m();75} else {76res = true;77}78return res;79}8081static public void main(String[] args) throws Exception {82// Create speculative trap entries83Test.m();8485String pid = Long.toString(ProcessHandle.current().pid());8687// Make the nmethod go away88for (int i = 0; i < 10; i++) {89System.gc();90}9192// Redefine class93try {94VirtualMachine vm = VirtualMachine.attach(pid);95vm.loadAgent(AGENT_JAR, "");96vm.detach();97} catch (Exception e) {98throw new RuntimeException(e);99}100101Test.m();102// GC will hit dead method pointer103for (int i = 0; i < 10; i++) {104System.gc();105}106}107108public synchronized byte[] transform(final ClassLoader classLoader,109final String className,110Class<?> classBeingRedefined,111ProtectionDomain protectionDomain,112byte[] classfileBuffer) {113System.out.println("Transforming class " + className);114return classfileBuffer;115}116117public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {118119try {120instrumentation.retransformClasses(to_redefine);121} catch (Exception e) {122e.printStackTrace();123}124125}126127public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {128Agent transformer = new Agent();129instrumentation.addTransformer(transformer, true);130131redefine(agentArgs, instrumentation, Test.class);132}133}134135136