1 /* 2 * Copyright (c) 2003, 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 <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <jvmti.h> 28 #include "agent_common.h" 29 30 #include "nsk_tools.h" 31 #include "JVMTITools.h" 32 #include "jvmti_tools.h" 33 34 extern "C" { 35 36 #define STATUS_FAILED 2 37 #define PASSED 0 38 39 static volatile jint result = PASSED; 40 static volatile int gcstart = 0; 41 static volatile int gcfinish = 0; 42 static jvmtiEnv *jvmti = NULL; 43 static jvmtiEventCallbacks callbacks; 44 static jvmtiCapabilities caps; 45 46 /** callback functions **/ 47 void JNICALL 48 GarbageCollectionStart(jvmtiEnv *jvmti_env) { 49 gcstart++; 50 NSK_DISPLAY1("GarbageCollectionStart event #%d received\n", 51 gcstart); 52 53 if (gcstart != (gcfinish+1)) { 54 result = STATUS_FAILED; 55 NSK_COMPLAIN2( 56 "TEST FAILED: GarbageCollectionStart event has no a matched pair GarbageCollectionFinish:\n" 57 "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n", 58 gcstart, gcfinish); 59 } 60 else 61 NSK_DISPLAY0("CHECK PASSED: GarbageCollectionStart event has a matched pair GarbageCollectionFinish as expected\n\n"); 62 } 63 64 void JNICALL 65 GarbageCollectionFinish(jvmtiEnv *jvmti_env) { 66 gcfinish++; 67 NSK_DISPLAY1("GarbageCollectionFinish event #%d received\n", 68 gcfinish); 69 70 if (gcstart != gcfinish) { 71 result = STATUS_FAILED; 72 NSK_COMPLAIN2( 73 "TEST FAILED: GarbageCollectionFinish event has no a matched pair GarbageCollectionStart:\n" 74 "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n", 75 gcstart, gcfinish); 76 } 77 else 78 NSK_DISPLAY0("CHECK PASSED: GarbageCollectionFinish event has a matched pair GarbageCollectionStart as expected\n\n"); 79 } 80 81 void JNICALL 82 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 83 NSK_DISPLAY0("VMDeath event received\n"); 84 85 if (gcstart != gcfinish || result == STATUS_FAILED) { 86 NSK_COMPLAIN2( 87 "TEST FAILED: some GarbageCollectionFinish events have no a matched pair GarbageCollectionStart:\n" 88 "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n", 89 gcstart, gcfinish); 90 91 exit(95 + STATUS_FAILED); 92 } 93 else 94 NSK_DISPLAY0("CHECK PASSED: all GarbageCollectionStart/GarbageCollectionFinish events have a matched pair as expected\n\n"); 95 } 96 97 /************************/ 98 99 #ifdef STATIC_BUILD 100 JNIEXPORT jint JNICALL Agent_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) { 101 return Agent_Initialize(jvm, options, reserved); 102 } 103 JNIEXPORT jint JNICALL Agent_OnAttach_gcstart001(JavaVM *jvm, char *options, void *reserved) { 104 return Agent_Initialize(jvm, options, reserved); 105 } 106 JNIEXPORT jint JNI_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) { 107 return JNI_VERSION_1_8; 108 } 109 #endif 110 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 111 /* init framework and parse options */ 112 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 113 return JNI_ERR; 114 115 /* create JVMTI environment */ 116 if (!NSK_VERIFY((jvmti = 117 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 118 return JNI_ERR; 119 120 /* add capability to generate compiled method events */ 121 memset(&caps, 0, sizeof(jvmtiCapabilities)); 122 caps.can_generate_garbage_collection_events = 1; 123 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, 124 jvmti, &caps))) 125 return JNI_ERR; 126 127 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities, 128 jvmti, &caps))) 129 return JNI_ERR; 130 131 if (!caps.can_generate_garbage_collection_events) 132 NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n"); 133 134 /* set event callback */ 135 NSK_DISPLAY0("setting event callbacks ...\n"); 136 (void) memset(&callbacks, 0, sizeof(callbacks)); 137 callbacks.VMDeath = &VMDeath; 138 callbacks.GarbageCollectionStart = &GarbageCollectionStart; 139 callbacks.GarbageCollectionFinish = &GarbageCollectionFinish; 140 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, 141 jvmti, &callbacks, sizeof(callbacks)))) 142 return JNI_ERR; 143 144 NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n"); 145 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 146 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 147 return JNI_ERR; 148 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 149 jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL))) 150 return JNI_ERR; 151 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 152 jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL))) 153 return JNI_ERR; 154 NSK_DISPLAY0("enabling the events done\n\n"); 155 156 return JNI_OK; 157 } 158 159 }