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  *  - receive ClassLoad event for class FirstLoadedClass, from handler for this event
  36  *  disable ClassLoad events for all threads except thread loading FirstLoadedClass
  37  * (after these ClassLoad events should come only from this thread)
  38  *  - receive ClassLoad event for class LastLoadedClass and finish work
  39  */
  40 
  41 static Options* options = NULL;
  42 static const char* agentName;
  43 
  44 #define FIRST_LOADED_CLASS  "Lnsk/jvmti/AttachOnDemand/attach009/FirstLoadedClass;"
  45 #define LAST_LOADED_CLASS  "Lnsk/jvmti/AttachOnDemand/attach009/LastLoadedClass;"
  46 
  47 static int disabledForOthers = 0;
  48 
  49 static volatile int success = 1;
  50 
  51 void JNICALL
  52 classLoadHandler(jvmtiEnv *jvmti,
  53         JNIEnv* jni,
  54         jthread thread,
  55         jclass klass) {
  56     static char mainThreadName[MAX_STRING_LENGTH];
  57     char loadedClassName[MAX_STRING_LENGTH];
  58     char threadName[MAX_STRING_LENGTH];
  59 
  60     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  61         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
  62         return;
  63     }
  64 
  65     if (!nsk_jvmti_aod_getClassName(jvmti, klass, loadedClassName)) {
  66         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
  67         return;
  68     }
  69 
  70     NSK_DISPLAY2("Class '%s' was loaded by thread '%s'\n", loadedClassName, threadName);
  71 
  72     /*
  73      * When class FIRST_LOADED_CLASS was loaded try to disable events for all threads
  74      * except main target application thread
  75      */
  76     if (strcmp(loadedClassName, FIRST_LOADED_CLASS) == 0) {
  77         strcpy(mainThreadName, threadName);
  78 
  79         if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD)) {
  80             NSK_COMPLAIN0("Failed to disable events\n");
  81             nsk_aod_agentFinished(jni, agentName, 0);
  82             return;
  83         }
  84 
  85         if (!NSK_JVMTI_VERIFY( NSK_CPP_STUB4(
  86                 SetEventNotificationMode, jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, thread) ) ) {
  87             NSK_COMPLAIN1("Failed to enable events for thread '%s'\n", mainThreadName);
  88             nsk_aod_agentFinished(jni, agentName, 0);
  89             return;
  90         }
  91 
  92         NSK_DISPLAY1("ClassLoad events are enabled only for thread '%s'", mainThreadName);
  93 
  94         disabledForOthers = 1;
  95 
  96         return;
  97     }
  98 
  99     if (disabledForOthers) {
 100         if (strcmp(threadName, mainThreadName) != 0) {
 101             success = 0;
 102             NSK_COMPLAIN1("ClassLoad event was erroneously generated for thread '%s'\n", threadName);
 103         }
 104     }
 105 
 106     /*
 107      * Stop agent when LAST_LOADED_CLASS was loaded
 108      */
 109     if (strcmp(loadedClassName, LAST_LOADED_CLASS) == 0) {
 110         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni);
 111     }
 112 }
 113 
 114 
 115 #ifdef STATIC_BUILD
 116 JNIEXPORT jint JNI_OnLoad_attach009Agent00(JavaVM *jvm, char *options, void *reserved) {
 117     return JNI_VERSION_1_8;
 118 }
 119 #endif
 120 
 121 JNIEXPORT jint JNICALL
 122 #ifdef STATIC_BUILD
 123 Agent_OnAttach_attach009Agent00(JavaVM *vm, char *optionsString, void *reserved)
 124 #else
 125 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 126 #endif
 127 {
 128     jvmtiEnv* jvmti;
 129     jvmtiEventCallbacks eventCallbacks;
 130     JNIEnv* jni;
 131 
 132     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 133         return JNI_ERR;
 134 
 135     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 136 
 137     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 138         return NSK_FALSE;
 139 
 140     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 141         return JNI_ERR;
 142 
 143     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 144     eventCallbacks.ClassLoad = classLoadHandler;
 145     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
 146         return JNI_ERR;
 147     }
 148 
 149     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {
 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 }