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 <string.h> 26 #include "jvmti.h" 27 #include "agent_common.h" 28 29 extern "C" { 30 31 32 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;} 33 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; } 34 35 #define THREADS_LIMIT 8 36 37 jrawMonitorID access_lock; 38 jvmtiEnv *jvmti; 39 jint iGlobalStatus = 0; 40 jthread susp_thrd[THREADS_LIMIT]; 41 static jvmtiEventCallbacks callbacks; 42 static jvmtiCapabilities jvmti_caps; 43 44 45 int printdump = 0; 46 int gc_start_count =0; 47 int gc_finish_count =0; 48 49 50 void debug_printf(const char *fmt, ...) { 51 va_list args; 52 53 va_start(args, fmt); 54 if (printdump) { 55 vprintf(fmt, args); 56 } 57 va_end(args); 58 } 59 60 61 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) { 62 63 debug_printf("VMInit event done\n"); 64 } 65 66 void JNICALL vmExit(jvmtiEnv *jvmti_env, JNIEnv *env) { 67 debug_printf("------------ JVMTI_EVENT_VM_DEATH ------------\n"); 68 debug_printf("JVMTI_EVENT_GARBAGE_COLLECTION_START count: %d\n",gc_start_count); 69 debug_printf("JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count: %d\n",gc_finish_count); 70 } 71 72 void JNICALL gc_start(jvmtiEnv *jvmti_env) { 73 74 gc_start_count++; 75 debug_printf("Event: JVMTI_EVENT_GC_START\n"); 76 } 77 78 void JNICALL gc_finish(jvmtiEnv *jvmti_env) { 79 80 gc_finish_count++; 81 debug_printf("Event: JVMTI_EVENT_GC_FINISH\n"); 82 } 83 84 85 void init_callbacks() { 86 memset((void *)&callbacks, 0, sizeof(jvmtiEventCallbacks)); 87 callbacks.VMInit = vmInit; 88 callbacks.VMDeath = vmExit; 89 callbacks.GarbageCollectionStart = gc_start; 90 callbacks.GarbageCollectionFinish = gc_finish; 91 } 92 93 94 #ifdef STATIC_BUILD 95 JNIEXPORT jint JNICALL Agent_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) { 96 return Agent_Initialize(jvm, options, reserved); 97 } 98 JNIEXPORT jint JNICALL Agent_OnAttach_gc(JavaVM *jvm, char *options, void *reserved) { 99 return Agent_Initialize(jvm, options, reserved); 100 } 101 JNIEXPORT jint JNI_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) { 102 return JNI_VERSION_1_8; 103 } 104 #endif 105 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) { 106 jint res; 107 108 if (options && strlen(options) > 0) { 109 if (strstr(options, "printdump")) { 110 printdump = 1; 111 } 112 } 113 114 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 115 if (res < 0) { 116 debug_printf("Wrong result of a valid call to GetEnv!\n"); 117 return JNI_ERR; 118 } 119 120 /* Create data access lock */ 121 res = jvmti->CreateRawMonitor("_access_lock", &access_lock); 122 JVMTI_ERROR_CHECK("RawMonitorEnter in monitor_contended_entered failed with error code ", res); 123 124 125 /* Add capabilities */ 126 res = jvmti->GetPotentialCapabilities(&jvmti_caps); 127 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res); 128 129 res = jvmti->AddCapabilities(&jvmti_caps); 130 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res); 131 132 /* Enable events */ 133 init_callbacks(); 134 res = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); 135 JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res); 136 137 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL); 138 JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res); 139 140 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL); 141 JVMTI_ERROR_CHECK("SetEventNotificationMode for vm death event returned error", res); 142 143 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL); 144 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc start returned error", res); 145 146 res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL); 147 JVMTI_ERROR_CHECK("SetEventNotificationMode for gc finish returned error", res); 148 149 return JNI_OK; 150 } 151 152 153 154 155 JNIEXPORT void JNICALL 156 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCStart(JNIEnv * env, jclass cls) { 157 if (gc_start_count == 0) { 158 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_START count = 0\n"); 159 iGlobalStatus = 2; 160 } 161 } 162 163 JNIEXPORT void JNICALL 164 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCFinish(JNIEnv * env, jclass cls) { 165 if (gc_finish_count == 0) { 166 printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count = 0\n"); 167 iGlobalStatus = 2; 168 } 169 } 170 171 JNIEXPORT jint JNICALL 172 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_GetResult(JNIEnv * env, jclass cls) { 173 return iGlobalStatus; 174 } 175 176 177 JNIEXPORT void JNICALL 178 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_jvmtiForceGC (JNIEnv * env, jclass cls) { 179 jvmtiError ret; 180 181 debug_printf("jvmti Force gc requested \n"); 182 ret = jvmti->ForceGarbageCollection(); 183 184 if (ret != JVMTI_ERROR_NONE) { 185 printf("Error: ForceGarbageCollection %d \n", ret); 186 iGlobalStatus = 2; 187 } 188 } 189 190 }