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  *  - during initialization agent enables ClassPrepare event
  36  *  - agent waits events for 2 classes loaded by target application and
  37  *    finishes work
  38  */
  39 
  40 #define CLASS_NAME1 "Lnsk/jvmti/AttachOnDemand/attach015/ClassToLoad1;"
  41 #define CLASS_NAME2 "Lnsk/jvmti/AttachOnDemand/attach015/ClassToLoad2;"
  42 
  43 static Options* options = NULL;
  44 static const char* agentName;
  45 
  46 static int receivedEventsCount = 0;
  47 
  48 void JNICALL classPrepareHandler(jvmtiEnv *jvmti,
  49         JNIEnv* jni,
  50         jthread thread,
  51         jclass klass) {
  52     char className[MAX_STRING_LENGTH];
  53 
  54     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
  55         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 0, jvmti, jni);
  56         return;
  57     }
  58 
  59     NSK_DISPLAY2("%s: class prepare event for class '%s'\n", agentName, className);
  60 
  61     if (!strcmp(className, CLASS_NAME1) || !strcmp(className, CLASS_NAME2)) {
  62         receivedEventsCount++;
  63 
  64         if (receivedEventsCount == 2) {
  65             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_PREPARE, 1, jvmti, jni);
  66         }
  67     }
  68 }
  69 
  70 #ifdef STATIC_BUILD
  71 JNIEXPORT jint JNI_OnLoad_attach015Agent00(JavaVM *jvm, char *options, void *reserved) {
  72     return JNI_VERSION_1_8;
  73 }
  74 #endif
  75 
  76 JNIEXPORT jint JNICALL
  77 #ifdef STATIC_BUILD
  78 Agent_OnAttach_attach015Agent00(JavaVM *vm, char *optionsString, void *reserved)
  79 #else
  80 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  81 #endif
  82 {
  83     jvmtiEventCallbacks eventCallbacks;
  84     jvmtiEnv* jvmti = NULL;
  85     JNIEnv* jni = NULL;
  86 
  87     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
  88         return JNI_ERR;
  89 
  90     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  91 
  92     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
  93         return NSK_FALSE;
  94 
  95     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
  96         return JNI_ERR;
  97 
  98     memset(&eventCallbacks,0, sizeof(eventCallbacks));
  99     eventCallbacks.ClassPrepare = classPrepareHandler;
 100     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 101         return JNI_ERR;
 102     }
 103 
 104     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_PREPARE))) {
 105         return JNI_ERR;
 106     }
 107 
 108     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 109 
 110     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 111         return JNI_ERR;
 112 
 113     return JNI_OK;
 114 }
 115 }