Path: blob/master/test/jdk/java/lang/instrument/NativeMethodPrefixAgent.java
41152 views
/*1* Copyright (c) 2005, 2015, 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* @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))27* @summary test setNativeMethodPrefix28* @author Robert Field, Sun Microsystems29*30* @modules java.base/jdk.internal.org.objectweb.asm31* java.management32* java.instrument33* @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'34* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-CheckIntrinsics -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp35*/3637import java.lang.instrument.*;38import java.security.ProtectionDomain;39import java.io.*;4041import asmlib.*;4243class NativeMethodPrefixAgent {4445static ClassFileTransformer t0, t1, t2;46static Instrumentation inst;4748static class Tr implements ClassFileTransformer {49final String trname;50final int transformId;5152Tr(int transformId) {53this.trname = "tr" + transformId;54this.transformId = transformId;55}5657public byte[]58transform(59ClassLoader loader,60String className,61Class<?> classBeingRedefined,62ProtectionDomain protectionDomain,63byte[] classfileBuffer) {64boolean redef = classBeingRedefined != null;65System.out.println(trname + ": " +66(redef? "Retransforming " : "Loading ") + className);67if (className != null) {68try {69byte[] newcf = Instrumentor.instrFor(classfileBuffer)70.addNativeMethodTrackingInjection(71"wrapped_" + trname + "_",72(h)->{73h.push(h.getName());74h.push(transformId);75h.invokeStatic("bootreporter/StringIdCallbackReporter", "tracker", "(Ljava/lang/String;I)V", false);76})77.apply();78/*** debugging ...79if (newcf != null) {80String fname = trname + (redef?"_redef" : "") + "/" + className;81System.err.println("dumping to: " + fname);82write_buffer(fname + "_before.class", classfileBuffer);83write_buffer(fname + "_instr.class", newcf);84}85***/8687return redef? null : newcf;88} catch (Throwable ex) {89System.err.println("ERROR: Injection failure: " + ex);90ex.printStackTrace();91System.err.println("Returning bad class file, to cause test failure");92return new byte[0];93}94}95return null;96}9798}99100// for debugging101static void write_buffer(String fname, byte[]buffer) {102try {103File f = new File(fname);104if (!f.getParentFile().exists()) {105f.getParentFile().mkdirs();106}107try (FileOutputStream outStream = new FileOutputStream(f)) {108outStream.write(buffer, 0, buffer.length);109}110} catch (IOException ex) {111System.err.println("EXCEPTION in write_buffer: " + ex);112}113}114115public static void116premain (String agentArgs, Instrumentation instArg)117throws IOException, IllegalClassFormatException,118ClassNotFoundException, UnmodifiableClassException {119inst = instArg;120System.out.println("Premain");121122t1 = new Tr(1);123t2 = new Tr(2);124t0 = new Tr(0);125inst.addTransformer(t1, true);126inst.addTransformer(t2, false);127inst.addTransformer(t0, true);128instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");129instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");130instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");131132// warm up: cause load of transformer classes before used during class load133instArg.retransformClasses(Runtime.class);134}135}136137138