1 /*
   2  * Copyright (c) 2003, 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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 
  33 /* ============================================================================= */
  34 
  35 /* constant names */
  36 #define DEBUGEE_CLASS_NAME      "nsk/jvmti/scenarios/sampling/SP01/sp01t001"
  37 #define THREAD_CLASS_NAME       "nsk/jvmti/scenarios/sampling/SP01/sp01t001Thread"
  38 #define THREADS_FIELD_NAME      "threads"
  39 #define THREADS_FIELD_SIG       "[L" THREAD_CLASS_NAME ";"
  40 
  41 /* scaffold objects */
  42 static JNIEnv* jni = NULL;
  43 static jvmtiEnv *jvmti = NULL;
  44 static jlong timeout = 0;
  45 
  46 /* number of tested threads */
  47 #define THREADS_COUNT 2
  48 
  49 /* names of tested threads */
  50 static const char* threadsName[THREADS_COUNT] = {
  51     "NotStarted",
  52     "Finished"
  53 };
  54 
  55 #define JVMTI_THREAD_STATE_NOT_STARTED 0
  56 /* expected states of tested threads */
  57 static jint threadsState[THREADS_COUNT] = {
  58     JVMTI_THREAD_STATE_NOT_STARTED,    /* JVMTI_THREAD_STATUS_NOT_STARTED */
  59     JVMTI_THREAD_STATE_TERMINATED      /* JVMTI_THREAD_STATUS_ZOMBIE */
  60 };
  61 
  62 /* references to tested threads */
  63 static jthread threadsList[THREADS_COUNT];
  64 
  65 /* ============================================================================= */
  66 
  67 /* testcase(s) */
  68 static int prepare();
  69 static int checkThreads(const char* kind);
  70 static int clean();
  71 
  72 /* ============================================================================= */
  73 
  74 /** Agent algorithm. */
  75 static void JNICALL
  76 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
  77     jni = agentJNI;
  78 
  79     /* wait for initial sync */
  80     if (!nsk_jvmti_waitForSync(timeout))
  81         return;
  82 
  83     /* perform testcase(s) */
  84     {
  85         /* obtain threads list */
  86         if (!prepare()) {
  87             nsk_jvmti_setFailStatus();
  88             return;
  89         }
  90 
  91         /* testcase #1: check not alive threads */
  92         NSK_DISPLAY0("Testcase #1: check state of not alive threads\n");
  93         if (!checkThreads("not alive"))
  94             return;
  95 
  96         /* clean threads references */
  97         if (!clean()) {
  98             nsk_jvmti_setFailStatus();
  99             return;
 100         }
 101     }
 102 
 103     /* resume debugee after last sync */
 104     if (!nsk_jvmti_resumeSync())
 105         return;
 106 }
 107 
 108 /* ============================================================================= */
 109 
 110 /**
 111  * Prepare data:
 112  *    - get threads array from static field
 113  *    - get each thread from array
 114  *    - make global refs
 115  */
 116 static int prepare() {
 117     jclass debugeeClass = NULL;
 118     jfieldID threadsFieldID = NULL;
 119     jobjectArray threadsArray = NULL;
 120     jsize threadsArrayLength = 0;
 121     jsize i;
 122 
 123     /* find debugee class */
 124     if (!NSK_JNI_VERIFY(jni, (debugeeClass = jni->FindClass(DEBUGEE_CLASS_NAME)) != NULL))
 125         return NSK_FALSE;
 126 
 127     /* find static field with threads array */
 128     if (!NSK_JNI_VERIFY(jni, (threadsFieldID =
 129             jni->GetStaticFieldID(debugeeClass, THREADS_FIELD_NAME, THREADS_FIELD_SIG)) != NULL))
 130         return NSK_FALSE;
 131 
 132     /* get threads array from static field */
 133     if (!NSK_JNI_VERIFY(jni, (threadsArray = (jobjectArray)
 134             jni->GetStaticObjectField(debugeeClass, threadsFieldID)) != NULL))
 135         return NSK_FALSE;
 136 
 137     /* check array length */
 138     if (!NSK_JNI_VERIFY(jni, (threadsArrayLength =
 139             jni->GetArrayLength(threadsArray)) == THREADS_COUNT))
 140         return NSK_FALSE;
 141 
 142     /* get each thread from array */
 143     for (i = 0; i < THREADS_COUNT; i++) {
 144         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 145                 jni->GetObjectArrayElement(threadsArray, i)) != NULL))
 146             return NSK_FALSE;
 147     }
 148 
 149     /* make global references to threads */
 150     for (i = 0; i < THREADS_COUNT; i++) {
 151         if (!NSK_JNI_VERIFY(jni, (threadsList[i] = (jthread)
 152                 jni->NewGlobalRef(threadsList[i])) != NULL))
 153             return NSK_FALSE;
 154     }
 155 
 156     return NSK_TRUE;
 157 }
 158 
 159 /**
 160  * Testcase: check threads state for given suspension
 161  *
 162  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 163  */
 164 static int checkThreads(const char* kind) {
 165     int i;
 166 
 167     /* check each thread */
 168     for (i = 0; i < THREADS_COUNT; i++) {
 169         jthread thread = threadsList[i];
 170         jint state = JVMTI_THREAD_STATE_NOT_STARTED;
 171 
 172         NSK_DISPLAY2("    thread #%d (%s):\n", i, threadsName[i]);
 173 
 174         /* get thread state */
 175         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threadsList[i], &state))) {
 176             nsk_jvmti_setFailStatus();
 177             return NSK_TRUE;
 178         }
 179 
 180         /* display thread state */
 181         NSK_DISPLAY2("        state=%s (%d)\n",
 182                                 TranslateState(state), (int)state);
 183 
 184         /* check thread state */
 185         if ((state & threadsState[i]) == 0) {
 186             if (state == JVMTI_THREAD_STATE_NOT_STARTED) {
 187                 NSK_DISPLAY1("state of thread #%d is NOT_STARTED\n", kind);
 188             } else {
 189                 NSK_COMPLAIN7("Unexpected state of %s thread #%d (%s):\n"
 190                                 "#   got: %s (%d), expected: %s (%d)\n",
 191                                 kind, i, threadsName[i],
 192                                 TranslateState(state), (int)state,
 193                                 TranslateState(threadsState[i]), (int)threadsState[i]);
 194                 nsk_jvmti_setFailStatus();
 195             }
 196         }
 197 
 198         /* check SUSPENDED state flag */
 199         if (state & JVMTI_THREAD_STATE_SUSPENDED) {
 200             NSK_COMPLAIN3("Unexpected SUSPENDED state flag for %s thread #%d: %d\n",
 201                                                     kind, i, (int)state);
 202             nsk_jvmti_setFailStatus();
 203         }
 204 
 205         /* check INTERRUPTED state flag */
 206         if (state & JVMTI_THREAD_STATE_INTERRUPTED) {
 207             NSK_COMPLAIN3("Unexpected INTERRUPTED state flag for %s thread #%d: %d\n",
 208                                                     kind, i, (int)state);
 209             nsk_jvmti_setFailStatus();
 210         }
 211 
 212         /* check NATIVE state flag */
 213         if (state & JVMTI_THREAD_STATE_IN_NATIVE) {
 214             NSK_COMPLAIN3("Unexpected NATIVE state flag for %s thread #%d: %d\n",
 215                                                     kind, i, (int)state);
 216             nsk_jvmti_setFailStatus();
 217         }
 218     }
 219 
 220     /* test may continue */
 221     return NSK_TRUE;
 222 }
 223 
 224 /**
 225  * Clean data:
 226  *   - dispose global references to tested threads
 227  */
 228 static int clean() {
 229     int i;
 230 
 231     /* dispose global references to threads */
 232     for (i = 0; i < THREADS_COUNT; i++) {
 233         NSK_TRACE(jni->DeleteGlobalRef(threadsList[i]));
 234     }
 235 
 236     return NSK_TRUE;
 237 }
 238 
 239 /* ============================================================================= */
 240 
 241 /** Agent library initialization. */
 242 #ifdef STATIC_BUILD
 243 JNIEXPORT jint JNICALL Agent_OnLoad_sp01t001(JavaVM *jvm, char *options, void *reserved) {
 244     return Agent_Initialize(jvm, options, reserved);
 245 }
 246 JNIEXPORT jint JNICALL Agent_OnAttach_sp01t001(JavaVM *jvm, char *options, void *reserved) {
 247     return Agent_Initialize(jvm, options, reserved);
 248 }
 249 JNIEXPORT jint JNI_OnLoad_sp01t001(JavaVM *jvm, char *options, void *reserved) {
 250     return JNI_VERSION_1_8;
 251 }
 252 #endif
 253 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 254 
 255     /* init framework and parse options */
 256     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 257         return JNI_ERR;
 258 
 259     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 260 
 261     /* create JVMTI environment */
 262     if (!NSK_VERIFY((jvmti =
 263             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 264         return JNI_ERR;
 265 
 266     /* register agent proc and arg */
 267     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 268         return JNI_ERR;
 269 
 270     return JNI_OK;
 271 }
 272 
 273 /* ============================================================================= */
 274 
 275 }