1 /*
2 * Copyright (c) 2004, 2018, Oracle and/or its affiliates. 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 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 <stdlib.h>
25 #include <string.h>
26 #include "jni_tools.h"
27 #include "jvmti_tools.h"
28 #include "Injector.h"
29 #include "agent_common.h"
30
31 #define PASSED 0
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /* ========================================================================== */
38
39 #define DEFAULT_MAX_NUMBER_OF_CLASSES 100
40 #define DEFAULT_NUMBER_OF_SAMPLES 10
41 #define DEFAULT_SAMPLING_INTERVAL 100
42 #define DEFAULT_PACKAGE_NAME "nsk/jvmti/scenarios/hotswap"
43 #define PROFILE_CLASS_NAME "nsk/share/jvmti/ProfileCollector"
44
45 enum {
46 VM_MODE_COMPILED = 0,
47 VM_MODE_INTERPRETED = 1,
48 VM_MODE_MIXED = 2
49 };
50
51 /* scaffold objects */
52 static jlong timeout = 0;
53
54 /* test options */
55 static int number_of_samples;
56 static jlong sampling_interval;
57 static const char* package_name;
58 static size_t package_name_length;
59 static int vm_mode = VM_MODE_COMPILED;
60 static int bci_mode = BCI_MODE_EMCP;
61 static int sync_freq = 0;
62
63 static jclass profile_klass = NULL;
64 static jfieldID count_field = NULL;
65
66 /* test objects */
67 static int max_classes;
68 static char** names = NULL;
69 static jvmtiClassDefinition* old_class_def = NULL;
70 static jvmtiClassDefinition* new_class_def = NULL;
71 static int classCount = 0;
72 static int newFlag = NSK_FALSE;
73
74 /* ========================================================================== */
75
redefine(jvmtiEnv * jvmti,jvmtiClassDefinition * class_def)76 static int redefine(jvmtiEnv* jvmti, jvmtiClassDefinition* class_def) {
77
78 if (!NSK_VERIFY(classCount != 0))
79 return NSK_FALSE;
80
81 NSK_DISPLAY1("Redefining %d classes...\n", classCount);
82
83 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(RedefineClasses,
84 jvmti, classCount, class_def)))
85 return NSK_FALSE;
86
87 return NSK_TRUE;
88 }
89
90 /* ========================================================================== */
91
92 /** callback functions **/
93
94 static void JNICALL
ClassFileLoadHook(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jclass class_being_redefined,jobject loader,const char * name,jobject protection_domain,jint class_data_len,const unsigned char * class_data,jint * new_class_data_len,unsigned char ** new_class_data)95 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
96 jclass class_being_redefined, jobject loader,
97 const char* name, jobject protection_domain,
98 jint class_data_len, const unsigned char* class_data,
99 jint *new_class_data_len, unsigned char** new_class_data) {
100 jint name_len;
101
102 if (name != NULL && classCount < max_classes &&
103 class_being_redefined == NULL &&
104 (strcmp(name, PROFILE_CLASS_NAME) != 0) &&
105 (strncmp(name, package_name, package_name_length) == 0)) {
106 NSK_DISPLAY1("ClassFileLoadHook: %s\n", name);
107 name_len = (jint) strlen(name) + 1;
108 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate, jvmti_env,
109 name_len, (unsigned char**) &names[classCount]))) {
110 nsk_jvmti_setFailStatus();
111 return;
112 }
113 memcpy(names[classCount], name, name_len);
114 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate,
115 jvmti_env, class_data_len, (unsigned char**)
116 &old_class_def[classCount].class_bytes))) {
117 nsk_jvmti_setFailStatus();
118 return;
119 }
120 memcpy((unsigned char*) old_class_def[classCount].class_bytes,
121 class_data, class_data_len);
122 old_class_def[classCount].class_byte_count = class_data_len;
123 classCount++;
124 }
125 }
126
127 static int CompiledMethodLoadEventsCount = 0;
128
129 static void JNICALL
CompiledMethodLoad(jvmtiEnv * jvmti_env,jmethodID method,jint code_size,const void * code_addr,jint map_length,const jvmtiAddrLocationMap * map,const void * compile_info)130 CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method,
131 jint code_size, const void* code_addr, jint map_length,
132 const jvmtiAddrLocationMap* map, const void* compile_info) {
133 char *name = NULL;
134 char *signature = NULL;
135
136 CompiledMethodLoadEventsCount++;
137
138 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
139 jvmti_env, method, &name, &signature, NULL))) {
140 nsk_jvmti_setFailStatus();
141 return;
142 }
143 NSK_DISPLAY3("CompiledMethodLoad event: %s%s (0x%p)\n",
144 name, signature, code_addr);
145 if (name != NULL)
146 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
147 if (signature != NULL)
148 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
149 }
150
151 static int SingleStepEventsCount = 0;
152
153 static void JNICALL
SingleStep(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location)154 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
155 jmethodID method, jlocation location) {
156
157 SingleStepEventsCount++;
158 }
159
160 static int ExceptionEventsCount = 0;
161
162 static void JNICALL
Exception(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jobject exception,jmethodID catch_method,jlocation catch_location)163 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
164 jmethodID method, jlocation location, jobject exception,
165 jmethodID catch_method, jlocation catch_location) {
166
167 if (sync_freq && ((ExceptionEventsCount % sync_freq) == 0)) {
168
169 if (nsk_getVerboseMode()) {
170 jclass klass = NULL;
171 char *signature = NULL;
172
173 if (!NSK_JNI_VERIFY(jni_env, (klass =
174 NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) {
175 nsk_jvmti_setFailStatus();
176 return;
177 }
178 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env,
179 klass, &signature, NULL))) {
180 nsk_jvmti_setFailStatus();
181 return;
182 }
183 NSK_DISPLAY2("Exception event %d: %s\n",
184 ExceptionEventsCount, signature);
185 if (signature != NULL)
186 NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
187 }
188
189 if (!redefine(jvmti_env, (bci_mode != BCI_MODE_EMCP && newFlag) ?
190 new_class_def : old_class_def))
191 nsk_jvmti_setFailStatus();
192
193 NSK_DISPLAY1("SingleStepEventsCount: %d\n", SingleStepEventsCount);
194 if (vm_mode == VM_MODE_MIXED) {
195 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
196 jvmti_env, ((newFlag) ? JVMTI_DISABLE : JVMTI_ENABLE),
197 JVMTI_EVENT_SINGLE_STEP, NULL)))
198 nsk_jvmti_setFailStatus();
199 }
200
201 if (nsk_getVerboseMode() && bci_mode != BCI_MODE_EMCP) {
202 jint profileCount = NSK_CPP_STUB3(GetStaticIntField, jni_env,
203 profile_klass, count_field);
204 NSK_DISPLAY1("profileCount: %d\n", profileCount);
205 }
206
207 newFlag = (newFlag) ? NSK_FALSE : NSK_TRUE;
208 }
209
210 ExceptionEventsCount++;
211 }
212
213 /* ========================================================================== */
214
215 static jrawMonitorID waitLock = NULL;
216
prepare(jvmtiEnv * jvmti,JNIEnv * jni)217 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
218 int i;
219
220 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
221 jvmti, JVMTI_DISABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
222 return NSK_FALSE;
223
224 if (vm_mode != VM_MODE_COMPILED) {
225 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
226 jvmti, JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
227 return NSK_FALSE;
228 }
229
230 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor,
231 jvmti, "waitLock", &waitLock)))
232 return NSK_FALSE;
233
234 for (i = 0; i < classCount; i++) {
235 NSK_DISPLAY1("Find class: %s\n", names[i]);
236 if (!NSK_JNI_VERIFY(jni, (old_class_def[i].klass =
237 NSK_CPP_STUB2(FindClass, jni, names[i])) != NULL))
238 return NSK_FALSE;
239
240 if (!NSK_JNI_VERIFY(jni, (old_class_def[i].klass =
241 NSK_CPP_STUB2(NewGlobalRef, jni,
242 old_class_def[i].klass)) != NULL))
243 return NSK_FALSE;
244 }
245
246 if (bci_mode != BCI_MODE_EMCP) {
247 NSK_DISPLAY1("Find class: %s\n", PROFILE_CLASS_NAME);
248 if (!NSK_JNI_VERIFY(jni, (profile_klass =
249 NSK_CPP_STUB2(FindClass, jni, PROFILE_CLASS_NAME)) != NULL))
250 return NSK_FALSE;
251
252 if (!NSK_JNI_VERIFY(jni, (profile_klass =
253 NSK_CPP_STUB2(NewGlobalRef, jni, profile_klass)) != NULL))
254 return NSK_FALSE;
255
256 if (!NSK_JNI_VERIFY(jni, (count_field =
257 NSK_CPP_STUB4(GetStaticFieldID, jni, profile_klass,
258 (bci_mode == BCI_MODE_CALL) ? "callCount" : "allocCount",
259 "I")) != NULL))
260 return NSK_FALSE;
261
262 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate, jvmti,
263 classCount * sizeof(jvmtiClassDefinition),
264 (unsigned char**) &new_class_def)))
265 return NSK_FALSE;
266
267 for (i = 0; i < classCount; i++) {
268 new_class_def[i].klass = old_class_def[i].klass;
269 if (!Inject(old_class_def[i].class_bytes,
270 old_class_def[i].class_byte_count,
271 (unsigned char**) &new_class_def[i].class_bytes,
272 &new_class_def[i].class_byte_count, bci_mode))
273 return NSK_FALSE;
274 }
275 }
276
277 if (sync_freq) {
278 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
279 jvmti, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
280 return NSK_FALSE;
281 }
282
283 return NSK_TRUE;
284 }
285
286 /* ========================================================================== */
287
wait_for(jvmtiEnv * jvmti,jlong millis)288 static int wait_for(jvmtiEnv* jvmti, jlong millis) {
289
290 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, waitLock)))
291 return NSK_FALSE;
292
293 if (!NSK_JVMTI_VERIFY(
294 NSK_CPP_STUB3(RawMonitorWait, jvmti, waitLock, millis)))
295 nsk_jvmti_setFailStatus();
296
297 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, waitLock)))
298 return NSK_FALSE;
299
300 return NSK_TRUE;
301 }
302
303 /* ========================================================================== */
304
305 /** Agent algorithm. */
306 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)307 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
308 int i;
309
310 if (!nsk_jvmti_waitForSync(timeout))
311 return;
312
313 if (!prepare(jvmti, jni)) {
314 nsk_jvmti_setFailStatus();
315 return;
316 }
317
318 /* resume debugee and wait for sync */
319 if (!nsk_jvmti_resumeSync())
320 return;
321 if (!nsk_jvmti_waitForSync(timeout))
322 return;
323
324 if (sync_freq) {
325 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
326 jvmti, JVMTI_DISABLE, JVMTI_EVENT_EXCEPTION, NULL)))
327 nsk_jvmti_setFailStatus();
328 } else {
329
330 for (i = 0; i < number_of_samples && !nsk_jvmti_isFailStatus(); i++) {
331 wait_for(jvmti, sampling_interval);
332
333 if (!redefine(jvmti, (bci_mode != BCI_MODE_EMCP && newFlag) ?
334 new_class_def : old_class_def))
335 nsk_jvmti_setFailStatus();
336
337 NSK_DISPLAY1("SingleStepEventsCount: %d\n", SingleStepEventsCount);
338 if (vm_mode == VM_MODE_MIXED) {
339 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
340 jvmti, (((i % 2)==0) ? JVMTI_DISABLE : JVMTI_ENABLE),
341 JVMTI_EVENT_SINGLE_STEP, NULL)))
342 nsk_jvmti_setFailStatus();
343 }
344
345 if (nsk_getVerboseMode() && bci_mode != BCI_MODE_EMCP) {
346 jint profileCount = NSK_CPP_STUB3(GetStaticIntField, jni,
347 profile_klass, count_field);
348 NSK_DISPLAY1("profileCount: %d\n", profileCount);
349 }
350
351 newFlag = (newFlag) ? NSK_FALSE : NSK_TRUE;
352 }
353
354 }
355
356 if (vm_mode != VM_MODE_COMPILED) {
357 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
358 jvmti, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
359 nsk_jvmti_setFailStatus();
360 }
361
362 if (!nsk_jvmti_resumeSync())
363 return;
364 }
365
366 /* ========================================================================== */
367
368 /** Agent library initialization. */
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)369 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
370 jvmtiEnv* jvmti = NULL;
371 jvmtiCapabilities caps;
372 jvmtiEventCallbacks callbacks;
373 const char* optValue;
374
375 NSK_DISPLAY0("Agent_OnLoad\n");
376
377 /* init framework and parse options */
378 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
379 return JNI_ERR;
380
381 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
382
383 /* get options */
384 number_of_samples = nsk_jvmti_findOptionIntValue("samples",
385 DEFAULT_NUMBER_OF_SAMPLES);
386 if (!NSK_VERIFY(number_of_samples > 0))
387 return JNI_ERR;
388 NSK_DISPLAY1("samples: %d\n", number_of_samples);
389
390 sampling_interval = nsk_jvmti_findOptionIntValue("interval",
391 DEFAULT_SAMPLING_INTERVAL);
392 if (!NSK_VERIFY(sampling_interval > 0))
393 return JNI_ERR;
394 NSK_DISPLAY1("interval: %d\n", sampling_interval);
395
396 package_name = nsk_jvmti_findOptionStringValue("package",
397 DEFAULT_PACKAGE_NAME);
398 if (!NSK_VERIFY(package_name != NULL))
399 return JNI_ERR;
400 NSK_DISPLAY1("package: %s\n", package_name);
401
402 package_name_length = strlen(package_name);
403 if (!NSK_VERIFY(package_name_length > 0))
404 return JNI_ERR;
405
406 max_classes = nsk_jvmti_findOptionIntValue("classes",
407 DEFAULT_MAX_NUMBER_OF_CLASSES);
408 if (!NSK_VERIFY(max_classes > 0))
409 return JNI_ERR;
410 NSK_DISPLAY1("classes: %d\n", max_classes);
411
412 optValue = nsk_jvmti_findOptionValue("mode");
413 if (optValue != NULL) {
414 if (strcmp(optValue, "compiled") == 0)
415 vm_mode = VM_MODE_COMPILED;
416 else if (strcmp(optValue, "interpreted") == 0)
417 vm_mode = VM_MODE_INTERPRETED;
418 else if (strcmp(optValue, "mixed") == 0)
419 vm_mode = VM_MODE_MIXED;
420 else {
421 NSK_COMPLAIN1("Unknown option value: mode=%s\n", optValue);
422 return JNI_ERR;
423 }
424 }
425
426 optValue = nsk_jvmti_findOptionValue("bci");
427 if (optValue != NULL) {
428 if (strcmp(optValue, "emcp") == 0)
429 bci_mode = BCI_MODE_EMCP;
430 else if (strcmp(optValue, "call") == 0)
431 bci_mode = BCI_MODE_CALL;
432 else if (strcmp(optValue, "alloc") == 0)
433 bci_mode = BCI_MODE_ALLOC;
434 else {
435 NSK_COMPLAIN1("Unknown option value: bci=%s\n", optValue);
436 return JNI_ERR;
437 }
438 }
439
440 sync_freq = nsk_jvmti_findOptionIntValue("sync", 0);
441 if (!NSK_VERIFY(sync_freq >= 0))
442 return JNI_ERR;
443 NSK_DISPLAY1("sync: %d\n", sync_freq);
444
445 /* create JVMTI environment */
446 if (!NSK_VERIFY((jvmti =
447 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
448 return JNI_ERR;
449
450 /* allocate tables for classes */
451 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate, jvmti,
452 max_classes * sizeof(char*), (unsigned char**) &names)))
453 return JNI_ERR;
454
455 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate, jvmti,
456 max_classes * sizeof(jvmtiClassDefinition),
457 (unsigned char**) &old_class_def)))
458 return JNI_ERR;
459
460 /* add capabilities */
461 memset(&caps, 0, sizeof(caps));
462 caps.can_redefine_classes = 1;
463 caps.can_generate_compiled_method_load_events = 1;
464 if (vm_mode != VM_MODE_COMPILED) {
465 caps.can_generate_single_step_events = 1;
466 }
467 if (sync_freq) {
468 caps.can_generate_exception_events = 1;
469 }
470 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
471 return JNI_ERR;
472
473 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
474 return JNI_ERR;
475
476 /* set event callbacks */
477 memset(&callbacks, 0, sizeof(callbacks));
478 callbacks.ClassFileLoadHook = &ClassFileLoadHook;
479 callbacks.CompiledMethodLoad = &CompiledMethodLoad;
480 if (vm_mode != VM_MODE_COMPILED) {
481 callbacks.SingleStep = &SingleStep;
482 }
483 if (sync_freq) {
484 callbacks.Exception = &Exception;
485 }
486 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
487 jvmti, &callbacks, sizeof(callbacks))))
488 return JNI_ERR;
489
490 /* enable events */
491 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
492 jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
493 return JNI_ERR;
494 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
495 jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))
496 return JNI_ERR;
497
498 return JNI_OK;
499 }
500
501 /* ========================================================================== */
502
503 #ifdef __cplusplus
504 }
505 #endif
506