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