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