Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/instrument/NativeMethodPrefixAgent.java
38813 views
/*1* Copyright (c) 2005, 2008, 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*/2223/**24* @test25* @bug 626331926* @summary test setNativeMethodPrefix27* @author Robert Field, Sun Microsystems28*29* @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'30* @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp31*/3233import java.lang.instrument.*;34import java.security.ProtectionDomain;35import java.io.*;3637import ilib.*;3839class NativeMethodPrefixAgent {4041static ClassFileTransformer t0, t1, t2;42static Instrumentation inst;4344static class Tr implements ClassFileTransformer {45final String trname;46final int transformId;4748Tr(int transformId) {49this.trname = "tr" + transformId;50this.transformId = transformId;51}5253public byte[]54transform(55ClassLoader loader,56String className,57Class<?> classBeingRedefined,58ProtectionDomain protectionDomain,59byte[] classfileBuffer) {60boolean redef = classBeingRedefined != null;61System.out.println(trname + ": " +62(redef? "Retransforming " : "Loading ") + className);63if (className != null) {64Options opt = new Options();65opt.shouldInstrumentNativeMethods = true;66opt.trackerClassName = "bootreporter/StringIdCallbackReporter";67opt.wrappedTrackerMethodName = "tracker";68opt.fixedIndex = transformId;69opt.wrappedPrefix = "wrapped_" + trname + "_";70try {71byte[] newcf = Inject.instrumentation(opt, loader, className, classfileBuffer);72return redef? null : newcf;73} catch (Throwable ex) {74System.err.println("ERROR: Injection failure: " + ex);75ex.printStackTrace();76System.err.println("Returning bad class file, to cause test failure");77return new byte[0];78}79}80return null;81}8283}8485// for debugging86static void write_buffer(String fname, byte[]buffer) {87try {88FileOutputStream outStream = new FileOutputStream(fname);89outStream.write(buffer, 0, buffer.length);90outStream.close();91} catch (Exception ex) {92System.err.println("EXCEPTION in write_buffer: " + ex);93}94}9596public static void97premain (String agentArgs, Instrumentation instArg)98throws IOException, IllegalClassFormatException,99ClassNotFoundException, UnmodifiableClassException {100inst = instArg;101System.out.println("Premain");102103t1 = new Tr(1);104t2 = new Tr(2);105t0 = new Tr(0);106inst.addTransformer(t1, true);107inst.addTransformer(t2, false);108inst.addTransformer(t0, true);109instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");110instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");111instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");112113// warm up: cause load of transformer classes before used during class load114instArg.retransformClasses(Runtime.class);115}116}117118119