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 <jni.h>
  24 #include <jvmti.h>
  25 #include "agent_common.h"
  26 #include <string.h>
  27 #include "jvmti_tools.h"
  28 
  29 extern "C" {
  30 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t003/MyClass"
  31 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS301/hs301t003/MyClass;"
  32 
  33 void JNICALL callbackClassPrepare(jvmtiEnv *jvmti_env,
  34                                JNIEnv* jni_env,
  35                                jthread thread,
  36                                jclass klass) {
  37   char * className;
  38   char * generic;
  39   if ( ! NSK_JVMTI_VERIFY ( NSK_CPP_STUB4(GetClassSignature,
  40                   jvmti_env, klass, &className, &generic) ) ) {
  41     nsk_printf(" Agent:: Error while getting ClassFileName Signature ");
  42   } else {
  43       if (strcmp(className, CLASS_NAME ) == 0) {
  44           int redefineNumber =0;
  45           char fileName[512];
  46           nsk_jvmti_getFileName(redefineNumber, FILE_NAME,
  47                   fileName, sizeof(fileName)/sizeof(char));
  48           nsk_jvmti_disableNotification(jvmti_env,
  49                   JVMTI_EVENT_CLASS_LOAD, NULL );
  50           if (nsk_jvmti_redefineClass(jvmti_env, klass, fileName) == NSK_TRUE) {
  51               nsk_printf("\n Redefine successful.\n");
  52           } else {
  53               nsk_printf("\n Redefine failed.\n");
  54           }
  55       }
  56   }
  57 }
  58 
  59 #ifdef STATIC_BUILD
  60 JNIEXPORT jint JNICALL Agent_OnLoad_hs301t003(JavaVM *jvm, char *options, void *reserved) {
  61     return Agent_Initialize(jvm, options, reserved);
  62 }
  63 JNIEXPORT jint JNICALL Agent_OnAttach_hs301t003(JavaVM *jvm, char *options, void *reserved) {
  64     return Agent_Initialize(jvm, options, reserved);
  65 }
  66 JNIEXPORT jint JNI_OnLoad_hs301t003(JavaVM *jvm, char *options, void *reserved) {
  67     return JNI_VERSION_1_8;
  68 }
  69 #endif
  70 jint  Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
  71     jvmtiEnv * jvmti;
  72     nsk_printf("Agent:: Agent_OnLoad.\n");
  73     if ( ! NSK_VERIFY ( JNI_OK == NSK_CPP_STUB3(GetEnv, vm,
  74                     (void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
  75         nsk_printf("Agent:: Could not load JVMTI interface.\n");
  76         return JNI_ERR;
  77     } else {
  78         jvmtiCapabilities caps;
  79         jvmtiEventCallbacks eventCallbacks;
  80         if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
  81             nsk_printf("# error agent Failed to parse options.\n");
  82             return JNI_ERR;
  83         }
  84         memset(&caps, 0, sizeof(caps));
  85         caps.can_redefine_classes = 1;
  86         caps.can_generate_all_class_hook_events=1;
  87         if (! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2(AddCapabilities, jvmti, &caps) ))  {
  88             nsk_printf(" Agent:: Error occured while adding capabilities.\n");
  89             return JNI_ERR;
  90         }
  91         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
  92         eventCallbacks.ClassPrepare = &callbackClassPrepare;
  93         if (!NSK_JVMTI_VERIFY(
  94                 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
  95                                     &eventCallbacks, sizeof(eventCallbacks)))) {
  96             nsk_printf(" Agent:: Error occured while setting event call back.\n");
  97             return JNI_ERR;
  98         }
  99         if ( nsk_jvmti_enableNotification(jvmti, JVMTI_EVENT_CLASS_PREPARE, NULL) == NSK_TRUE ) {
 100             nsk_printf(" Agent:: Enabled notification.\n");
 101         } else {
 102             nsk_printf(" Agent:: Failed to enable notification.\n");
 103         }
 104     }
 105     return JNI_OK;
 106 }
 107 }