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 #define LOADED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach014/ClassToLoad;"
  34 
  35 /*
  36  * Expected agent work scenario:
  37  *  - during initialization agent enables ClassLoad events
  38  *  - target application loads class 'ClassToLoad'
  39  *  - agent receives ClassLoad event for this class, calls DisposeEnvironment and finishes work
  40  */
  41 
  42 static Options* options = NULL;
  43 static const char* agentName;
  44 
  45 void JNICALL
  46 classLoadHandler(jvmtiEnv *jvmti,
  47         JNIEnv* jni,
  48         jthread thread,
  49         jclass klass) {
  50     char className[MAX_STRING_LENGTH];
  51 
  52     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
  53         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
  54         return;
  55     }
  56 
  57     NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);
  58 
  59     if (!strcmp(className, LOADED_CLASS_NAME)) {
  60         int success = 1;
  61 
  62         if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))
  63             success = 0;
  64 
  65         if (!NSK_JVMTI_VERIFY(jvmti->DisposeEnvironment())) {
  66             success = 0;
  67             NSK_COMPLAIN1("%s: failed to dispose environment\n", agentName);
  68         } else {
  69             NSK_DISPLAY1("%s: jvmti env was disposed\n", agentName);
  70         }
  71 
  72         nsk_aod_agentFinished(jni, agentName, success);
  73     }
  74 }
  75 
  76 
  77 #ifdef STATIC_BUILD
  78 JNIEXPORT jint JNI_OnLoad_attach014Agent00(JavaVM *jvm, char *options, void *reserved) {
  79     return JNI_VERSION_1_8;
  80 }
  81 #endif
  82 
  83 JNIEXPORT jint JNICALL
  84 #ifdef STATIC_BUILD
  85 Agent_OnAttach_attach014Agent00(JavaVM *vm, char *optionsString, void *reserved)
  86 #else
  87 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  88 #endif
  89 {
  90     jvmtiEventCallbacks eventCallbacks;
  91     jvmtiEnv* jvmti = NULL;
  92     JNIEnv* jni = NULL;
  93 
  94     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
  95         return JNI_ERR;
  96 
  97     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  98 
  99     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 100         return NSK_FALSE;
 101 
 102     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 103         return JNI_ERR;
 104 
 105     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 106     eventCallbacks.ClassLoad = classLoadHandler;
 107     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 108         return JNI_ERR;
 109     }
 110 
 111     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {
 112         return JNI_ERR;
 113     }
 114 
 115     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 116 
 117     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 118         return JNI_ERR;
 119 
 120     return JNI_OK;
 121 }
 122 
 123 }