1 /* 2 * Copyright (c) 2007, 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 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <jvmti.h> 27 #include "agent_common.h" 28 #include <jni.h> 29 #include "jvmti_tools.h" 30 #include "jni_tools.h" 31 #include "JVMTITools.h" 32 33 extern "C" { 34 35 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS204/hs204t003/MyThread" 36 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS204/hs204t003/MyThread;" 37 #define FIELDNAME "intState" 38 #define TYPE "I" 39 40 static jint redefineNumber; 41 static jvmtiEnv * jvmti; 42 static jclass watchFieldClass; 43 44 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env, 45 JNIEnv* jni, 46 jthread thread, 47 jclass klass) { 48 char * className; 49 char * generic; 50 51 className = NULL; 52 generic = NULL; 53 redefineNumber=0; 54 if ( !NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &className, &generic)) ) { 55 NSK_DISPLAY0(" Agent :: Failed get class signature.\n"); 56 nsk_jvmti_agentFailed(); 57 } else { 58 if( (strcmp(className, CLASS_NAME) == 0 ) ) { 59 jfieldID fieldId; 60 if ( ! NSK_JNI_VERIFY(jni, (fieldId = jni->GetStaticFieldID(klass, FIELDNAME, TYPE) ) != NULL ) ) { 61 NSK_DISPLAY0(" Agent :: Failed to get FieldId.\n"); 62 nsk_jvmti_agentFailed(); 63 } else { 64 if ( ! NSK_JVMTI_VERIFY(jvmti_env->SetFieldAccessWatch(klass, fieldId) ) ) { 65 NSK_DISPLAY0(" Agent :: Failed to set watch point on a field.\n"); 66 nsk_jvmti_agentFailed(); 67 } else { 68 nsk_jvmti_enableNotification(jvmti_env, JVMTI_EVENT_FIELD_ACCESS, NULL); 69 if (! NSK_JNI_VERIFY(jni, 70 ( watchFieldClass = (jclass) 71 jni->NewGlobalRef(klass) ) 72 != NULL ) ) { 73 NSK_DISPLAY0(" Agent :: Failed to get global reference for class.\n"); 74 nsk_jvmti_agentFailed(); 75 } 76 NSK_DISPLAY0(" Agent :: SetFieldAccessWatch.\n"); 77 } 78 } 79 NSK_DISPLAY1(" Agent :: Leaving callbackClassPrepare for class = %s .\n", className); 80 } 81 } 82 83 if ( className != NULL ) { 84 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) { 85 NSK_DISPLAY1(" Agent :: #error failed to Deallocate className = %s.", className); 86 nsk_jvmti_agentFailed(); 87 } 88 } 89 90 if ( generic != NULL ) { 91 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)generic))) { 92 NSK_DISPLAY1(" Agent :: #error failed to Deallocate class signature = %s.", generic); 93 nsk_jvmti_agentFailed(); 94 } 95 } 96 return; 97 } 98 99 JNIEXPORT void JNICALL callbackFieldAccess(jvmtiEnv *jvmti_env, 100 JNIEnv* jni, 101 jthread thread, 102 jmethodID method, 103 jlocation location, 104 jclass field_klass, 105 jobject object, 106 jfieldID field) { 107 char fileName[512]; 108 char * className; 109 char * generic; 110 111 className = NULL; 112 generic = NULL; 113 if (redefineNumber != 0 ) { 114 return; 115 } 116 if ( ! NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(field_klass, &className, &generic)) ) { 117 NSK_DISPLAY0(" Agent :: Failed get class signature.\n"); 118 nsk_jvmti_agentFailed(); 119 } else { 120 if( (strcmp(className, CLASS_NAME) == 0 ) ) { 121 jvmtiThreadInfo info; 122 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName, 123 sizeof(fileName)/sizeof(char)); 124 if ( nsk_jvmti_redefineClass(jvmti_env, field_klass, fileName) == NSK_TRUE ) { 125 NSK_DISPLAY0(" Agent :: Successfully redefined.\n"); 126 redefineNumber++; 127 } else { 128 NSK_DISPLAY0(" Agent :: Failed to redefine.\n"); 129 nsk_jvmti_agentFailed(); 130 } 131 NSK_DISPLAY0(" Agent :: Before attempting thread suspend.\n"); 132 if ( ! NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(thread, &info))) { 133 NSK_DISPLAY0(" Agent :: error getting thread info "); 134 nsk_jvmti_agentFailed(); 135 } else { 136 NSK_DISPLAY1(" Agent :: Thread Name = %s .\n", info.name); 137 } 138 if ( ! NSK_JVMTI_VERIFY(jvmti_env->SuspendThread(thread))) { 139 NSK_DISPLAY0(" Agent :: Failed to suspend thread.\n"); 140 nsk_jvmti_agentFailed(); 141 } 142 } 143 } 144 145 if ( className != NULL ) { 146 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)className))) { 147 NSK_DISPLAY1(" Agent :: #error failed to Deallocate className = %s.", className); 148 nsk_jvmti_agentFailed(); 149 } 150 } 151 152 if ( generic != NULL ) { 153 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char *)generic))) { 154 NSK_DISPLAY1(" Agent :: #error failed to Deallocate class signature = %s.", generic); 155 nsk_jvmti_agentFailed(); 156 } 157 } 158 } 159 160 #ifdef STATIC_BUILD 161 JNIEXPORT jint JNICALL Agent_OnLoad_hs204t003(JavaVM *jvm, char *options, void *reserved) { 162 return Agent_Initialize(jvm, options, reserved); 163 } 164 JNIEXPORT jint JNICALL Agent_OnAttach_hs204t003(JavaVM *jvm, char *options, void *reserved) { 165 return Agent_Initialize(jvm, options, reserved); 166 } 167 JNIEXPORT jint JNI_OnLoad_hs204t003(JavaVM *jvm, char *options, void *reserved) { 168 return JNI_VERSION_1_8; 169 } 170 #endif 171 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) { 172 if ( ! NSK_VERIFY ( JNI_OK == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1) ) ) { 173 NSK_DISPLAY0("Agent :: Could not load JVMTI interface \n"); 174 return JNI_ERR; 175 } else { 176 jvmtiCapabilities caps; 177 jvmtiEventCallbacks eventCallbacks; 178 memset(&caps, 0, sizeof(caps)); 179 if (!nsk_jvmti_parseOptions(options)) { 180 NSK_DISPLAY0(" NSK Failed to parse.."); 181 return JNI_ERR; 182 } 183 caps.can_redefine_classes = 1; 184 caps.can_generate_field_access_events = 1; 185 caps.can_pop_frame = 1; 186 caps.can_suspend = 1; 187 if ( ! NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)) ) { 188 NSK_DISPLAY0(" Agent :: Failed add required capabilities\n."); 189 return JNI_ERR; 190 } 191 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 192 eventCallbacks.ClassPrepare = callbackClassPrepare; 193 eventCallbacks.FieldAccess = callbackFieldAccess; 194 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks) ) ) ) { 195 NSK_DISPLAY0(" Agent :: Error occured while setting event call back \n"); 196 return JNI_ERR; 197 } 198 nsk_jvmti_enableNotification(jvmti, JVMTI_EVENT_CLASS_PREPARE, NULL); 199 } 200 return JNI_OK; 201 } 202 203 JNIEXPORT jboolean JNICALL 204 Java_nsk_jvmti_scenarios_hotswap_HS204_hs204t003_hs204t003_popFrame(JNIEnv * jni, 205 jobject object, 206 jthread thread) { 207 jboolean retvalue; 208 jint state; 209 retvalue = JNI_FALSE; 210 if (! NSK_JVMTI_VERIFY(jvmti->GetThreadState(thread, &state))) { 211 NSK_DISPLAY0(" Agent :: Error getting thread state.\n"); 212 nsk_jvmti_agentFailed(); 213 } else { 214 if ( state & JVMTI_THREAD_STATE_SUSPENDED) { 215 NSK_DISPLAY0(" Agent :: Thread state = JVMTI_THREAD_STATE_SUSPENDED.\n"); 216 if ( ! NSK_JVMTI_VERIFY ( jvmti->PopFrame(thread) ) ) { 217 NSK_DISPLAY0("#error Agent :: Jvmti failed to do popFrame.\n"); 218 nsk_jvmti_agentFailed(); 219 } else { 220 if ( ! NSK_JVMTI_VERIFY ( jvmti->ResumeThread(thread) ) ) { 221 NSK_DISPLAY0(" Agent :: Error occured in resuming a thread.\n"); 222 nsk_jvmti_agentFailed(); 223 } else { 224 jfieldID fieldId = jni->GetStaticFieldID(watchFieldClass, FIELDNAME, TYPE); 225 if ( ! NSK_JNI_VERIFY(jni, fieldId != NULL ) ) { 226 NSK_DISPLAY0(" Agent :: Failed to get FieldId before droping watchers.\n"); 227 nsk_jvmti_agentFailed(); 228 } else { 229 if ( ! NSK_JVMTI_VERIFY ( jvmti->ClearFieldAccessWatch(watchFieldClass, fieldId) ) ) { 230 NSK_DISPLAY0(" Agent :: failed to drop field watces.\n"); 231 nsk_jvmti_agentFailed(); 232 } else { 233 NSK_DISPLAY0(" Agent :: Sucessfully droped watches.\n"); 234 retvalue = JNI_TRUE; 235 } 236 } 237 } 238 } 239 } else { 240 NSK_DISPLAY0(" Agent :: Thread should be suspended to its pop frame.\n"); 241 } 242 } 243 return retvalue; 244 } 245 }