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  * Expected agent work scenario:
  35  * - from Agent_OnAttach function start auxiliary thread waiting on 'gcFinishMonitor'
  36  * - receive GarbageCollectionStart event
  37  * - receive GarbageCollectionFinish event, notify 'gcFinishMonitor'
  38  * - notified auxiliary thread calls function 'nsk_aod_agentFinished' and agent completes work
  39  * (such schema is used because of agent can't call nsk_aod_agentFinished from GarbageCollectionFinish handler,
  40  * nsk_aod_agentFinished calls JNI functions and it is prohibited in GarbageCollectionFinish handler)
  41  *
  42  */
  43 
  44 static Options* options = NULL;
  45 static const char* agentName;
  46 
  47 static jvmtiEvent testEvents[] = {JVMTI_EVENT_GARBAGE_COLLECTION_START, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH};
  48 static const int testEventsNumber = 2;
  49 
  50 static jrawMonitorID gcFinishMonitor;
  51 
  52 static volatile int gcStartEventReceived = 0;
  53 
  54 static volatile int gcFinishEventReceived = 0;
  55 
  56 static volatile int success = 1;
  57 
  58 void JNICALL garbageCollectionStartHandler(jvmtiEnv *jvmti) {
  59     NSK_DISPLAY1("%s: GC start event received\n", agentName);
  60 
  61     gcStartEventReceived = 1;
  62 }
  63 
  64 void JNICALL garbageCollectionFinishHandler(jvmtiEnv *jvmti) {
  65     int auxiliaryThreadNotified = 0;
  66 
  67     NSK_DISPLAY1("%s: GC finish event received\n", agentName);
  68 
  69     if (!gcStartEventReceived) {
  70         NSK_COMPLAIN1("%s: GC start event wasn't received before GC finish event\n", agentName);
  71         success = 0;
  72     }
  73 
  74     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {
  75 
  76         gcFinishEventReceived = 1;
  77 
  78         if (NSK_JVMTI_VERIFY(jvmti->RawMonitorNotify(gcFinishMonitor))) {
  79             auxiliaryThreadNotified = 1;
  80         }
  81 
  82         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {
  83             success = 0;
  84         }
  85     } else {
  86         success = 0;
  87     }
  88 
  89     if (!auxiliaryThreadNotified) {
  90         NSK_COMPLAIN1("%s: Error happenned during auxiliary thread notification, test may hang\n", agentName);
  91     }
  92 }
  93 
  94 void JNICALL auxiliaryThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  95     NSK_DISPLAY1("%s: auxiliary thread is running\n", agentName);
  96 
  97     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(gcFinishMonitor))) {
  98 
  99         if (!gcFinishEventReceived) {
 100             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorWait(gcFinishMonitor, 0))) {
 101                 success = 0;
 102             }
 103         }
 104 
 105         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(gcFinishMonitor))) {
 106             success = 0;
 107         }
 108     } else {
 109         success = 0;
 110     }
 111 
 112     nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
 113 }
 114 
 115 int startAuxiliaryThread(jvmtiEnv* jvmti, JNIEnv* jni) {
 116     jthread thread;
 117 
 118     if (!NSK_VERIFY((thread = nsk_jvmti_aod_createThread(jni)) != NULL))
 119         return NSK_FALSE;
 120 
 121     if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, auxiliaryThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY))) {
 122         return NSK_FALSE;
 123     }
 124 
 125     NSK_DISPLAY1("%s: auxiliary thread was started\n", agentName);
 126 
 127     return NSK_TRUE;
 128 }
 129 
 130 #ifdef STATIC_BUILD
 131 JNIEXPORT jint JNI_OnLoad_attach020Agent00(JavaVM *jvm, char *options, void *reserved) {
 132     return JNI_VERSION_1_8;
 133 }
 134 #endif
 135 
 136 JNIEXPORT jint JNICALL
 137 #ifdef STATIC_BUILD
 138 Agent_OnAttach_attach020Agent00(JavaVM *vm, char *optionsString, void *reserved)
 139 #else
 140 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 141 #endif
 142 {
 143     jvmtiEnv* jvmti;
 144     jvmtiEventCallbacks eventCallbacks;
 145     jvmtiCapabilities caps;
 146     JNIEnv* jni;
 147 
 148     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 149         return JNI_ERR;
 150 
 151     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 152 
 153     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 154         return JNI_ERR;
 155 
 156     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 157         return JNI_ERR;
 158 
 159     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("GCFinishMonitor", &gcFinishMonitor))) {
 160         return JNI_ERR;
 161     }
 162 
 163     if (!NSK_VERIFY(startAuxiliaryThread(jvmti, jni)))
 164         return JNI_ERR;
 165 
 166     memset(&caps, 0, sizeof(caps));
 167     caps.can_generate_garbage_collection_events = 1;
 168     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 169         return JNI_ERR;
 170     }
 171 
 172     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 173     eventCallbacks.GarbageCollectionStart  = garbageCollectionStartHandler;
 174     eventCallbacks.GarbageCollectionFinish = garbageCollectionFinishHandler;
 175     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 176         return JNI_ERR;
 177     }
 178 
 179     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 180         return JNI_ERR;
 181     }
 182 
 183     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 184 
 185     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 186         return JNI_ERR;
 187 
 188     return JNI_OK;
 189 }
 190 }