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