author | iignatyev |
Wed, 27 Jun 2018 16:19:28 -0700 | |
changeset 50855 | 9ca5f18240b2 |
parent 47216 | 71c04702a3d5 |
child 52319 | 625f6c742392 |
permissions | -rw-r--r-- |
38197
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
1 |
/* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
4 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
8 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
13 |
* accompanied this code). |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
14 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
18 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
21 |
* questions. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
22 |
*/ |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
23 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
24 |
/* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
25 |
* A simple way to test JVMTI ClassFileLoadHook. See ../testlibrary_tests/SimpleClassFileLoadHookTest.java |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
26 |
* for an example. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
27 |
*/ |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
28 |
#include <stdio.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
29 |
#include <stdarg.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
30 |
#include <stdlib.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
31 |
#include <string.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
32 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
33 |
#include <jvmti.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
34 |
#include <jni.h> |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
35 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
36 |
static char* CLASS_NAME = NULL; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
37 |
static char* FROM = NULL; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
38 |
static char* TO = NULL; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
39 |
static jvmtiEnv *jvmti = NULL; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
40 |
static jvmtiEventCallbacks callbacks; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
41 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
42 |
/** |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
43 |
* For all classes whose name equals to CLASS_NAME, we replace all occurrence of FROM to TO |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
44 |
* in the classfile data. CLASS_NAME must be a binary class name. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
45 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
46 |
* FROM is usually chosen as part of a UTF8 string in the class file. For example, if the |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
47 |
* original class file has |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
48 |
* String getXXX() { return "theXXX";} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
49 |
* You can set FROM=XXX, TO=YYY to rewrite the class to be |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
50 |
* String getYYY() { return "theYYY";} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
51 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
52 |
* Please note that the replacement is NOT limited just the UTF8 strings, but rather applies |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
53 |
* to all the bytes in the classfile. So if you pick a very short FROM string like X, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
54 |
* it may override any POP2 bytecodes, which have the value 88 (ascii 'X'). |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
55 |
* |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
56 |
* A good FROM string to use is 'cellphone', where the first 4 bytes represent the bytecode |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
57 |
* sequence DADD/LSUB/IDIV/IDIV, which does not appear in valid bytecode streams. |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
58 |
*/ |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
59 |
void JNICALL |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
60 |
ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, jclass class_beeing_redefined, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
61 |
jobject loader, const char* name, jobject protection_domain, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
62 |
jint class_data_len, const unsigned char* class_data, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
63 |
jint *new_class_data_len, unsigned char** new_class_data) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
64 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
65 |
if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
66 |
size_t n = strlen(FROM); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
67 |
unsigned char* new_data; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
68 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
69 |
if ((*jvmti)->Allocate(jvmti, class_data_len, &new_data) == JNI_OK) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
70 |
const unsigned char* s = class_data; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
71 |
unsigned char* d = new_data; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
72 |
unsigned char* end = d + class_data_len; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
73 |
int count = 0; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
74 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
75 |
fprintf(stderr, "found class to be hooked: %s - rewriting ...\n", name); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
76 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
77 |
while (d + n < end) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
78 |
if (memcmp(s, FROM, n) == 0) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
79 |
memcpy(d, TO, n); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
80 |
s += n; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
81 |
d += n; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
82 |
count++; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
83 |
} else { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
84 |
*d++ = *s++; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
85 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
86 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
87 |
while (d < end) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
88 |
*d++ = *s++; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
89 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
90 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
91 |
*new_class_data_len = class_data_len; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
92 |
*new_class_data = new_data; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
93 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
94 |
fprintf(stderr, "Rewriting done. Replaced %d occurrence(s)\n", count); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
95 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
96 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
97 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
98 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
99 |
static jint init_options(char *options) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
100 |
char* class_name; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
101 |
char* from; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
102 |
char* to; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
103 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
104 |
fprintf(stderr, "Agent library loaded with options = %s\n", options); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
105 |
if ((class_name = options) != NULL && |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
106 |
(from = strchr(class_name, ',')) != NULL && (from[1] != 0)) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
107 |
*from = 0; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
108 |
from++; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
109 |
if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
110 |
*to = 0; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
111 |
to++; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
112 |
if (strchr(to, ',') == NULL && |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
113 |
strlen(to) == strlen(from) && |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
114 |
strlen(class_name) > 0 && |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
115 |
strlen(to) > 0) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
116 |
CLASS_NAME = strdup(class_name); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
117 |
FROM = strdup(from); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
118 |
TO = strdup(to); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
119 |
fprintf(stderr, "CLASS_NAME = %s, FROM = %s, TO = %s\n", |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
120 |
CLASS_NAME, FROM, TO); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
121 |
return JNI_OK; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
122 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
123 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
124 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
125 |
fprintf(stderr, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
126 |
"Incorrect options. You need to start the JVM with -agentlib:ClassFileLoadHook=<classname>,<from>,<to>\n" |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
127 |
"where <classname> is the class you want to hook, <from> is the string in the classfile to be replaced\n" |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
128 |
"with <to>. <from> and <to> must have the same length. Example:\n" |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
129 |
" @run main/native -agentlib:ClassFileLoadHook=Foo,XXX,YYY ClassFileLoadHookTest\n"); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
130 |
return JNI_ERR; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
131 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
132 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
133 |
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
134 |
int rc; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
135 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
136 |
if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
137 |
fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
138 |
return JNI_ERR; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
139 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
140 |
if ((rc = init_options(options)) != JNI_OK) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
141 |
return JNI_ERR; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
142 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
143 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
144 |
(void) memset(&callbacks, 0, sizeof(callbacks)); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
145 |
callbacks.ClassFileLoadHook = &ClassFileLoadHook; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
146 |
if ((rc = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks))) != JNI_OK) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
147 |
fprintf(stderr, "SetEventCallbacks failed, error = %d\n", rc); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
148 |
return JNI_ERR; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
149 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
150 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
151 |
if ((rc = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
152 |
JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)) != JNI_OK) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
153 |
fprintf(stderr, "SetEventNotificationMode failed, error = %d\n", rc); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
154 |
return JNI_ERR; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
155 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
156 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
157 |
return JNI_OK; |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
158 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
159 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
160 |
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
161 |
return Agent_Initialize(jvm, options, reserved); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
162 |
} |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
163 |
|
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
164 |
JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
165 |
return Agent_Initialize(jvm, options, reserved); |
d51ca2fac8d6
8155239: [TESTBUG] Simple test setup for JVMTI ClassFileLoadHook
iklam
parents:
diff
changeset
|
166 |
} |