author | shurailine |
Tue, 27 Oct 2015 20:06:02 -0700 | |
changeset 33402 | 1156d495a525 |
parent 30376 | 2ccf2cf7ea48 |
child 36427 | 1be706dbb3b8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
26205
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6263319 |
|
27 |
* @summary test setNativeMethodPrefix |
|
28 |
* @author Robert Field, Sun Microsystems |
|
29 |
* |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
26205
diff
changeset
|
30 |
* @modules java.base/jdk.internal.org.objectweb.asm |
33402
1156d495a525
8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents:
30376
diff
changeset
|
31 |
* java.management |
1156d495a525
8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents:
30376
diff
changeset
|
32 |
* java.instrument |
277
9d9c1747de00
6528548: 4/4 NativeMethodPrefixAgent.java times out intermittently in nightly
dcubed
parents:
2
diff
changeset
|
33 |
* @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true' |
2 | 34 |
* @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp |
35 |
*/ |
|
36 |
||
37 |
import java.lang.instrument.*; |
|
38 |
import java.security.ProtectionDomain; |
|
39 |
import java.io.*; |
|
40 |
||
26205
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
41 |
import asmlib.*; |
2 | 42 |
|
43 |
class NativeMethodPrefixAgent { |
|
44 |
||
45 |
static ClassFileTransformer t0, t1, t2; |
|
46 |
static Instrumentation inst; |
|
47 |
||
48 |
static class Tr implements ClassFileTransformer { |
|
49 |
final String trname; |
|
50 |
final int transformId; |
|
51 |
||
52 |
Tr(int transformId) { |
|
53 |
this.trname = "tr" + transformId; |
|
54 |
this.transformId = transformId; |
|
55 |
} |
|
56 |
||
57 |
public byte[] |
|
58 |
transform( |
|
59 |
ClassLoader loader, |
|
60 |
String className, |
|
61 |
Class<?> classBeingRedefined, |
|
62 |
ProtectionDomain protectionDomain, |
|
63 |
byte[] classfileBuffer) { |
|
64 |
boolean redef = classBeingRedefined != null; |
|
65 |
System.out.println(trname + ": " + |
|
66 |
(redef? "Retransforming " : "Loading ") + className); |
|
67 |
if (className != null) { |
|
68 |
try { |
|
26205
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
69 |
byte[] newcf = Instrumentor.instrFor(classfileBuffer) |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
70 |
.addNativeMethodTrackingInjection( |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
71 |
"wrapped_" + trname + "_", |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
72 |
(h)->{ |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
73 |
h.push(h.getName()); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
74 |
h.push(transformId); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
75 |
h.invokeStatic("bootreporter/StringIdCallbackReporter", "tracker", "(Ljava/lang/String;I)V", false); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
76 |
}) |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
77 |
.apply(); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
78 |
/*** debugging ... |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
79 |
if (newcf != null) { |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
80 |
String fname = trname + (redef?"_redef" : "") + "/" + className; |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
81 |
System.err.println("dumping to: " + fname); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
82 |
write_buffer(fname + "_before.class", classfileBuffer); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
83 |
write_buffer(fname + "_instr.class", newcf); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
84 |
} |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
85 |
***/ |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
86 |
|
2 | 87 |
return redef? null : newcf; |
88 |
} catch (Throwable ex) { |
|
89 |
System.err.println("ERROR: Injection failure: " + ex); |
|
90 |
ex.printStackTrace(); |
|
91 |
System.err.println("Returning bad class file, to cause test failure"); |
|
92 |
return new byte[0]; |
|
93 |
} |
|
94 |
} |
|
95 |
return null; |
|
96 |
} |
|
97 |
||
98 |
} |
|
99 |
||
100 |
// for debugging |
|
101 |
static void write_buffer(String fname, byte[]buffer) { |
|
102 |
try { |
|
26205
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
103 |
File f = new File(fname); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
104 |
if (!f.getParentFile().exists()) { |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
105 |
f.getParentFile().mkdirs(); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
106 |
} |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
107 |
try (FileOutputStream outStream = new FileOutputStream(f)) { |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
108 |
outStream.write(buffer, 0, buffer.length); |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
109 |
} |
c073791a67de
8037082: java/lang/instrument/NativeMethodPrefixAgent.java failing
jbachorik
parents:
5506
diff
changeset
|
110 |
} catch (IOException ex) { |
2 | 111 |
System.err.println("EXCEPTION in write_buffer: " + ex); |
112 |
} |
|
113 |
} |
|
114 |
||
115 |
public static void |
|
116 |
premain (String agentArgs, Instrumentation instArg) |
|
117 |
throws IOException, IllegalClassFormatException, |
|
118 |
ClassNotFoundException, UnmodifiableClassException { |
|
119 |
inst = instArg; |
|
120 |
System.out.println("Premain"); |
|
121 |
||
122 |
t1 = new Tr(1); |
|
123 |
t2 = new Tr(2); |
|
124 |
t0 = new Tr(0); |
|
125 |
inst.addTransformer(t1, true); |
|
126 |
inst.addTransformer(t2, false); |
|
127 |
inst.addTransformer(t0, true); |
|
128 |
instArg.setNativeMethodPrefix(t0, "wrapped_tr0_"); |
|
129 |
instArg.setNativeMethodPrefix(t1, "wrapped_tr1_"); |
|
130 |
instArg.setNativeMethodPrefix(t2, "wrapped_tr2_"); |
|
131 |
||
132 |
// warm up: cause load of transformer classes before used during class load |
|
133 |
instArg.retransformClasses(Runtime.class); |
|
134 |
} |
|
135 |
} |