1 /*
 2  * Copyright (c) 2004, 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 
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "jvmti_common.h"
28 
29 extern "C" {
30 
31 #define WAIT_START 100
32 
33 static jvmtiEnv *jvmti = NULL;
34 static jint wait_time = 0;
35 static jint state[] = {
36     0,                               /*  JVMTI_THREAD_STATUS_NOT_STARTED, */
37     JVMTI_THREAD_STATE_SLEEPING,
38     JVMTI_THREAD_STATE_TERMINATED    /*  JVMTI_THREAD_STATUS_ZOMBIE */
39 };
40 
41 JNIEXPORT void JNICALL
42 Java_thrstat03_init(JNIEnv *env, jclass cls, jint waitTime) {
43   wait_time = waitTime * 60000;
44 }
45 
46 jint Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
47   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
48   if (res != JNI_OK || jvmti == NULL) {
49     LOG("Wrong result of a valid call to GetEnv!\n");
50     return JNI_ERR;
51   }
52   return JNI_OK;
53 }
54 
55 JNIEXPORT jboolean JNICALL
56 Java_thrstat03_check(JNIEnv *jni, jclass cls, jthread thread, jint statInd) {
57   jboolean result = JNI_TRUE;
58   jrawMonitorID wait_lock;
59   jint thr_state = 0;
60 
61   if (jvmti == NULL) {
62     LOG("JVMTI client was not properly loaded!\n");
63     return JNI_FALSE;
64   }
65 
66   wait_lock = create_raw_monitor(jvmti, "_wait_lock");
67   for (int i = WAIT_START; i < wait_time; i <<= 1) {
68     thr_state = get_thread_state(jvmti, jni, thread);
69     LOG(">>> thread state: %s (%d)\n", TranslateState(thr_state), thr_state);
70 
71     if ((thr_state & JVMTI_THREAD_STATE_RUNNABLE) == 0) {
72       break;
73     }
74 
75     RawMonitorLocker rml = RawMonitorLocker(jvmti, jni, wait_lock);
76     rml.wait(i);
77   }
78 
79   destroy_raw_monitor(jvmti, jni,  wait_lock);
80 
81   /* We expect that thread is NOT_STARTED if statInd == 0 */
82   if (statInd == 0 && thr_state != state[statInd]) {
83     result = JNI_FALSE;
84   } else if (statInd != 0 && (thr_state & state[statInd]) == 0) {
85     result = JNI_FALSE;
86   }
87   if (result == JNI_FALSE) {
88     LOG("Wrong state: %s (%d)\n", TranslateState(thr_state), thr_state);
89     LOG("   expected: %s (%d)\n", TranslateState(state[statInd]), state[statInd]);
90   }
91 
92   return result;
93 }
94 
95 }