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 /* scaffold objects */
  35 static JNIEnv* jni = NULL;
  36 static jvmtiEnv *jvmti = NULL;
  37 static jlong timeout = 0;
  38 
  39 /* constants */
  40 #define THREADS_COUNT   6
  41 #define MAX_NAME_LENGTH 100
  42 #define MAX_STACK_SIZE  100
  43 
  44 /* thread description structure */
  45 typedef struct {
  46     char threadName[MAX_NAME_LENGTH];
  47     int minDepth;
  48     jthread thread;
  49 } ThreadDesc;
  50 
  51 /* descriptions of tested threads */
  52 static ThreadDesc threadsDesc[THREADS_COUNT] = {
  53     {"threadRunning", 2, NULL},
  54     {"threadEntering", 2, NULL},
  55     {"threadWaiting", 2, NULL},
  56     {"threadSleeping", 2, NULL},
  57     {"threadRunningInterrupted", 2, NULL},
  58     {"threadRunningNative", 2, NULL}
  59 };
  60 
  61 /* ============================================================================= */
  62 
  63 /* testcase(s) */
  64 static int prepare();
  65 static int checkSuspendedThreads();
  66 static int suspendThreadsIndividually(int suspend);
  67 static int clean();
  68 
  69 /* ============================================================================= */
  70 
  71 /** Agent algorithm. */
  72 static void JNICALL
  73 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
  74     jni = agentJNI;
  75 
  76     /* wait for initial sync */
  77     if (!nsk_jvmti_waitForSync(timeout))
  78         return;
  79 
  80     /* perform testcase(s) */
  81     {
  82         /* prepare data: find threads */
  83         NSK_DISPLAY0("Prepare data\n");
  84         if (!prepare()) {
  85             nsk_jvmti_setFailStatus();
  86             return;
  87         }
  88         /* suspend threads */
  89         NSK_DISPLAY0("Suspend each thread\n");
  90         if (!suspendThreadsIndividually(NSK_TRUE))
  91             return;
  92 
  93         /* check suspended threads */
  94         NSK_DISPLAY0("Check stack frames of suspended threads\n");
  95         if (!checkSuspendedThreads())
  96             return;
  97 
  98         /* resume threads */
  99         NSK_DISPLAY0("Resume each thread\n");
 100         if (!suspendThreadsIndividually(NSK_FALSE))
 101             return;
 102 
 103         /* clean date: delete threads references */
 104         NSK_DISPLAY0("Clean data\n");
 105         if (!clean()) {
 106             nsk_jvmti_setFailStatus();
 107             return;
 108         }
 109     }
 110 
 111     /* resume debugee after last sync */
 112     if (!nsk_jvmti_resumeSync())
 113         return;
 114 }
 115 
 116 /* ============================================================================= */
 117 
 118 /**
 119  * Prepare data:
 120  *    - clean threads list
 121  *    - get all live threads with names
 122  *    - find tested threads by name
 123  *    - make global refs
 124  */
 125 static int prepare() {
 126     jthread *allThreadsList = NULL;
 127     jint allThreadsCount = 0;
 128     int found = 0;
 129     int i;
 130 
 131     NSK_DISPLAY1("Find tested threads: %d\n", THREADS_COUNT);
 132 
 133     /* clean threads list */
 134     for (i = 0; i < THREADS_COUNT; i++) {
 135         threadsDesc[i].thread = (jthread)NULL;
 136     }
 137 
 138     /* get all live threads */
 139     if (!NSK_JVMTI_VERIFY(
 140             NSK_CPP_STUB3(GetAllThreads, jvmti, &allThreadsCount, &allThreadsList)))
 141         return NSK_FALSE;
 142 
 143     if (!NSK_VERIFY(allThreadsCount > 0 && allThreadsList != NULL))
 144         return NSK_FALSE;
 145 
 146     /* find tested threads */
 147     for (i = 0; i < allThreadsCount; i++) {
 148         jvmtiThreadInfo threadInfo;
 149 
 150         if (!NSK_VERIFY(allThreadsList[i] != NULL))
 151             return NSK_FALSE;
 152 
 153         /* get thread name (info) */
 154         if (!NSK_JVMTI_VERIFY(
 155                 NSK_CPP_STUB3(GetThreadInfo, jvmti, allThreadsList[i], &threadInfo)))
 156             return NSK_FALSE;
 157 
 158         /* find by name */
 159         if (threadInfo.name != NULL) {
 160             int j;
 161 
 162             for (j = 0; j < THREADS_COUNT; j++) {
 163                 if (strcmp(threadInfo.name, threadsDesc[j].threadName) == 0) {
 164                     threadsDesc[j].thread = allThreadsList[i];
 165                     NSK_DISPLAY3("    thread #%d (%s): %p\n",
 166                                             j, threadInfo.name, (void*)threadsDesc[j].thread);
 167                 }
 168             }
 169         }
 170     }
 171 
 172     /* deallocate all threads list */
 173     if (!NSK_JVMTI_VERIFY(
 174             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)allThreadsList)))
 175         return NSK_FALSE;
 176 
 177     /* check if all tested threads found */
 178     found = 0;
 179     for (i = 0; i < THREADS_COUNT; i++) {
 180         if (threadsDesc[i].thread == NULL) {
 181             NSK_COMPLAIN2("Not found tested thread #%d (%s)\n", i, threadsDesc[i].threadName);
 182         } else {
 183             found++;
 184         }
 185     }
 186 
 187     if (found < THREADS_COUNT)
 188         return NSK_FALSE;
 189 
 190     /* make global refs */
 191     for (i = 0; i < THREADS_COUNT; i++) {
 192         if (!NSK_JNI_VERIFY(jni, (threadsDesc[i].thread = (jthread)
 193                 NSK_CPP_STUB2(NewGlobalRef, jni, threadsDesc[i].thread)) != NULL))
 194             return NSK_FALSE;
 195     }
 196 
 197     return NSK_TRUE;
 198 }
 199 
 200 /**
 201  * Suspend or resume tested threads.
 202  */
 203 static int suspendThreadsIndividually(int suspend) {
 204     int i;
 205 
 206     for (i = 0; i < THREADS_COUNT; i++) {
 207         if (suspend) {
 208             NSK_DISPLAY2("    suspend thread #%d (%s)\n", i, threadsDesc[i].threadName);
 209             if (!NSK_JVMTI_VERIFY(
 210                     NSK_CPP_STUB2(SuspendThread, jvmti, threadsDesc[i].thread)))
 211                 nsk_jvmti_setFailStatus();
 212         } else {
 213             NSK_DISPLAY2("    resume thread #%d (%s)\n", i, threadsDesc[i].threadName);
 214             if (!NSK_JVMTI_VERIFY(
 215                     NSK_CPP_STUB2(ResumeThread, jvmti, threadsDesc[i].thread)))
 216                 nsk_jvmti_setFailStatus();
 217         }
 218     }
 219     return NSK_TRUE;
 220 }
 221 
 222 /**
 223  * Testcase: check tested threads
 224  *    - invoke getFrameCount() for each thread
 225  *    - check if frameCount is not less than minimal stack depth
 226  *    - invoke getStackTrace() for each thread
 227  *    - check if stack depth is equal to frameCount
 228  *
 229  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 230  */
 231 static int checkSuspendedThreads() {
 232     int i;
 233 
 234     /* check each thread */
 235     for (i = 0; i < THREADS_COUNT; i++) {
 236         jint frameCount = 0;
 237         jint frameStackSize = 0;
 238         jvmtiFrameInfo frameStack[MAX_STACK_SIZE];
 239         int found = 0;
 240 
 241         NSK_DISPLAY2("  thread #%d (%s):\n", i, threadsDesc[i].threadName);
 242 
 243         /* get frame count */
 244         if (!NSK_JVMTI_VERIFY(
 245                 NSK_CPP_STUB3(GetFrameCount, jvmti,
 246                                     threadsDesc[i].thread, &frameCount))) {
 247             nsk_jvmti_setFailStatus();
 248             return NSK_TRUE;
 249         }
 250 
 251         NSK_DISPLAY1("    frameCount:  %d\n", (int)frameCount);
 252 
 253         /* get stack trace */
 254         if (!NSK_JVMTI_VERIFY(
 255                 NSK_CPP_STUB6(GetStackTrace, jvmti, threadsDesc[i].thread,
 256                                     0, MAX_STACK_SIZE, frameStack, &frameStackSize))) {
 257             nsk_jvmti_setFailStatus();
 258             return NSK_TRUE;
 259         }
 260 
 261         NSK_DISPLAY1("    stack depth: %d\n", (int)frameStackSize);
 262 
 263         /* check frame count */
 264         if (frameCount < threadsDesc[i].minDepth) {
 265             NSK_COMPLAIN4("Too few frameCount of suspended thread #%d (%s):\n"
 266                             "#   got frameCount:   %d\n"
 267                             "#   expected minimum: %d\n",
 268                             i, threadsDesc[i].threadName,
 269                             (int)frameCount, threadsDesc[i].minDepth);
 270             nsk_jvmti_setFailStatus();
 271         }
 272 
 273         if (frameStackSize != frameCount) {
 274             NSK_COMPLAIN4("Different frames count for suspended thread #%d (%s):\n"
 275                             "#   getStackTrace(): %d\n"
 276                             "#   getFrameCount(): %d\n",
 277                             i, threadsDesc[i].threadName,
 278                             (int)frameStackSize, (int)frameCount);
 279             nsk_jvmti_setFailStatus();
 280         }
 281 
 282     }
 283 
 284     /* test may continue */
 285     return NSK_TRUE;
 286 }
 287 
 288 /**
 289  * Clean data:
 290  *   - dispose global references to tested threads
 291  */
 292 static int clean() {
 293     int i;
 294 
 295     /* dispose global references to threads */
 296     for (i = 0; i < THREADS_COUNT; i++) {
 297         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadsDesc[i].thread));
 298     }
 299 
 300     return NSK_TRUE;
 301 }
 302 
 303 /* ============================================================================= */
 304 
 305 static volatile int testedThreadRunning = NSK_FALSE;
 306 static volatile int testedThreadShouldFinish = NSK_FALSE;
 307 
 308 /** Native running method in tested thread. */
 309 JNIEXPORT void JNICALL
 310 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t001ThreadRunningNative_testedMethod(JNIEnv* jni,
 311                                                                                 jobject obj) {
 312     volatile int i = 0, n = 1000;
 313 
 314     /* run in a loop */
 315     testedThreadRunning = NSK_TRUE;
 316     while (!testedThreadShouldFinish) {
 317         if (n <= 0)
 318             n = 1000;
 319         if (i >= n)
 320             i = 0;
 321         i++;
 322     }
 323     testedThreadRunning = NSK_FALSE;
 324 }
 325 
 326 /** Wait for native method is running. */
 327 JNIEXPORT jboolean JNICALL
 328 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t001ThreadRunningNative_checkReady(JNIEnv* jni,
 329                                                                             jobject obj) {
 330     while (!testedThreadRunning) {
 331         nsk_jvmti_sleep(1000);
 332     }
 333     return testedThreadRunning ? JNI_TRUE : JNI_FALSE;
 334 }
 335 
 336 /** Let native method to finish. */
 337 JNIEXPORT void JNICALL
 338 Java_nsk_jvmti_scenarios_sampling_SP02_sp02t001ThreadRunningNative_letFinish(JNIEnv* jni,
 339                                                                             jobject obj) {
 340     testedThreadShouldFinish = NSK_TRUE;
 341 }
 342 
 343 /* ============================================================================= */
 344 
 345 /** Agent library initialization. */
 346 #ifdef STATIC_BUILD
 347 JNIEXPORT jint JNICALL Agent_OnLoad_sp02t001(JavaVM *jvm, char *options, void *reserved) {
 348     return Agent_Initialize(jvm, options, reserved);
 349 }
 350 JNIEXPORT jint JNICALL Agent_OnAttach_sp02t001(JavaVM *jvm, char *options, void *reserved) {
 351     return Agent_Initialize(jvm, options, reserved);
 352 }
 353 JNIEXPORT jint JNI_OnLoad_sp02t001(JavaVM *jvm, char *options, void *reserved) {
 354     return JNI_VERSION_1_8;
 355 }
 356 #endif
 357 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 358 
 359     /* init framework and parse options */
 360     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 361         return JNI_ERR;
 362 
 363     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 364 
 365     /* create JVMTI environment */
 366     if (!NSK_VERIFY((jvmti =
 367             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 368         return JNI_ERR;
 369 
 370     /* add specific capabilities for suspending thread */
 371     {
 372         jvmtiCapabilities suspendCaps;
 373         memset(&suspendCaps, 0, sizeof(suspendCaps));
 374         suspendCaps.can_suspend = 1;
 375         if (!NSK_JVMTI_VERIFY(
 376                 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
 377             return JNI_ERR;
 378     }
 379 
 380     /* register agent proc and arg */
 381     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 382         return JNI_ERR;
 383 
 384     return JNI_OK;
 385 }
 386 
 387 /* ============================================================================= */
 388 
 389 }