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     hs202t001:
  31         1. Set breakpoints in several methods when Object.wait(),
  32         Object.notify(), Object.notifyAll() are in use in these methods.
  33         2. Upon reaching a breakpoint, enable SingleStep.
  34         3. Redefine an java.lang.Object class within SingleStep callback
  35         when one of its methods is called by the tested method.
  36         4. Pop a currently executed frame.
  37 
  38 */
  39 extern "C" {
  40 #define FILE_NAME "nsk/jvmti/scenarios/hotswap/HS202/hs203t001/MyObject"
  41 #define CLASS_NAME "Lnsk/jvmti/scenarios/hotswap/HS202/hs203t001/MyObject;"
  42 #define METHOD_NAME "leaveMonitor"
  43 #define METHOD_SIGN "()V"
  44 #define METHOD_NOTIFYALL "notifyAll"
  45 
  46 static jvmtiEnv * jvmti;
  47 
  48 
  49 JNIEXPORT void JNICALL callbackClassPrepare(jvmtiEnv *jvmti,
  50                                         JNIEnv* jni,
  51                                         jthread thread,
  52                                         jclass klass) {
  53     char * className;
  54     char * generic;
  55     jvmti->GetClassSignature(klass, &className, &generic);
  56     if (strcmp(className,CLASS_NAME) == 0) {
  57         jmethodID method;
  58         method = jni->GetMethodID(klass, METHOD_NAME, METHOD_SIGN);
  59         if (method == NULL) {
  60             nsk_printf("Agent:: Method is null ");
  61         } else {
  62             jlocation start;
  63             jlocation end;
  64             jvmtiError err ;
  65             err=jvmti->GetMethodLocation(method, &start, &end);
  66             if ( err != JVMTI_ERROR_NONE ) {
  67                 nsk_printf(" ## Error occured %s \n",TranslateError(err));
  68             }else {
  69                 nsk_printf("\n Start = %d and end = %d ", start , end);
  70                 nsk_printf(" setting break points..");
  71                 err= jvmti->SetBreakpoint(method, start);
  72                 if (err != JVMTI_ERROR_NONE) {
  73                     nsk_printf(" ## Error occured %s \n",TranslateError(err));
  74                 } else  {
  75                     nsk_printf(" NO ERRORS ");
  76                     if (nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_BREAKPOINT, NULL) == NSK_TRUE ) {
  77                         nsk_printf(" Enabled.. notification event ..\n");
  78                     }
  79                 }
  80             }
  81         }
  82     }
  83 }
  84 
  85 /*
  86  *This event call back will be called when a filed
  87  *threadState is beeing upodated or beeing used in
  88  *the program java flow.
  89  *In this current example the code will be called
  90  */
  91 void JNICALL callbackSingleStep(jvmtiEnv *jvmti_env,
  92         JNIEnv* jni,
  93         jthread thread,
  94         jmethodID method,
  95         jlocation location) {
  96     jvmtiError err;
  97     char * name;
  98     char * signature;
  99     char * generic ;
 100     err = JVMTI_ERROR_NONE;
 101     jvmti_env->GetMethodName(method, &name, &signature, &generic);
 102     if (strcmp(name,METHOD_NAME) == 0) { /* same method */
 103         jclass cls;
 104         jmethodID mem ;
 105         jvmti_env->GetMethodDeclaringClass(method, &cls);
 106         mem=jni->GetMethodID(cls,METHOD_NOTIFYALL,"()V");
 107         jni->CallVoidMethod(thread,mem);
 108     }
 109 
 110 }
 111 
 112 void JNICALL callbackBreakpoint(jvmtiEnv *jvmti_env,
 113         JNIEnv* jni_env,
 114         jthread thread,
 115         jmethodID method,
 116         jlocation location) {
 117     jvmtiError err;
 118     err = JVMTI_ERROR_NONE;
 119     if ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_SINGLE_STEP, NULL)
 120             == NSK_TRUE ) {
 121         nsk_printf(" Enabled.. notification event ..");
 122     }
 123     err= jvmti->SetEventNotificationMode(JVMTI_DISABLE,
 124             JVMTI_EVENT_BREAKPOINT, NULL);
 125     if (err == JVMTI_ERROR_NONE) {
 126         nsk_printf(" Disabled notification..");
 127     }
 128 
 129 }
 130 
 131 
 132 #ifdef STATIC_BUILD
 133 JNIEXPORT jint JNICALL Agent_OnLoad_hs202t001(JavaVM *jvm, char *options, void *reserved) {
 134     return Agent_Initialize(jvm, options, reserved);
 135 }
 136 JNIEXPORT jint JNICALL Agent_OnAttach_hs202t001(JavaVM *jvm, char *options, void *reserved) {
 137     return Agent_Initialize(jvm, options, reserved);
 138 }
 139 JNIEXPORT jint JNI_OnLoad_hs202t001(JavaVM *jvm, char *options, void *reserved) {
 140     return JNI_VERSION_1_8;
 141 }
 142 #endif
 143 jint  Agent_Initialize(JavaVM *vm, char *options, void *reserved) {
 144     jvmtiError rc;
 145     jint code;
 146     nsk_printf("Agent:: VM.. Started..\n");
 147     code = vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_1);
 148     if ( code != JNI_OK ) {
 149         nsk_printf("Agent:: Could not load JVMTI interface \n");
 150         return JNI_ERR;
 151     } else {
 152         jvmtiCapabilities caps;
 153         jvmtiEventCallbacks eventCallbacks;
 154         memset(&caps, 0, sizeof(caps));
 155         if (nsk_jvmti_parseOptions(options) == NSK_FALSE ) {
 156             nsk_printf("# error agent Failed to parse options \n");
 157             return JNI_ERR;
 158         }
 159         caps.can_redefine_classes = 1;
 160         caps.can_suspend = 1;
 161         caps.can_pop_frame = 1;
 162         caps.can_generate_all_class_hook_events = 1;
 163         caps.can_generate_compiled_method_load_events = 1;
 164         caps.can_generate_breakpoint_events=1;
 165         caps.can_generate_single_step_events=1;
 166         jvmti->AddCapabilities(&caps);
 167         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 168         eventCallbacks.ClassPrepare =callbackClassPrepare;
 169         eventCallbacks.SingleStep =callbackSingleStep;
 170         eventCallbacks.Breakpoint =callbackBreakpoint;
 171         rc = jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks));
 172         if (rc != JVMTI_ERROR_NONE) {
 173             nsk_printf(" ## Error occured %s \n",TranslateError(rc));
 174             return JNI_ERR;
 175         }
 176         if ( nsk_jvmti_enableNotification(jvmti,JVMTI_EVENT_CLASS_PREPARE, NULL) == NSK_TRUE ) {
 177             nsk_printf("Agent :: NOTIFICATIONS ARE ENABLED \n");
 178         } else {
 179             nsk_printf(" Error in Eanableing Notifications..");
 180         }
 181     }
 182     return JNI_OK;
 183 }
 184 
 185 JNIEXPORT jboolean JNICALL
 186 Java_nsk_jvmti_scenarios_hotswap_HS202_hs202t001_hs202t001_popThreadFrame(JNIEnv * jni,
 187         jclass clas,
 188         jthread thread) {
 189     jvmtiError err ;
 190     jboolean retvalue;
 191     jint state;
 192     nsk_printf("Agent:: POPING THE FRAME....\n");
 193     retvalue = JNI_FALSE;
 194     jvmti->GetThreadState(thread, &state);
 195     if ( state & JVMTI_THREAD_STATE_SUSPENDED) {
 196         err = jvmti->PopFrame(thread);
 197         if (err == JVMTI_ERROR_NONE) {
 198             nsk_printf("Agent:: NO Errors poped very well ..\n");
 199             retvalue=JNI_OK;
 200             return retvalue;
 201         } else if (err != JVMTI_ERROR_NONE) {
 202             nsk_printf(" ## Error occured %s \n",TranslateError(err));
 203         }
 204         nsk_printf("Agent:: some other error ..\n");
 205     } else {
 206         nsk_printf("Agent:: Thread was not suspened.. check for capabilities, and java method signature ");
 207     }
 208     return retvalue;
 209 }
 210 
 211 JNIEXPORT jboolean JNICALL
 212 Java_nsk_jvmti_scenarios_hotswap_HS202_hs202t001_hs202t001_resumeThread(JNIEnv * jni,
 213                                                                            jclass clas,
 214                                                                            jthread thread) {
 215     jvmtiError err ;
 216     jboolean retvalue;
 217     retvalue = JNI_FALSE;
 218     err = jvmti->ResumeThread(thread);
 219     if (err == JVMTI_ERROR_NONE) {
 220         nsk_printf(" Agent:: Thread Resumed.. \n");
 221         retvalue=JNI_OK;
 222     } else {
 223         nsk_printf(" Agent:: Failed.. to Resume the thread.\n");
 224     }
 225     return retvalue;
 226 }
 227 
 228 }