< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/multienv/MA10/ma10t003/ma10t003.cpp

Print this page
rev 52100 : 8212082: Remove the NSK_CPP_STUB macros for remaining vmTestbase/jvmti/[sS]*
Summary:
Reviewed-by:


  38 static jlong timeout = 0;
  39 
  40 /* test objects */
  41 static jthread thread = NULL;
  42 
  43 /* event counts */
  44 static int MethodExitEventsCount = 0;
  45 
  46 /* ========================================================================== */
  47 
  48 /** callback functions **/
  49 
  50 static void JNICALL
  51 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  52         jthread thread, jmethodID method,
  53         jboolean was_poped_by_exception, jvalue return_value) {
  54     char *name = NULL;
  55     char *signature = NULL;
  56 
  57     MethodExitEventsCount++;
  58     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
  59             jvmti_env, method, &name, &signature, NULL))) {
  60         nsk_jvmti_setFailStatus();
  61         return;
  62     }
  63     NSK_DISPLAY2("MethodExit event: %s%s\n", name, signature);
  64     if (name != NULL)
  65         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
  66     if (signature != NULL)
  67         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  68 }
  69 
  70 /* ========================================================================== */
  71 
  72 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
  73     const char* THREAD_NAME = "Debuggee Thread";
  74     jvmtiThreadInfo info;
  75     jthread *threads = NULL;
  76     jint threads_count = 0;
  77     int i;
  78 
  79     NSK_DISPLAY0("Prepare: find tested thread\n");
  80 
  81     /* get all live threads */
  82     if (!NSK_JVMTI_VERIFY(
  83            NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads)))
  84         return NSK_FALSE;
  85 
  86     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
  87         return NSK_FALSE;
  88 
  89     /* find tested thread */
  90     for (i = 0; i < threads_count; i++) {
  91         if (!NSK_VERIFY(threads[i] != NULL))
  92             return NSK_FALSE;
  93 
  94         /* get thread information */
  95         if (!NSK_JVMTI_VERIFY(
  96                 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info)))
  97             return NSK_FALSE;
  98 
  99         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
 100 
 101         /* find by name */
 102         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
 103             thread = threads[i];
 104         }
 105     }
 106 
 107     if (!NSK_JNI_VERIFY(jni, (thread =
 108             NSK_CPP_STUB2(NewGlobalRef, jni, thread)) != NULL))
 109         return NSK_FALSE;
 110 
 111     /* deallocate threads list */
 112     if (!NSK_JVMTI_VERIFY(
 113             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads)))
 114         return NSK_FALSE;
 115 
 116     return NSK_TRUE;
 117 }
 118 
 119 /* ========================================================================== */
 120 
 121 /** Agent algorithm. */
 122 static void JNICALL
 123 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 124 
 125     if (!nsk_jvmti_waitForSync(timeout))
 126         return;
 127 
 128     if (!prepare(jvmti, jni)) {
 129         nsk_jvmti_setFailStatus();
 130         return;
 131     }
 132 
 133     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 134             jvmti, JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread)))
 135         nsk_jvmti_setFailStatus();
 136 
 137     /* resume debugee and wait for sync */
 138     if (!nsk_jvmti_resumeSync())
 139         return;
 140     if (!nsk_jvmti_waitForSync(timeout))
 141         return;
 142 
 143     NSK_DISPLAY1("MethodExit events received: %d\n",
 144         MethodExitEventsCount);
 145     if (!NSK_VERIFY(MethodExitEventsCount != 0))
 146         nsk_jvmti_setFailStatus();
 147 
 148     NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, thread));
 149 
 150     if (!nsk_jvmti_resumeSync())
 151         return;
 152 }
 153 
 154 /* ========================================================================== */
 155 
 156 /** Agent library initialization. */
 157 #ifdef STATIC_BUILD
 158 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 159     return Agent_Initialize(jvm, options, reserved);
 160 }
 161 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 162     return Agent_Initialize(jvm, options, reserved);
 163 }
 164 JNIEXPORT jint JNI_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 165     return JNI_VERSION_1_8;
 166 }
 167 #endif
 168 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 169     jvmtiEnv* jvmti = NULL;
 170     jvmtiCapabilities caps;
 171     jvmtiEventCallbacks callbacks;
 172 
 173     NSK_DISPLAY0("Agent_OnLoad\n");
 174 
 175     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 176         return JNI_ERR;
 177 
 178     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 179 
 180     if (!NSK_VERIFY((jvmti =
 181             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 182         return JNI_ERR;
 183 
 184     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 185         return JNI_ERR;
 186 
 187     memset(&caps, 0, sizeof(caps));
 188     caps.can_generate_method_exit_events = 1;
 189     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 190         return JNI_ERR;
 191     }
 192 
 193     memset(&callbacks, 0, sizeof(callbacks));
 194     callbacks.MethodExit = &MethodExit;
 195     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 196         return JNI_ERR;
 197 
 198     return JNI_OK;
 199 }
 200 
 201 /* ========================================================================== */
 202 
 203 }


  38 static jlong timeout = 0;
  39 
  40 /* test objects */
  41 static jthread thread = NULL;
  42 
  43 /* event counts */
  44 static int MethodExitEventsCount = 0;
  45 
  46 /* ========================================================================== */
  47 
  48 /** callback functions **/
  49 
  50 static void JNICALL
  51 MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  52         jthread thread, jmethodID method,
  53         jboolean was_poped_by_exception, jvalue return_value) {
  54     char *name = NULL;
  55     char *signature = NULL;
  56 
  57     MethodExitEventsCount++;
  58     if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {

  59         nsk_jvmti_setFailStatus();
  60         return;
  61     }
  62     NSK_DISPLAY2("MethodExit event: %s%s\n", name, signature);
  63     if (name != NULL)
  64         jvmti_env->Deallocate((unsigned char*)name);
  65     if (signature != NULL)
  66         jvmti_env->Deallocate((unsigned char*)signature);
  67 }
  68 
  69 /* ========================================================================== */
  70 
  71 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
  72     const char* THREAD_NAME = "Debuggee Thread";
  73     jvmtiThreadInfo info;
  74     jthread *threads = NULL;
  75     jint threads_count = 0;
  76     int i;
  77 
  78     NSK_DISPLAY0("Prepare: find tested thread\n");
  79 
  80     /* get all live threads */
  81     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))

  82         return NSK_FALSE;
  83 
  84     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
  85         return NSK_FALSE;
  86 
  87     /* find tested thread */
  88     for (i = 0; i < threads_count; i++) {
  89         if (!NSK_VERIFY(threads[i] != NULL))
  90             return NSK_FALSE;
  91 
  92         /* get thread information */
  93         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))

  94             return NSK_FALSE;
  95 
  96         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
  97 
  98         /* find by name */
  99         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
 100             thread = threads[i];
 101         }
 102     }
 103 
 104     if (!NSK_JNI_VERIFY(jni, (thread = jni->NewGlobalRef(thread)) != NULL))

 105         return NSK_FALSE;
 106 
 107     /* deallocate threads list */
 108     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))

 109         return NSK_FALSE;
 110 
 111     return NSK_TRUE;
 112 }
 113 
 114 /* ========================================================================== */
 115 
 116 /** Agent algorithm. */
 117 static void JNICALL
 118 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 119 
 120     if (!nsk_jvmti_waitForSync(timeout))
 121         return;
 122 
 123     if (!prepare(jvmti, jni)) {
 124         nsk_jvmti_setFailStatus();
 125         return;
 126     }
 127 
 128     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, thread)))

 129         nsk_jvmti_setFailStatus();
 130 
 131     /* resume debugee and wait for sync */
 132     if (!nsk_jvmti_resumeSync())
 133         return;
 134     if (!nsk_jvmti_waitForSync(timeout))
 135         return;
 136 
 137     NSK_DISPLAY1("MethodExit events received: %d\n",
 138         MethodExitEventsCount);
 139     if (!NSK_VERIFY(MethodExitEventsCount != 0))
 140         nsk_jvmti_setFailStatus();
 141 
 142     NSK_TRACE(jni->DeleteGlobalRef(thread));
 143 
 144     if (!nsk_jvmti_resumeSync())
 145         return;
 146 }
 147 
 148 /* ========================================================================== */
 149 
 150 /** Agent library initialization. */
 151 #ifdef STATIC_BUILD
 152 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 153     return Agent_Initialize(jvm, options, reserved);
 154 }
 155 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 156     return Agent_Initialize(jvm, options, reserved);
 157 }
 158 JNIEXPORT jint JNI_OnLoad_ma10t003(JavaVM *jvm, char *options, void *reserved) {
 159     return JNI_VERSION_1_8;
 160 }
 161 #endif
 162 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 163     jvmtiEnv* jvmti = NULL;
 164     jvmtiCapabilities caps;
 165     jvmtiEventCallbacks callbacks;
 166 
 167     NSK_DISPLAY0("Agent_OnLoad\n");
 168 
 169     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 170         return JNI_ERR;
 171 
 172     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 173 
 174     if (!NSK_VERIFY((jvmti =
 175             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 176         return JNI_ERR;
 177 
 178     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 179         return JNI_ERR;
 180 
 181     memset(&caps, 0, sizeof(caps));
 182     caps.can_generate_method_exit_events = 1;
 183     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 184         return JNI_ERR;
 185     }
 186 
 187     memset(&callbacks, 0, sizeof(callbacks));
 188     callbacks.MethodExit = &MethodExit;
 189     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 190         return JNI_ERR;
 191 
 192     return JNI_OK;
 193 }
 194 
 195 /* ========================================================================== */
 196 
 197 }
< prev index next >