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 jlong timeout = 0;
  36 
  37 /* This is how long we verify that the thread has really suspended (in ms) */
  38 static jlong verificationTime = 5 * 1000;
  39 
  40 /* constant names */
  41 #define THREAD_NAME     "TestedThread"
  42 
  43 /* constants */
  44 #define EVENTS_COUNT    1
  45 
  46 /* events list */
  47 static jvmtiEvent eventsList[EVENTS_COUNT] = {
  48     JVMTI_EVENT_THREAD_END
  49 };
  50 
  51 static volatile int eventsReceived = 0;
  52 static jthread testedThread = NULL;
  53 
  54 /* ============================================================================= */
  55 
  56 /** Agent algorithm. */
  57 static void JNICALL
  58 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  59 
  60     NSK_DISPLAY0("Wait for thread to start\n");
  61     if (!nsk_jvmti_waitForSync(timeout))
  62         return;
  63 
  64     /* perform testing */
  65     {
  66         NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME);
  67         if (!NSK_VERIFY((testedThread =
  68                 nsk_jvmti_threadByName(THREAD_NAME)) != NULL))
  69             return;
  70         NSK_DISPLAY1("  ... found thread: %p\n", (void*)testedThread);
  71 
  72         eventsReceived = 0;
  73         NSK_DISPLAY1("Enable event: %s\n", "THREAD_END");
  74         if (!nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, eventsList, NULL))
  75             return;
  76 
  77         NSK_DISPLAY1("Suspend thread: %p\n", (void*)testedThread);
  78         if (!NSK_JVMTI_VERIFY(
  79                 NSK_CPP_STUB2(SuspendThread, jvmti, testedThread))) {
  80             nsk_jvmti_setFailStatus();
  81             return;
  82         }
  83 
  84         NSK_DISPLAY0("Let thread to run and finish\n");
  85         if (!nsk_jvmti_resumeSync())
  86             return;
  87 
  88         NSK_DISPLAY1("Check that THREAD_END event NOT received for timeout: %ld ms\n", (long)verificationTime);
  89         {
  90             jlong delta = 1000;
  91             jlong time;
  92             for (time = 0; time < verificationTime; time += delta) {
  93                 if (eventsReceived > 0) {
  94                     NSK_COMPLAIN0("Thread ran and finished after suspension\n");
  95                     nsk_jvmti_setFailStatus();
  96                     break;
  97                 }
  98                 nsk_jvmti_sleep(delta);
  99             }
 100         }
 101 
 102         NSK_DISPLAY1("Disable event: %s\n", "THREAD_END");
 103         if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, eventsList, NULL))
 104             return;
 105 
 106         NSK_DISPLAY1("Resume thread: %p\n", (void*)testedThread);
 107         if (!NSK_JVMTI_VERIFY(
 108                 NSK_CPP_STUB2(ResumeThread, jvmti, testedThread))) {
 109             nsk_jvmti_setFailStatus();
 110         }
 111 
 112         NSK_DISPLAY0("Wait for thread to finish\n");
 113         if (!nsk_jvmti_waitForSync(timeout))
 114             return;
 115 
 116         NSK_DISPLAY0("Delete thread reference\n");
 117         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedThread));
 118     }
 119 
 120     NSK_DISPLAY0("Let debugee to finish\n");
 121     if (!nsk_jvmti_resumeSync())
 122         return;
 123 }
 124 
 125 /* ============================================================================= */
 126 
 127 /** THREAD_END callback. */
 128 JNIEXPORT void JNICALL
 129 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 130     /* check if event is for tested thread */
 131     if (thread != NULL &&
 132                 NSK_CPP_STUB3(IsSameObject, jni, testedThread, thread)) {
 133         NSK_DISPLAY1("  ... received THREAD_END event for tested thread: %p\n", (void*)thread);
 134         eventsReceived++;
 135     } else {
 136         NSK_DISPLAY1("  ... received THREAD_END event for unknown thread: %p\n", (void*)thread);
 137     }
 138 }
 139 
 140 /* ============================================================================= */
 141 
 142 /** Agent library initialization. */
 143 #ifdef STATIC_BUILD
 144 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
 145     return Agent_Initialize(jvm, options, reserved);
 146 }
 147 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
 148     return Agent_Initialize(jvm, options, reserved);
 149 }
 150 JNIEXPORT jint JNI_OnLoad_suspendthrd002(JavaVM *jvm, char *options, void *reserved) {
 151     return JNI_VERSION_1_8;
 152 }
 153 #endif
 154 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 155     jvmtiEnv* jvmti = NULL;
 156 
 157     /* init framework and parse options */
 158     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 159         return JNI_ERR;
 160 
 161     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 162 
 163     /* create JVMTI environment */
 164     if (!NSK_VERIFY((jvmti =
 165             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 166         return JNI_ERR;
 167 
 168     /* add specific capabilities for suspending thread */
 169     {
 170         jvmtiCapabilities suspendCaps;
 171         memset(&suspendCaps, 0, sizeof(suspendCaps));
 172         suspendCaps.can_suspend = 1;
 173         if (!NSK_JVMTI_VERIFY(
 174                 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
 175             return JNI_ERR;
 176     }
 177 
 178     /* set callbacks for THREAD_END event */
 179     {
 180         jvmtiEventCallbacks callbacks;
 181         memset(&callbacks, 0, sizeof(callbacks));
 182         callbacks.ThreadEnd = callbackThreadEnd;
 183         if (!NSK_JVMTI_VERIFY(
 184                 NSK_CPP_STUB3(SetEventCallbacks, jvmti, &callbacks, sizeof(callbacks))))
 185         return JNI_ERR;
 186     }
 187 
 188     /* register agent proc and arg */
 189     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 190         return JNI_ERR;
 191 
 192     return JNI_OK;
 193 }
 194 
 195 /* ============================================================================= */
 196 
 197 }