< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/events/EM07/em07t002/em07t002.cpp

Print this page
rev 52050 : [mq]: refactor


  46 const void *plist = NULL;
  47 
  48 typedef struct nsk_jvmti_CompiledMethodIDStruct {
  49     jmethodID method;
  50     const void* code_addr;
  51     char name[NAME_LENGTH];
  52 } nsk_jvmti_CompiledMethod;
  53 
  54 
  55 /* ============================================================================= */
  56 
  57 /* callbacks */
  58 void JNICALL
  59 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
  60                 const void* code_addr, jint map_length,
  61                 const jvmtiAddrLocationMap* map, const void* compile_info) {
  62     char *name;
  63     char *sign;
  64     char *genc;
  65 
  66     if (!NSK_JVMTI_VERIFY(
  67             NSK_CPP_STUB5(
  68                 GetMethodName, jvmti_env, method, &name, &sign, &genc))) {
  69         nsk_jvmti_setFailStatus();
  70         return;
  71     }
  72 
  73     if (!strncmp(name,"javaMethod", 8)) {
  74         nsk_jvmti_CompiledMethod *rec =
  75             (nsk_jvmti_CompiledMethod *)malloc(sizeof(nsk_jvmti_CompiledMethod));
  76 
  77         rec->method = method;
  78         rec->code_addr = code_addr;
  79         strncpy(rec->name, name, NAME_LENGTH);
  80         rec->name[NAME_LENGTH - 1] = '\0';
  81 
  82         if (!NSK_VERIFY(nsk_list_add(plist, rec))) {
  83             nsk_jvmti_setFailStatus();
  84             free((void *)rec);
  85         } else {
  86             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_LOAD received for\n");
  87             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
  88 
  89             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
  90                 nsk_jvmti_setFailStatus();
  91 
  92             methodLoadCount++;
  93 
  94             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
  95                 nsk_jvmti_setFailStatus();
  96 
  97         }
  98     }
  99 
 100     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
 101             jvmti_env, (unsigned char*)name))) {
 102         nsk_jvmti_setFailStatus();
 103     }
 104     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
 105             jvmti_env, (unsigned char*)sign))) {
 106         nsk_jvmti_setFailStatus();
 107     }
 108     if (genc != NULL)
 109         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
 110                 jvmti_env, (unsigned char*)genc))) {
 111             nsk_jvmti_setFailStatus();
 112         }
 113 }
 114 
 115 void JNICALL
 116 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
 117                 const void* code_addr) {
 118 
 119     nsk_jvmti_CompiledMethod *rec;
 120 
 121     int count = nsk_list_getCount(plist);
 122     int i;
 123 
 124     for (i = 0; i < count; i ++) {
 125         rec = (nsk_jvmti_CompiledMethod *)nsk_list_get(plist, i);
 126         if ((rec->code_addr == code_addr) && (rec->method == method)) {
 127             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_UNLOAD received for\n");
 128             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
 129 
 130             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
 131                 nsk_jvmti_setFailStatus();
 132 
 133             methodUnloadCount++;
 134 
 135             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
 136                 nsk_jvmti_setFailStatus();
 137 
 138             free(rec);
 139             nsk_list_remove(plist, i);
 140             return;
 141         }
 142 
 143     }
 144 
 145 }
 146 
 147 /* ============================================================================= */
 148 
 149 static int
 150 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 151     if (!NSK_JVMTI_VERIFY(
 152             NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable,
 153                                             event, NULL))) {
 154         nsk_jvmti_setFailStatus();
 155         return NSK_FALSE;
 156     }
 157 
 158     return NSK_TRUE;
 159 }
 160 
 161 int checkEvents() {
 162 
 163     int result = methodUnloadCount <= methodLoadCount;
 164 
 165     if (result) {
 166         NSK_DISPLAY0("Received correct number of events:\n");
 167         NSK_DISPLAY1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 168                                 methodLoadCount);
 169         NSK_DISPLAY1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 170                                 methodUnloadCount);
 171     } else {
 172         NSK_COMPLAIN0("Received incorrect number of events:\n");
 173         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 174                                 methodLoadCount);
 175         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 176                                 methodUnloadCount);
 177     }
 178 
 179     return result;
 180 }
 181 
 182 /* ============================================================================= */
 183 
 184 static int
 185 setCallBacks() {
 186 
 187     jvmtiEventCallbacks eventCallbacks;
 188 
 189     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 190 
 191     eventCallbacks.CompiledMethodLoad        = cbCompiledMethodLoad;
 192     eventCallbacks.CompiledMethodUnload      = cbCompiledMethodUnload;
 193 
 194     return NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 195                                             &eventCallbacks,
 196                                             sizeof(eventCallbacks)));
 197 }
 198 
 199 /* ============================================================================= */
 200 
 201 /** Agent algorithm. */
 202 static void JNICALL
 203 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 204 
 205     int i;
 206 
 207     int attempts = nsk_jvmti_findOptionIntValue("attempts", 1);
 208 
 209     for (i = 0; i < attempts; i++) {
 210 
 211         if (!nsk_jvmti_waitForSync(timeout))
 212             return;
 213 
 214         if (!checkEvents())
 215             nsk_jvmti_setFailStatus();
 216 
 217         NSK_DISPLAY0("Let debuggee to continue\n");
 218         if (!nsk_jvmti_resumeSync())
 219             return;
 220     }
 221 
 222     {
 223         int count = nsk_list_getCount(plist);
 224 
 225         while (count > 0) {
 226             free((void *)nsk_list_get(plist, 0));
 227             nsk_list_remove(plist, 0);
 228             count = nsk_list_getCount(plist);
 229         }
 230 
 231     }
 232 
 233     if (!NSK_JVMTI_VERIFY(
 234             NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock)))
 235         nsk_jvmti_setFailStatus();
 236 
 237 }
 238 
 239 /* ============================================================================= */
 240 
 241 /** Agent library initialization. */
 242 #ifdef STATIC_BUILD
 243 JNIEXPORT jint JNICALL Agent_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 244     return Agent_Initialize(jvm, options, reserved);
 245 }
 246 JNIEXPORT jint JNICALL Agent_OnAttach_em07t002(JavaVM *jvm, char *options, void *reserved) {
 247     return Agent_Initialize(jvm, options, reserved);
 248 }
 249 JNIEXPORT jint JNI_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 250     return JNI_VERSION_1_8;
 251 }
 252 #endif
 253 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 254 
 255     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 256         return JNI_ERR;
 257 
 258     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 259 
 260     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 261         return JNI_ERR;
 262 
 263     if (!NSK_JVMTI_VERIFY(
 264             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) {
 265         nsk_jvmti_setFailStatus();
 266         return JNI_ERR;
 267     }
 268 
 269     if (!NSK_VERIFY((plist = (const void *)nsk_list_create()) != NULL))
 270         return JNI_ERR;
 271 
 272     {
 273         jvmtiCapabilities caps;
 274         memset(&caps, 0, sizeof(caps));
 275 
 276         caps.can_generate_compiled_method_load_events = 1;
 277         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)))
 278             return JNI_ERR;
 279     }
 280 
 281     if (!setCallBacks()) {
 282         return JNI_ERR;
 283     }
 284 
 285     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD) ||
 286                 !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
 287         return JNI_ERR;
 288     }
 289 
 290     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 291         return JNI_ERR;
 292 
 293     return JNI_OK;
 294 }
 295 
 296 /* ============================================================================= */
 297 


  46 const void *plist = NULL;
  47 
  48 typedef struct nsk_jvmti_CompiledMethodIDStruct {
  49     jmethodID method;
  50     const void* code_addr;
  51     char name[NAME_LENGTH];
  52 } nsk_jvmti_CompiledMethod;
  53 
  54 
  55 /* ============================================================================= */
  56 
  57 /* callbacks */
  58 void JNICALL
  59 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
  60                 const void* code_addr, jint map_length,
  61                 const jvmtiAddrLocationMap* map, const void* compile_info) {
  62     char *name;
  63     char *sign;
  64     char *genc;
  65 
  66     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &sign, &genc))) {


  67         nsk_jvmti_setFailStatus();
  68         return;
  69     }
  70 
  71     if (!strncmp(name,"javaMethod", 8)) {
  72         nsk_jvmti_CompiledMethod *rec =
  73             (nsk_jvmti_CompiledMethod *)malloc(sizeof(nsk_jvmti_CompiledMethod));
  74 
  75         rec->method = method;
  76         rec->code_addr = code_addr;
  77         strncpy(rec->name, name, NAME_LENGTH);
  78         rec->name[NAME_LENGTH - 1] = '\0';
  79 
  80         if (!NSK_VERIFY(nsk_list_add(plist, rec))) {
  81             nsk_jvmti_setFailStatus();
  82             free((void *)rec);
  83         } else {
  84             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_LOAD received for\n");
  85             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
  86 
  87             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
  88                 nsk_jvmti_setFailStatus();
  89 
  90             methodLoadCount++;
  91 
  92             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
  93                 nsk_jvmti_setFailStatus();
  94 
  95         }
  96     }
  97 
  98     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)name))) {

  99         nsk_jvmti_setFailStatus();
 100     }
 101     if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)sign))) {

 102         nsk_jvmti_setFailStatus();
 103     }
 104     if (genc != NULL)
 105         if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)genc))) {

 106             nsk_jvmti_setFailStatus();
 107         }
 108 }
 109 
 110 void JNICALL
 111 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
 112                 const void* code_addr) {
 113 
 114     nsk_jvmti_CompiledMethod *rec;
 115 
 116     int count = nsk_list_getCount(plist);
 117     int i;
 118 
 119     for (i = 0; i < count; i ++) {
 120         rec = (nsk_jvmti_CompiledMethod *)nsk_list_get(plist, i);
 121         if ((rec->code_addr == code_addr) && (rec->method == method)) {
 122             NSK_DISPLAY0(">>>JVMTI_EVENT_COMPILED_METHOD_UNLOAD received for\n");
 123             NSK_DISPLAY1("\t\tmethod: %s\n", rec->name);
 124 
 125             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
 126                 nsk_jvmti_setFailStatus();
 127 
 128             methodUnloadCount++;
 129 
 130             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
 131                 nsk_jvmti_setFailStatus();
 132 
 133             free(rec);
 134             nsk_list_remove(plist, i);
 135             return;
 136         }
 137 
 138     }
 139 
 140 }
 141 
 142 /* ============================================================================= */
 143 
 144 static int
 145 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 146     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(enable, event, NULL))) {


 147         nsk_jvmti_setFailStatus();
 148         return NSK_FALSE;
 149     }
 150 
 151     return NSK_TRUE;
 152 }
 153 
 154 int checkEvents() {
 155 
 156     int result = methodUnloadCount <= methodLoadCount;
 157 
 158     if (result) {
 159         NSK_DISPLAY0("Received correct number of events:\n");
 160         NSK_DISPLAY1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 161                                 methodLoadCount);
 162         NSK_DISPLAY1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 163                                 methodUnloadCount);
 164     } else {
 165         NSK_COMPLAIN0("Received incorrect number of events:\n");
 166         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_LOAD events number = %d\n",
 167                                 methodLoadCount);
 168         NSK_COMPLAIN1("\t\tCOMPILED_METHOD_UNLOAD events number = %d\n",
 169                                 methodUnloadCount);
 170     }
 171 
 172     return result;
 173 }
 174 
 175 /* ============================================================================= */
 176 
 177 static int
 178 setCallBacks() {
 179 
 180     jvmtiEventCallbacks eventCallbacks;
 181 
 182     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 183 
 184     eventCallbacks.CompiledMethodLoad        = cbCompiledMethodLoad;
 185     eventCallbacks.CompiledMethodUnload      = cbCompiledMethodUnload;
 186 
 187     return NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)));


 188 }
 189 
 190 /* ============================================================================= */
 191 
 192 /** Agent algorithm. */
 193 static void JNICALL
 194 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 195 
 196     int i;
 197 
 198     int attempts = nsk_jvmti_findOptionIntValue("attempts", 1);
 199 
 200     for (i = 0; i < attempts; i++) {
 201 
 202         if (!nsk_jvmti_waitForSync(timeout))
 203             return;
 204 
 205         if (!checkEvents())
 206             nsk_jvmti_setFailStatus();
 207 
 208         NSK_DISPLAY0("Let debuggee to continue\n");
 209         if (!nsk_jvmti_resumeSync())
 210             return;
 211     }
 212 
 213     {
 214         int count = nsk_list_getCount(plist);
 215 
 216         while (count > 0) {
 217             free((void *)nsk_list_get(plist, 0));
 218             nsk_list_remove(plist, 0);
 219             count = nsk_list_getCount(plist);
 220         }
 221 
 222     }
 223 
 224     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))

 225         nsk_jvmti_setFailStatus();
 226 
 227 }
 228 
 229 /* ============================================================================= */
 230 
 231 /** Agent library initialization. */
 232 #ifdef STATIC_BUILD
 233 JNIEXPORT jint JNICALL Agent_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 234     return Agent_Initialize(jvm, options, reserved);
 235 }
 236 JNIEXPORT jint JNICALL Agent_OnAttach_em07t002(JavaVM *jvm, char *options, void *reserved) {
 237     return Agent_Initialize(jvm, options, reserved);
 238 }
 239 JNIEXPORT jint JNI_OnLoad_em07t002(JavaVM *jvm, char *options, void *reserved) {
 240     return JNI_VERSION_1_8;
 241 }
 242 #endif
 243 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 244 
 245     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 246         return JNI_ERR;
 247 
 248     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 249 
 250     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 251         return JNI_ERR;
 252 
 253     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {

 254         nsk_jvmti_setFailStatus();
 255         return JNI_ERR;
 256     }
 257 
 258     if (!NSK_VERIFY((plist = (const void *)nsk_list_create()) != NULL))
 259         return JNI_ERR;
 260 
 261     {
 262         jvmtiCapabilities caps;
 263         memset(&caps, 0, sizeof(caps));
 264 
 265         caps.can_generate_compiled_method_load_events = 1;
 266         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 267             return JNI_ERR;
 268     }
 269 
 270     if (!setCallBacks()) {
 271         return JNI_ERR;
 272     }
 273 
 274     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD) ||
 275                 !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
 276         return JNI_ERR;
 277     }
 278 
 279     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 280         return JNI_ERR;
 281 
 282     return JNI_OK;
 283 }
 284 
 285 /* ============================================================================= */
 286 
< prev index next >