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 
  24 #include <jni.h>
  25 #include <jvmti.h>
  26 #include "agent_common.h"
  27 #include <string.h>
  28 #include "jvmti_tools.h"
  29 #include "jni_tools.h"
  30 extern "C" {
  31 
  32 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t001/MyClass"
  33 #define DIR_NAME "newclass"
  34 #define PATH_FORMAT "%s%02d/%s"
  35 #define SEARCH_NAME "nsk/jvmti/scenarios/hotswap/HS301/hs301t001/MyClass"
  36 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS301/hs301t001/MyClass;"
  37 static jvmtiEnv * jvmti;
  38 
  39 #ifdef STATIC_BUILD
  40 JNIEXPORT jint JNICALL Agent_OnLoad_hs301t001(JavaVM *jvm, char *options, void *reserved) {
  41     return Agent_Initialize(jvm, options, reserved);
  42 }
  43 JNIEXPORT jint JNICALL Agent_OnAttach_hs301t001(JavaVM *jvm, char *options, void *reserved) {
  44     return Agent_Initialize(jvm, options, reserved);
  45 }
  46 JNIEXPORT jint JNI_OnLoad_hs301t001(JavaVM *jvm, char *options, void *reserved) {
  47     return JNI_VERSION_1_8;
  48 }
  49 #endif
  50 jint  Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
  51     nsk_printf("Agent:: Agent_OnLoad .\n");
  52     if ( ! NSK_VERIFY ( JNI_OK == NSK_CPP_STUB3(GetEnv, vm,
  53                     (void **)&jvmti, JVMTI_VERSION_1_1) ) ) {
  54         nsk_printf("Agent:: Could not load JVMTI interface.\n");
  55         return JNI_ERR;
  56     } else {
  57         jvmtiCapabilities caps;
  58         jvmtiEventCallbacks eventCallbacks;
  59         if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
  60             nsk_printf("# error agent Failed to parse options.\n");
  61             return JNI_ERR;
  62         }
  63         memset(&caps, 0, sizeof(caps));
  64         caps.can_redefine_classes = 1;
  65         if (! NSK_JVMTI_VERIFY ( NSK_CPP_STUB2(AddCapabilities, jvmti, &caps) )) {
  66             nsk_printf(" Agent:: Error occured while adding capabilities.\n");
  67             return JNI_ERR;
  68         }
  69         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
  70         if (!NSK_JVMTI_VERIFY(
  71                 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
  72                                     &eventCallbacks, sizeof(eventCallbacks)))) {
  73             nsk_printf(" Agent:: Error occured while setting event call back.\n");
  74             return JNI_ERR;
  75         }
  76     }
  77     return JNI_OK;
  78 }
  79 
  80 JNIEXPORT jboolean JNICALL
  81 Java_nsk_jvmti_scenarios_hotswap_HS301_hs301t001_hs301t001_redefine(JNIEnv * jni, jobject jboj) {
  82     jclass cls;
  83     jboolean ret;
  84     char fileName[512];
  85     int redefineNumber;
  86 
  87 
  88     redefineNumber=0;
  89     //cls= NSK_CPP_STUB2(FindClass, jni, SEARCH_NAME);
  90     if (! NSK_JNI_VERIFY(jni, (cls = NSK_CPP_STUB2(FindClass, jni, SEARCH_NAME)) != NULL) ) {
  91         nsk_printf("Agent:: (*JNI)->FindClass(jni, %s) returns `null`.\n",SEARCH_NAME);
  92         return NSK_FALSE;
  93     }
  94     ret = JNI_FALSE;
  95     nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName, sizeof(fileName)/sizeof(char));
  96     if ( nsk_jvmti_redefineClass(jvmti, cls, fileName) == NSK_TRUE ) {
  97         nsk_printf("Agent:: Redefine successful.\n");
  98         ret = JNI_TRUE;
  99     } else {
 100         nsk_printf("#error Agent :: Redefine failed.\n");
 101     }
 102     return ret;
 103 }
 104 
 105 }