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 6263319
0N/A * @summary test setNativeMethodPrefix
0N/A * @author Robert Field, Sun Microsystems
0N/A *
137N/A * @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'
0N/A * @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp
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 NativeMethodPrefixAgent {
0N/A
0N/A static ClassFileTransformer t0, t1, t2;
0N/A static Instrumentation inst;
0N/A
0N/A static class Tr implements ClassFileTransformer {
0N/A final String trname;
0N/A final int transformId;
0N/A
0N/A Tr(int transformId) {
0N/A this.trname = "tr" + transformId;
0N/A this.transformId = transformId;
0N/A }
0N/A
0N/A public byte[]
0N/A transform(
0N/A 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.out.println(trname + ": " +
0N/A (redef? "Retransforming " : "Loading ") + className);
0N/A if (className != null) {
0N/A Options opt = new Options();
0N/A opt.shouldInstrumentNativeMethods = true;
0N/A opt.trackerClassName = "bootreporter/StringIdCallbackReporter";
0N/A opt.wrappedTrackerMethodName = "tracker";
0N/A opt.fixedIndex = transformId;
0N/A opt.wrappedPrefix = "wrapped_" + trname + "_";
0N/A try {
0N/A byte[] newcf = Inject.instrumentation(opt, loader, className, classfileBuffer);
0N/A return redef? null : newcf;
0N/A } catch (Throwable ex) {
0N/A System.err.println("ERROR: Injection failure: " + ex);
0N/A ex.printStackTrace();
0N/A System.err.println("Returning bad class file, to cause test failure");
0N/A return new byte[0];
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A }
0N/A
0N/A // for debugging
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
0N/A premain (String agentArgs, Instrumentation instArg)
0N/A throws IOException, IllegalClassFormatException,
0N/A ClassNotFoundException, UnmodifiableClassException {
0N/A inst = instArg;
0N/A System.out.println("Premain");
0N/A
0N/A t1 = new Tr(1);
0N/A t2 = new Tr(2);
0N/A t0 = new Tr(0);
0N/A inst.addTransformer(t1, true);
0N/A inst.addTransformer(t2, false);
0N/A inst.addTransformer(t0, true);
0N/A instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");
0N/A instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");
0N/A instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");
0N/A
0N/A // warm up: cause load of transformer classes before used during class load
0N/A instArg.retransformClasses(Runtime.class);
0N/A }
0N/A}