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