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 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.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <jvmti.h>
27 #include "agent_common.h"
28 #include "JVMTITools.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #ifndef JNI_ENV_ARG
35
36 #ifdef __cplusplus
37 #define JNI_ENV_ARG(x, y) y
38 #define JNI_ENV_PTR(x) x
39 #else
40 #define JNI_ENV_ARG(x,y) x, y
41 #define JNI_ENV_PTR(x) (*x)
42 #endif
43
44 #endif
45
46 #define STATUS_FAILED 2
47 #define PASSED 0
48
49 static jvmtiEnv *jvmti = NULL;
50 static jvmtiCapabilities caps;
51 static jvmtiEventCallbacks callbacks;
52 static jint result = PASSED;
53 static jboolean printdump = JNI_FALSE;
54
55 const char* CLASS_NAME = "nsk/jvmti/unit/events/redefineCFLH/JvmtiTestr";
56
57 void JNICALL
58 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
59 if (printdump == JNI_TRUE) {
60 printf("VMInit event received\n");
61 }
62 }
63
64 void JNICALL
106
107 #ifdef STATIC_BUILD
108 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
109 return Agent_Initialize(jvm, options, reserved);
110 }
111 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
112 return Agent_Initialize(jvm, options, reserved);
113 }
114 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
115 return JNI_VERSION_1_8;
116 }
117 #endif
118 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
119 jint res;
120 jvmtiError err;
121
122 if (options != NULL && strcmp(options, "printdump") == 0) {
123 printdump = JNI_TRUE;
124 }
125
126 if ((res = JNI_ENV_PTR(vm)->GetEnv(JNI_ENV_ARG(vm, (void **) &jvmti),
127 JVMTI_VERSION_1_1)) != JNI_OK) {
128 printf("%s: Failed to call GetEnv: error=%d\n", __FILE__, res);
129 return JNI_ERR;
130 }
131
132 err = jvmti->GetPotentialCapabilities(&caps);
133 if (err != JVMTI_ERROR_NONE) {
134 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
135 TranslateError(err), err);
136 return JNI_ERR;
137 }
138
139 err = jvmti->AddCapabilities(&caps);
140 if (err != JVMTI_ERROR_NONE) {
141 printf("(AddCapabilities) unexpected error: %s (%d)\n",
142 TranslateError(err), err);
143 return JNI_ERR;
144 }
145
146 err = jvmti->GetCapabilities(&caps);
147 if (err != JVMTI_ERROR_NONE) {
180 return JNI_OK;
181 }
182
183 JNIEXPORT jint JNICALL
184 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_makeRedefinition(JNIEnv *env,
185 jclass cls, jint fl, jclass redefCls, jbyteArray classBytes) {
186 jvmtiClassDefinition classDef;
187 jvmtiError err;
188
189 if (jvmti == NULL) {
190 printf("JVMTI client was not properly loaded!\n");
191 return STATUS_FAILED;
192 }
193
194 if (!caps.can_redefine_classes) {
195 return PASSED;
196 }
197
198 /* filling the structure jvmtiClassDefinition */
199 classDef.klass = redefCls;
200 classDef.class_byte_count =
201 JNI_ENV_PTR(env)->GetArrayLength(JNI_ENV_ARG(env, classBytes));
202 classDef.class_bytes = (unsigned char *)
203 JNI_ENV_PTR(env)->GetByteArrayElements(JNI_ENV_ARG(env, classBytes),
204 NULL);
205
206 if (fl == 2) {
207 printf(">>>>>>>> Invoke RedefineClasses():\n");
208 printf("\tnew class byte count=%d\n", classDef.class_byte_count);
209 }
210 err = jvmti->RedefineClasses(1, &classDef);
211 if (err != JVMTI_ERROR_NONE) {
212 printf("%s: Failed to call RedefineClasses():\n", __FILE__);
213 printf("\tthe function returned error %d: %s\n",
214 err, TranslateError(err));
215 printf("\tFor more info about this error see the JVMTI spec.\n");
216 return STATUS_FAILED;
217 }
218 if (fl == 2)
219 printf("<<<<<<<< RedefineClasses() is successfully done\n");
220
221 return PASSED;
222 }
223
224 JNIEXPORT jint JNICALL
225 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_GetResult(JNIEnv *env, jclass cls) {
226 return result;
227 }
228
229 #ifdef __cplusplus
230 }
231 #endif
|
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 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.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <jvmti.h>
27 #include "agent_common.h"
28 #include "JVMTITools.h"
29
30 extern "C" {
31
32
33 #define STATUS_FAILED 2
34 #define PASSED 0
35
36 static jvmtiEnv *jvmti = NULL;
37 static jvmtiCapabilities caps;
38 static jvmtiEventCallbacks callbacks;
39 static jint result = PASSED;
40 static jboolean printdump = JNI_FALSE;
41
42 const char* CLASS_NAME = "nsk/jvmti/unit/events/redefineCFLH/JvmtiTestr";
43
44 void JNICALL
45 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
46 if (printdump == JNI_TRUE) {
47 printf("VMInit event received\n");
48 }
49 }
50
51 void JNICALL
93
94 #ifdef STATIC_BUILD
95 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
96 return Agent_Initialize(jvm, options, reserved);
97 }
98 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
99 return Agent_Initialize(jvm, options, reserved);
100 }
101 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
102 return JNI_VERSION_1_8;
103 }
104 #endif
105 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
106 jint res;
107 jvmtiError err;
108
109 if (options != NULL && strcmp(options, "printdump") == 0) {
110 printdump = JNI_TRUE;
111 }
112
113 if ((res = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {
114 printf("%s: Failed to call GetEnv: error=%d\n", __FILE__, res);
115 return JNI_ERR;
116 }
117
118 err = jvmti->GetPotentialCapabilities(&caps);
119 if (err != JVMTI_ERROR_NONE) {
120 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
121 TranslateError(err), err);
122 return JNI_ERR;
123 }
124
125 err = jvmti->AddCapabilities(&caps);
126 if (err != JVMTI_ERROR_NONE) {
127 printf("(AddCapabilities) unexpected error: %s (%d)\n",
128 TranslateError(err), err);
129 return JNI_ERR;
130 }
131
132 err = jvmti->GetCapabilities(&caps);
133 if (err != JVMTI_ERROR_NONE) {
166 return JNI_OK;
167 }
168
169 JNIEXPORT jint JNICALL
170 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_makeRedefinition(JNIEnv *env,
171 jclass cls, jint fl, jclass redefCls, jbyteArray classBytes) {
172 jvmtiClassDefinition classDef;
173 jvmtiError err;
174
175 if (jvmti == NULL) {
176 printf("JVMTI client was not properly loaded!\n");
177 return STATUS_FAILED;
178 }
179
180 if (!caps.can_redefine_classes) {
181 return PASSED;
182 }
183
184 /* filling the structure jvmtiClassDefinition */
185 classDef.klass = redefCls;
186 classDef.class_byte_count = env->GetArrayLength(classBytes);
187 classDef.class_bytes = (unsigned char *) env->GetByteArrayElements(classBytes, NULL);
188
189 if (fl == 2) {
190 printf(">>>>>>>> Invoke RedefineClasses():\n");
191 printf("\tnew class byte count=%d\n", classDef.class_byte_count);
192 }
193 err = jvmti->RedefineClasses(1, &classDef);
194 if (err != JVMTI_ERROR_NONE) {
195 printf("%s: Failed to call RedefineClasses():\n", __FILE__);
196 printf("\tthe function returned error %d: %s\n",
197 err, TranslateError(err));
198 printf("\tFor more info about this error see the JVMTI spec.\n");
199 return STATUS_FAILED;
200 }
201 if (fl == 2)
202 printf("<<<<<<<< RedefineClasses() is successfully done\n");
203
204 return PASSED;
205 }
206
207 JNIEXPORT jint JNICALL
208 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_GetResult(JNIEnv *env, jclass cls) {
209 return result;
210 }
211
212 }
|