--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetThreadState/thrstat001/thrstat001.cpp Mon Nov 25 15:30:35 2019 -0800
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetThreadState/thrstat001/thrstat001.cpp Tue Nov 26 02:32:32 2019 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -39,63 +39,72 @@
static jvmtiCapabilities caps;
static jvmtiEventCallbacks callbacks;
static jrawMonitorID access_lock;
+static jrawMonitorID wait_lock;
static jint result = PASSED;
-static jboolean printdump = JNI_FALSE;
static jthread thr_ptr = NULL;
+
static jint state[] = {
JVMTI_THREAD_STATE_RUNNABLE,
JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER,
JVMTI_THREAD_STATE_IN_OBJECT_WAIT
};
-static int entry_count = 0;
-static int entry_error_count = 0;
-static int exit_count = 0;
-static int exit_error_count = 0;
+static void
+lock(const char* func_name, jrawMonitorID lock) {
+ jvmtiError err = jvmti->RawMonitorEnter(lock);
+ if (err != JVMTI_ERROR_NONE) {
+ printf("%s: unexpected error in RawMonitorEnter: %s (%d)\n",
+ func_name, TranslateError(err), err);
+ result = STATUS_FAILED;
+ }
+}
-void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
- jvmtiError err;
-
- err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
- JVMTI_EVENT_THREAD_START, NULL);
+static void
+unlock(const char* func_name, jrawMonitorID lock) {
+ jvmtiError err = jvmti->RawMonitorExit(lock);
if (err != JVMTI_ERROR_NONE) {
- printf("Failed to enable THREAD_START event: %s (%d)\n",
- TranslateError(err), err);
+ printf("%s: unexpected error in RawMonitorExit: %s (%d)\n",
+ func_name, TranslateError(err), err);
result = STATUS_FAILED;
}
+}
- if (caps.can_generate_method_entry_events) {
- err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
- JVMTI_EVENT_METHOD_ENTRY, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to enable METHOD_ENTRY event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
+static void
+wait(const char* func_name, jrawMonitorID lock, jint millis) {
+ jvmtiError err = jvmti->RawMonitorWait(lock, (jlong)millis);
+ if (err != JVMTI_ERROR_NONE) {
+ printf("%s: unexpected error in RawMonitorWait: %s (%d)\n",
+ func_name, TranslateError(err), err);
+ result = STATUS_FAILED;
}
+}
- if (caps.can_generate_method_exit_events) {
- err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
- JVMTI_EVENT_METHOD_EXIT, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to enable METHOD_EXIT event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
+static void
+set_notification_mode(const char* event_name,
+ jvmtiEventMode mode,
+ jvmtiEvent event_type,
+ jthread event_thread) {
+ const char* action = (mode == JVMTI_ENABLE) ? "enable" : "disable";
+ jvmtiError err = jvmti->SetEventNotificationMode(mode, event_type, event_thread);
+
+ if (err != JVMTI_ERROR_NONE) {
+ printf("Failed to %s %s event: %s (%d)\n",
+ action, event_name, TranslateError(err), err);
+ result = STATUS_FAILED;
}
}
+void JNICALL VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
+ set_notification_mode("JVMTI_EVENT_THREAD_START", JVMTI_ENABLE,
+ JVMTI_EVENT_THREAD_START, NULL);
+}
+
void JNICALL
ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
jvmtiError err;
jvmtiThreadInfo thrInfo;
- err = jvmti_env->RawMonitorEnter(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorEnter#TS) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
+ lock("ThreadStart", access_lock);
err = jvmti_env->GetThreadInfo(thread, &thrInfo);
if (err != JVMTI_ERROR_NONE) {
@@ -105,111 +114,12 @@
}
if (thrInfo.name != NULL && strcmp(thrInfo.name, "thr1") == 0) {
thr_ptr = env->NewGlobalRef(thread);
- if (printdump == JNI_TRUE) {
- printf(">>> ThreadStart: \"%s\", 0x%p\n", thrInfo.name, thr_ptr);
- }
- }
-
- err = jvmti_env->RawMonitorExit(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorExit#TS) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
-}
-
-void JNICALL MethodEntry(jvmtiEnv *jvmti_env, JNIEnv *env,
- jthread thread, jmethodID mid) {
- jvmtiError err;
- jvmtiThreadInfo thrInfo;
- jint thrState;
-
- err = jvmti_env->RawMonitorEnter(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorEnter#ME) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
-
- entry_count++;
- err = jvmti_env->GetThreadState(thread, &thrState);
- if (err != JVMTI_ERROR_NONE) {
- printf("(GetThreadState#ME) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- if ((thrState & JVMTI_THREAD_STATE_RUNNABLE) == 0) {
- if (entry_error_count == 0) {
- err = jvmti_env->GetThreadInfo(thread, &thrInfo);
- if (err != JVMTI_ERROR_NONE) {
- printf("(GetThreadInfo#ME) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- printf("Wrong thread \"%s\" state on MethodEntry event:\n",
- thrInfo.name);
- printf(" expected: JVMTI_THREAD_STATE_RUNNABLE\n");
- printf(" got: %s (%d)\n",
- TranslateState(thrState), thrState);
- }
- entry_error_count++;
- result = STATUS_FAILED;
+ printf(">>> ThreadStart: \"%s\", 0x%p\n", thrInfo.name, thr_ptr);
+ set_notification_mode("JVMTI_EVENT_THREAD_START", JVMTI_DISABLE,
+ JVMTI_EVENT_THREAD_START, NULL);
}
- err = jvmti_env->RawMonitorExit(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorExit#ME) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
-
-}
-
-void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env,
- jthread thread, jmethodID mid,
- jboolean was_poped_by_exception, jvalue return_value) {
- jvmtiError err;
- jvmtiThreadInfo thrInfo;
- jint thrState;
-
- err = jvmti_env->RawMonitorEnter(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorEnter#MX) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
-
- exit_count++;
- err = jvmti_env->GetThreadState(thread, &thrState);
- if (err != JVMTI_ERROR_NONE) {
- printf("(GetThreadState#MX) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- if ((thrState & JVMTI_THREAD_STATE_RUNNABLE) == 0) {
- if (exit_error_count == 0) {
- err = jvmti_env->GetThreadInfo(thread, &thrInfo);
- if (err != JVMTI_ERROR_NONE) {
- printf("(GetThreadInfo#MX) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- printf("Wrong thread \"%s\" state on MethodExit event:\n",
- thrInfo.name);
- printf(" expected: JVMTI_THREAD_STATE_RUNNABLE\n");
- printf(" got: %s (%d)\n",
- TranslateState(thrState), thrState);
- }
- exit_error_count++;
- result = STATUS_FAILED;
- }
-
- err = jvmti_env->RawMonitorExit(access_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorExit#MX) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
+ unlock("ThreadStart", access_lock);
}
#ifdef STATIC_BUILD
@@ -223,13 +133,12 @@
return JNI_VERSION_1_8;
}
#endif
+
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
jint res;
jvmtiError err;
- if (options != NULL && strcmp(options, "printdump") == 0) {
- printdump = JNI_TRUE;
- }
+ printf("Agent_Initialize started\n");
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
if (res != JNI_OK || jvmti == NULL) {
@@ -260,23 +169,20 @@
err = jvmti->CreateRawMonitor("_access_lock", &access_lock);
if (err != JVMTI_ERROR_NONE) {
- printf("(CreateRawMonitor) unexpected error: %s (%d)\n",
+ printf("(CreateRawMonitor)#access_lock unexpected error: %s (%d)\n",
+ TranslateError(err), err);
+ return JNI_ERR;
+ }
+
+ err = jvmti->CreateRawMonitor("_wait_lock", &wait_lock);
+ if (err != JVMTI_ERROR_NONE) {
+ printf("(CreateRawMonitor#wait_lock) unexpected error: %s (%d)\n",
TranslateError(err), err);
return JNI_ERR;
}
callbacks.VMInit = &VMInit;
callbacks.ThreadStart = &ThreadStart;
- if (caps.can_generate_method_entry_events) {
- callbacks.MethodEntry = &MethodEntry;
- } else {
- printf("Warning: MethodEntry event is not implemented\n");
- }
- if (caps.can_generate_method_exit_events) {
- callbacks.MethodExit = &MethodExit;
- } else {
- printf("Warning: MethodExit event is not implemented\n");
- }
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
if (err != JVMTI_ERROR_NONE) {
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
@@ -284,14 +190,10 @@
return JNI_ERR;
}
- err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
- JVMTI_EVENT_VM_INIT, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to enable VM_INIT event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
+ set_notification_mode("JVMTI_EVENT_VM_INIT", JVMTI_ENABLE,
+ JVMTI_EVENT_VM_INIT, NULL);
+ printf("Agent_Initialize finished\n\n");
return JNI_OK;
}
@@ -299,10 +201,10 @@
Java_nsk_jvmti_GetThreadState_thrstat001_checkStatus(JNIEnv *env,
jclass cls, jint statInd) {
jvmtiError err;
- jrawMonitorID wait_lock;
jint thrState;
jint millis;
+ printf("native method checkStatus started\n");
if (jvmti == NULL) {
printf("JVMTI client was not properly loaded!\n");
result = STATUS_FAILED;
@@ -316,12 +218,6 @@
}
/* wait until thread gets an expected state */
- err = jvmti->CreateRawMonitor("_wait_lock", &wait_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(CreateRawMonitor) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
for (millis = WAIT_START; millis < WAIT_TIME; millis <<= 1) {
err = jvmti->GetThreadState(thr_ptr, &thrState);
if (err != JVMTI_ERROR_NONE) {
@@ -332,36 +228,13 @@
if ((thrState & state[statInd]) != 0) {
break;
}
- err = jvmti->RawMonitorEnter(wait_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorEnter) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- err = jvmti->RawMonitorWait(wait_lock, (jlong)millis);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorWait) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- err = jvmti->RawMonitorExit(wait_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(RawMonitorExit) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- }
- err = jvmti->DestroyRawMonitor(wait_lock);
- if (err != JVMTI_ERROR_NONE) {
- printf("(DestroyRawMonitor) unexpected error: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
+ lock("checkStatus", wait_lock);
+ wait("checkStatus", wait_lock, millis);
+ unlock("checkStatus", wait_lock);
}
- if (printdump == JNI_TRUE) {
- printf(">>> thread \"thr1\" (0x%p) state: %s (%d)\n",
+ printf(">>> thread \"thr1\" (0x%p) state: %s (%d)\n",
thr_ptr, TranslateState(thrState), thrState);
- }
if ((thrState & state[statInd]) == 0) {
printf("Wrong thread \"thr1\" (0x%p) state:\n", thr_ptr);
@@ -371,55 +244,12 @@
TranslateState(thrState), thrState);
result = STATUS_FAILED;
}
+ printf("native method checkStatus finished\n\n");
}
JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetThreadState_thrstat001_getRes(JNIEnv *env, jclass cls) {
- jvmtiError err;
-
- err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
- JVMTI_EVENT_THREAD_START, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to disable THREAD_START event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
-
- if (caps.can_generate_method_entry_events) {
- err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
- JVMTI_EVENT_METHOD_ENTRY, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to disable METHOD_ENTRY event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- }
-
- if (caps.can_generate_method_exit_events) {
- err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
- JVMTI_EVENT_METHOD_EXIT, NULL);
- if (err != JVMTI_ERROR_NONE) {
- printf("Failed to disable METHOD_EXIT event: %s (%d)\n",
- TranslateError(err), err);
- result = STATUS_FAILED;
- }
- }
-
- if (printdump == JNI_TRUE) {
- printf(">>> total number of method entry events = %d\n", entry_count);
- printf(">>> total number of method exit events = %d\n", exit_count);
- }
-
- if (entry_error_count != 0) {
- printf("Total number of errors on METHOD_ENTRY: %d of %d events\n",
- entry_error_count, entry_count);
- }
-
- if (exit_error_count != 0) {
- printf("Total number of errors on METHOD_EXIT: %d of %d events\n",
- exit_error_count, exit_count);
- }
-
+ printf("native method getRes: result: %d\n\n", result);
return result;
}