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 #include "ExceptionCheckingJniEnv.hpp"
  31 
  32 extern "C" {
  33 
  34 #define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"
  35 
  36 static jvmtiEnv* jvmti;
  37 static Options* options = NULL;
  38 static const char* agentName;
  39 
  40 static jvmtiEvent testEvents[] = {JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC};
  41 static const int testEventsNumber = 2;
  42 
  43 static volatile int taggedObjectsCounter = 0;
  44 static volatile int freedObjectsCounter = 0;
  45 
  46 static jrawMonitorID objectTagMonitor;
  47 static jrawMonitorID objectFreeMonitor;
  48 
  49 volatile int success = 1;
  50 
  51 volatile int agentFinished;
  52 
  53 void shutdownAgent(JNIEnv* jni) {
  54     if (agentFinished)
  55         return;
  56 
  57     if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
  58         success = 0;
  59 
  60     nsk_aod_agentFinished(jni, agentName, success);
  61 
  62     agentFinished = 1;
  63 }
  64 
  65 JNIEXPORT jboolean JNICALL
  66 Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,
  67         jclass klass, jint expectedTaggedObjectsCounter) {
  68     if (taggedObjectsCounter != expectedTaggedObjectsCounter) {
  69         success = 0;
  70         NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);
  71     }
  72 
  73     if (taggedObjectsCounter != freedObjectsCounter) {
  74         success = 0;
  75         NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",
  76                 taggedObjectsCounter, freedObjectsCounter);
  77     }
  78 
  79     shutdownAgent(jni);
  80 
  81     return success ? JNI_TRUE : JNI_FALSE;
  82 }
  83 
  84 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
  85     NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);
  86 
  87     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {
  88         freedObjectsCounter++;
  89 
  90         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
  91             success = 0;
  92         }
  93     } else {
  94         success = 0;
  95     }
  96 }
  97 
  98 #define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
  99 
 100 void registerNativeMethods(JNIEnv* jni_env) {
 101     ExceptionCheckingJniEnvPtr jni(jni_env);
 102     jclass appClass;
 103     JNINativeMethod nativeMethods[] = {
 104             {(char*)"shutdownAgent", (char*)"(I)Z",
 105             (void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent}};
 106     jint nativeMethodsNumber = 1;
 107 
 108     appClass = jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);
 109     jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);
 110 }
 111 
 112 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
 113         JNIEnv * jni,
 114         jthread thread,
 115         jobject object,
 116         jclass object_class,
 117         jlong size) {
 118     char className[MAX_STRING_LENGTH];
 119 
 120     if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
 121         success = 0;
 122         shutdownAgent(jni);
 123         return;
 124     }
 125 
 126     NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
 127 
 128     if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
 129         if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
 130             jlong tagValue = taggedObjectsCounter + 1;
 131 
 132             if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tagValue))) {
 133                 NSK_COMPLAIN1("%s: failed to set tag\n", agentName);
 134                 success = 0;
 135             } else {
 136                 NSK_DISPLAY2("%s: object was tagged (tag value: %ld)\n", agentName, tagValue);
 137                 taggedObjectsCounter++;
 138             }
 139 
 140             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectTagMonitor))) {
 141                 success = 0;
 142             }
 143         } else {
 144             success = 0;
 145         }
 146     }
 147 
 148     if (!success) {
 149         NSK_COMPLAIN1("%s: error happened during agent work, stop agent\n", agentName);
 150         shutdownAgent(jni);
 151     }
 152 }
 153 
 154 #ifdef STATIC_BUILD
 155 JNIEXPORT jint JNI_OnLoad_attach022Agent00(JavaVM *jvm, char *options, void *reserved) {
 156     return JNI_VERSION_1_8;
 157 }
 158 #endif
 159 
 160 JNIEXPORT jint JNICALL
 161 #ifdef STATIC_BUILD
 162 Agent_OnAttach_attach022Agent00(JavaVM *vm, char *optionsString, void *reserved)
 163 #else
 164 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 165 #endif
 166 {
 167     jvmtiEventCallbacks eventCallbacks;
 168     jvmtiCapabilities caps;
 169     JNIEnv* jni;
 170 
 171     options = (Options*) nsk_aod_createOptions(optionsString);
 172     if (!NSK_VERIFY(options != NULL))
 173         return JNI_ERR;
 174 
 175     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 176 
 177     jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
 178     if (jni == NULL)
 179         return JNI_ERR;
 180 
 181     jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
 182     if (!NSK_VERIFY(jvmti != NULL))
 183         return JNI_ERR;
 184 
 185     registerNativeMethods(jni);
 186 
 187     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
 188         return JNI_ERR;
 189     }
 190 
 191     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
 192         return JNI_ERR;
 193     }
 194 
 195     memset(&caps, 0, sizeof(caps));
 196     caps.can_tag_objects = 1;
 197     caps.can_generate_object_free_events = 1;
 198     caps.can_generate_vm_object_alloc_events = 1;
 199     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 200         return JNI_ERR;
 201     }
 202 
 203     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 204     eventCallbacks.ObjectFree = objectFreeHandler;
 205     eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
 206     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 207         return JNI_ERR;
 208     }
 209 
 210     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 211         return JNI_ERR;
 212     }
 213 
 214     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 215 
 216     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 217         return JNI_ERR;
 218 
 219     return JNI_OK;
 220 }
 221 
 222 }