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 <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 /*
  34  * Agent receives expected number of ClassPrepare events and finishes work
  35  * (events should be provoked by target application)
  36  */
  37 
  38 #define EXPECTED_EVENTS_NUMBER 500
  39 
  40 static Options* options = NULL;
  41 static const char* agentName;
  42 
  43 static jrawMonitorID eventsCounterMonitor;
  44 
  45 static volatile int eventsCounter = 0;
  46 
  47 void JNICALL classPrepareHandler(
  48         jvmtiEnv *jvmti,
  49         JNIEnv* jni,
  50         jthread thread,
  51         jclass klass) {
  52     int success = 1;
  53     char className[MAX_STRING_LENGTH];
  54     jint loadedClassesCount;
  55     jclass *loadedClasses;
  56 
  57     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
  58         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);
  59         return;
  60     }
  61 
  62     if (!NSK_JVMTI_VERIFY(jvmti->GetLoadedClasses(&loadedClassesCount, &loadedClasses))) {
  63         NSK_COMPLAIN1("%s: failed to get loaded classes\n", agentName);
  64         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);
  65         return;
  66     }
  67 
  68     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)loadedClasses);
  69 
  70     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {
  71 
  72         eventsCounter++;
  73 
  74         NSK_DISPLAY3("%s: ClassPrepare event received for class '%s' (eventsCounter: %d)\n", agentName, className, eventsCounter);
  75 
  76         if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
  77             NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
  78 
  79             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, success, jvmti, jni);
  80         }
  81 
  82         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {
  83             success = 0;
  84         }
  85     } else {
  86         success = 0;
  87     }
  88 
  89     if (!success) {
  90         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);
  91     }
  92 }
  93 
  94 #ifdef STATIC_BUILD
  95 JNIEXPORT jint JNI_OnLoad_attach045Agent01(JavaVM *jvm, char *options, void *reserved) {
  96     return JNI_VERSION_1_8;
  97 }
  98 #endif
  99 
 100 JNIEXPORT jint JNICALL
 101 #ifdef STATIC_BUILD
 102 Agent_OnAttach_attach045Agent01(JavaVM *vm, char *optionsString, void *reserved)
 103 #else
 104 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 105 #endif
 106 {
 107     jvmtiEventCallbacks eventCallbacks;
 108     jvmtiEnv* jvmti;
 109     JNIEnv* jni;
 110 
 111     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 112         return JNI_ERR;
 113 
 114     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 115 
 116     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 117         return JNI_ERR;
 118 
 119     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 120         return JNI_ERR;
 121 
 122     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent01-eventsCounterMonitor", &eventsCounterMonitor))) {
 123         return JNI_ERR;
 124     }
 125 
 126     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 127     eventCallbacks.ClassPrepare = classPrepareHandler;
 128     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 129         return JNI_ERR;
 130     }
 131 
 132     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_PREPARE))) {
 133         return JNI_ERR;
 134     }
 135 
 136     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 137 
 138     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 139         return JNI_ERR;
 140 
 141     return JNI_OK;
 142 }
 143 
 144 
 145 }