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(jvmti->RawMonitorEnter(eventsCounterMonitor))) {
  72 
  73         eventsCounter++;
  74 
  75         NSK_DISPLAY4("%s: VMObjectAlloc received in thread '%s' for instance of '%s' (eventsCounter: %d)\n",
  76                 agentName, threadName, className, eventsCounter);
  77 
  78         if ((eventsCounter % 10) == 0) {
  79             NSK_DISPLAY1("%s: force garbage collection\n", agentName);
  80 
  81             if (!NSK_JVMTI_VERIFY(jvmti->ForceGarbageCollection()))
  82                 success = 0;
  83         }
  84 
  85         if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
  86             NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
  87 
  88             nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
  89         }
  90 
  91         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {
  92             success = 0;
  93         }
  94     } else {
  95         success = 0;
  96     }
  97 
  98     if (!success) {
  99         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
 100     }
 101 }
 102 
 103 #ifdef STATIC_BUILD
 104 JNIEXPORT jint JNI_OnLoad_attach045Agent03(JavaVM *jvm, char *options, void *reserved) {
 105     return JNI_VERSION_1_8;
 106 }
 107 #endif
 108 
 109 JNIEXPORT jint JNICALL
 110 #ifdef STATIC_BUILD
 111 Agent_OnAttach_attach045Agent03(JavaVM *vm, char *optionsString, void *reserved)
 112 #else
 113 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 114 #endif
 115 {
 116     jvmtiEventCallbacks eventCallbacks;
 117     jvmtiEnv* jvmti;
 118     jvmtiCapabilities caps;
 119     JNIEnv* jni;
 120 
 121     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 122         return JNI_ERR;
 123 
 124     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 125 
 126     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 127         return JNI_ERR;
 128 
 129     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 130         return JNI_ERR;
 131 
 132     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent03-eventsCounterMonitor", &eventsCounterMonitor))) {
 133         return JNI_ERR;
 134     }
 135 
 136     memset(&caps, 0, sizeof(caps));
 137     caps.can_generate_vm_object_alloc_events = 1;
 138     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 139         return JNI_ERR;
 140     }
 141 
 142     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 143     eventCallbacks.VMObjectAlloc = VMObjectAllocHandler;
 144     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 145         return JNI_ERR;
 146     }
 147 
 148     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 149         return JNI_ERR;
 150     }
 151 
 152     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 153 
 154     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 155         return JNI_ERR;
 156 
 157     return JNI_OK;
 158 }
 159 
 160 
 161 }