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 == vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1) ) ) { 53 nsk_printf("Agent:: Could not load JVMTI interface.\n"); 54 return JNI_ERR; 55 } else { 56 jvmtiCapabilities caps; 57 jvmtiEventCallbacks eventCallbacks; 58 if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) { 59 nsk_printf("# error agent Failed to parse options.\n"); 60 return JNI_ERR; 61 } 62 memset(&caps, 0, sizeof(caps)); 63 caps.can_redefine_classes = 1; 64 if (! NSK_JVMTI_VERIFY (jvmti->AddCapabilities(&caps) )) { 65 nsk_printf(" Agent:: Error occured while adding capabilities.\n"); 66 return JNI_ERR; 67 } 68 memset(&eventCallbacks, 0, sizeof(eventCallbacks)); 69 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 70 nsk_printf(" Agent:: Error occured while setting event call back.\n"); 71 return JNI_ERR; 72 } 73 } 74 return JNI_OK; 75 } 76 77 JNIEXPORT jboolean JNICALL 78 Java_nsk_jvmti_scenarios_hotswap_HS301_hs301t001_hs301t001_redefine(JNIEnv * jni, jobject jboj) { 79 jclass cls; 80 jboolean ret; 81 char fileName[512]; 82 int redefineNumber; 83 84 85 redefineNumber=0; 86 cls = jni->FindClass(SEARCH_NAME); 87 if (! NSK_JNI_VERIFY(jni, cls != NULL) ) { 88 nsk_printf("Agent:: (*JNI)->FindClass(jni, %s) returns `null`.\n",SEARCH_NAME); 89 return NSK_FALSE; 90 } 91 ret = JNI_FALSE; 92 nsk_jvmti_getFileName(redefineNumber, FILE_NAME, fileName, sizeof(fileName)/sizeof(char)); 93 if ( nsk_jvmti_redefineClass(jvmti, cls, fileName) == NSK_TRUE ) { 94 nsk_printf("Agent:: Redefine successful.\n"); 95 ret = JNI_TRUE; 96 } else { 97 nsk_printf("#error Agent :: Redefine failed.\n"); 98 } 99 return ret; 100 } 101 102 }