blob: a31a4d1c9762abd2cbb39dc160d0ab985c1becda [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 6263319
27 * @summary test setNativeMethodPrefix
28 * @author Robert Field, Sun Microsystems
29 *
30 * @run shell MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'
31 * @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp
32 */
33
34import java.lang.instrument.*;
35import java.security.ProtectionDomain;
36import java.io.*;
37
38import ilib.*;
39
40class NativeMethodPrefixAgent {
41
42 static ClassFileTransformer t0, t1, t2;
43 static Instrumentation inst;
44
45 static class Tr implements ClassFileTransformer {
46 final String trname;
47 final int transformId;
48
49 Tr(int transformId) {
50 this.trname = "tr" + transformId;
51 this.transformId = transformId;
52 }
53
54 public byte[]
55 transform(
56 ClassLoader loader,
57 String className,
58 Class<?> classBeingRedefined,
59 ProtectionDomain protectionDomain,
60 byte[] classfileBuffer) {
61 boolean redef = classBeingRedefined != null;
62 System.out.println(trname + ": " +
63 (redef? "Retransforming " : "Loading ") + className);
64 if (className != null) {
65 Options opt = new Options();
66 opt.shouldInstrumentNativeMethods = true;
67 opt.trackerClassName = "bootreporter/StringIdCallbackReporter";
68 opt.wrappedTrackerMethodName = "tracker";
69 opt.fixedIndex = transformId;
70 opt.wrappedPrefix = "wrapped_" + trname + "_";
71 try {
72 byte[] newcf = Inject.instrumentation(opt, loader, className, classfileBuffer);
73 return redef? null : newcf;
74 } catch (Throwable ex) {
75 System.err.println("ERROR: Injection failure: " + ex);
76 ex.printStackTrace();
77 System.err.println("Returning bad class file, to cause test failure");
78 return new byte[0];
79 }
80 }
81 return null;
82 }
83
84 }
85
86 // for debugging
87 static void write_buffer(String fname, byte[]buffer) {
88 try {
89 FileOutputStream outStream = new FileOutputStream(fname);
90 outStream.write(buffer, 0, buffer.length);
91 outStream.close();
92 } catch (Exception ex) {
93 System.err.println("EXCEPTION in write_buffer: " + ex);
94 }
95 }
96
97 public static void
98 premain (String agentArgs, Instrumentation instArg)
99 throws IOException, IllegalClassFormatException,
100 ClassNotFoundException, UnmodifiableClassException {
101 inst = instArg;
102 System.out.println("Premain");
103
104 t1 = new Tr(1);
105 t2 = new Tr(2);
106 t0 = new Tr(0);
107 inst.addTransformer(t1, true);
108 inst.addTransformer(t2, false);
109 inst.addTransformer(t0, true);
110 instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");
111 instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");
112 instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");
113
114 // warm up: cause load of transformer classes before used during class load
115 instArg.retransformClasses(Runtime.class);
116 }
117}