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  * In this test the same agent library is attached 3 times, but with
  35  * different options. In such scenario functions nsk_jvmti_aod_addMultiagentsOptions and
  36  * nsk_jvmti_aod_getMultiagentsOptions are used.
  37  *
  38  * Each agent from ClassLoad event handler tries to redefine class ClassToRedefine and
  39  * finishes work.
  40  */
  41 
  42 #define REDEFINED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach046/ClassToRedefine;"
  43 #define REDEFINED_CLASS_FILE_NAME "nsk/jvmti/AttachOnDemand/attach046/ClassToRedefine"
  44 
  45 void JNICALL  classLoadHandler(
  46         jvmtiEnv *jvmti,
  47         JNIEnv* jni,
  48         jthread thread,
  49         jclass klass) {
  50     char className[MAX_STRING_LENGTH];
  51     const char* agentName;
  52     Options* options;
  53 
  54     if (!NSK_VERIFY((options = nsk_jvmti_aod_getMultiagentsOptions(jvmti)) != NULL)) {
  55         NSK_COMPLAIN0("Failed to get agents's options\n");
  56         nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD);
  57         // can't call nsk_aod_agentFinished because of without options can't get agent's name
  58         return;
  59     }
  60 
  61     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
  62 
  63     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
  64         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
  65         return;
  66     }
  67 
  68     NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);
  69 
  70     if (!strcmp(className, REDEFINED_CLASS_NAME)) {
  71 
  72         NSK_DISPLAY1("%s: redefining class\n", agentName);
  73 
  74         if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, klass, REDEFINED_CLASS_FILE_NAME))) {
  75             NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);
  76             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
  77         } else {
  78             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 1, jvmti, jni);
  79         }
  80     }
  81 }
  82 
  83 #ifdef STATIC_BUILD
  84 JNIEXPORT jint JNI_OnLoad_attach046Agent00(JavaVM *jvm, char *options, void *reserved) {
  85     return JNI_VERSION_1_8;
  86 }
  87 #endif
  88 
  89 JNIEXPORT jint JNICALL
  90 #ifdef STATIC_BUILD
  91 Agent_OnAttach_attach046Agent00(JavaVM *vm, char *optionsString, void *reserved)
  92 #else
  93 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
  94 #endif
  95 {
  96     jvmtiEventCallbacks eventCallbacks;
  97     jvmtiCapabilities caps;
  98     jvmtiEnv* jvmti = NULL;
  99     JNIEnv* jni = NULL;
 100     Options* options;
 101     const char* agentName;
 102 
 103     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 104         return JNI_ERR;
 105 
 106     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 107 
 108     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 109         return NSK_FALSE;
 110 
 111     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 112         return JNI_ERR;
 113 
 114     memset(&caps, 0, sizeof(caps));
 115     caps.can_redefine_classes = 1;
 116     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 117         return JNI_ERR;
 118     }
 119 
 120     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 121     eventCallbacks.ClassLoad = classLoadHandler;
 122     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 123         return JNI_ERR;
 124     }
 125 
 126     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {
 127         return JNI_ERR;
 128     }
 129 
 130     if (!NSK_VERIFY(nsk_jvmti_aod_addMultiagentsOptions(jvmti, options))) {
 131         return JNI_ERR;
 132     }
 133 
 134     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 135 
 136     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 137         return JNI_ERR;
 138 
 139     return JNI_OK;
 140 }
 141 
 142 }