1 /*
   2  * Copyright (c) 2008, 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 VMObjectAlloc 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 jvmtiEvent testEvents[] = {JVMTI_EVENT_VM_OBJECT_ALLOC};
  44 static const int testEventsNumber = 1;
  45 
  46 static jrawMonitorID eventsCounterMonitor;
  47 
  48 static volatile int eventsCounter = 0;
  49 
  50 void JNICALL
  51 VMObjectAllocHandler(jvmtiEnv *jvmti,
  52         JNIEnv* jni,
  53         jthread thread,
  54         jobject object,
  55         jclass object_klass,
  56         jlong size) {
  57     char threadName[MAX_STRING_LENGTH];
  58     char className[MAX_STRING_LENGTH];
  59     int success = 1;
  60 
  61     if (!nsk_jvmti_aod_getClassName(jvmti, object_klass, className)) {
  62         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  63         return;
  64     }
  65 
  66     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  67         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  68         return;
  69     }
  70 
  71     if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
  72             RawMonitorEnter, jvmti, eventsCounterMonitor))) {
  73 
  74         eventsCounter++;
  75 
  76         NSK_DISPLAY4("%s: VMObjectAlloc received in thread '%s' for instance of '%s' (eventsCounter: %d)\n",
  77                 agentName, threadName, className, eventsCounter);
  78 
  79         if ((eventsCounter % 10) == 0) {
  80             NSK_DISPLAY1("%s: force garbage collection\n", agentName);
  81 
  82             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB1(ForceGarbageCollection, jvmti)))
  83                 success = 0;
  84         }
  85 
  86         if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
  87             NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
  88 
  89             nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
  90         }
  91 
  92         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, eventsCounterMonitor))) {
  93             success = 0;
  94         }
  95     } else {
  96         success = 0;
  97     }
  98 
  99     if (!success) {
 100         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
 101     }
 102 }
 103 
 104 #ifdef STATIC_BUILD
 105 JNIEXPORT jint JNI_OnLoad_attach045Agent03(JavaVM *jvm, char *options, void *reserved) {
 106     return JNI_VERSION_1_8;
 107 }
 108 #endif
 109 
 110 JNIEXPORT jint JNICALL
 111 #ifdef STATIC_BUILD
 112 Agent_OnAttach_attach045Agent03(JavaVM *vm, char *optionsString, void *reserved)
 113 #else
 114 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 115 #endif
 116 {
 117     jvmtiEventCallbacks eventCallbacks;
 118     jvmtiEnv* jvmti;
 119     jvmtiCapabilities caps;
 120     JNIEnv* jni;
 121 
 122     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 123         return JNI_ERR;
 124 
 125     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 126 
 127     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 128         return JNI_ERR;
 129 
 130     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 131         return JNI_ERR;
 132 
 133     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "attach045-agent03-eventsCounterMonitor", &eventsCounterMonitor))) {
 134         return JNI_ERR;
 135     }
 136 
 137     memset(&caps, 0, sizeof(caps));
 138     caps.can_generate_vm_object_alloc_events = 1;
 139     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps)) ) {
 140         return JNI_ERR;
 141     }
 142 
 143     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 144     eventCallbacks.VMObjectAlloc = VMObjectAllocHandler;
 145     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
 146         return JNI_ERR;
 147     }
 148 
 149     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 150         return JNI_ERR;
 151     }
 152 
 153     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 154 
 155     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 156         return JNI_ERR;
 157 
 158     return JNI_OK;
 159 }
 160 
 161 
 162 }