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 /* constant names */
  38 #define THREAD_NAME     "TestedThread"
  39 
  40 /* constants */
  41 #define DEFAULT_THREADS_COUNT   10
  42 
  43 static int threadsCount = 0;
  44 
  45 /* ============================================================================= */
  46 
  47 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
  48                                 const char name[], int foundCount, jthread foundThreads[]);
  49 
  50 /** Agent algorithm. */
  51 static void JNICALL
  52 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  53 
  54     NSK_DISPLAY0("Wait for threads to start\n");
  55     if (!nsk_jvmti_waitForSync(timeout))
  56         return;
  57 
  58     /* perform testing */
  59     {
  60         jthread* threads = NULL;
  61         jvmtiError* results = NULL;
  62         int i;
  63 
  64         NSK_DISPLAY1("Allocate threads array: %d threads\n", threadsCount);
  65         if (!NSK_JVMTI_VERIFY(jvmti->Allocate((threadsCount * sizeof(jthread)),
  66                                               (unsigned char**)&threads))) {
  67             nsk_jvmti_setFailStatus();
  68             return;
  69         }
  70         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  71 
  72         NSK_DISPLAY1("Allocate results array: %d threads\n", threadsCount);
  73         if (!NSK_JVMTI_VERIFY(jvmti->Allocate((threadsCount * sizeof(jvmtiError)),
  74                                               (unsigned char**)&results))) {
  75             nsk_jvmti_setFailStatus();
  76             return;
  77         }
  78         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  79 
  80         NSK_DISPLAY1("Find threads: %d threads\n", threadsCount);
  81         if (!NSK_VERIFY(fillThreadsByName(jvmti, jni, THREAD_NAME, threadsCount, threads)))
  82             return;
  83 
  84         NSK_DISPLAY0("Suspend threads list\n");
  85         if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(threadsCount, threads, results))) {
  86             nsk_jvmti_setFailStatus();
  87             return;
  88         }
  89 
  90         NSK_DISPLAY0("Check threads results:\n");
  91         for (i = 0; i < threadsCount; i++) {
  92             NSK_DISPLAY3("  ... thread #%d: %s (%d)\n",
  93                                 i, TranslateError(results[i]), (int)results[i]);
  94             if (!NSK_JVMTI_VERIFY(results[i]))
  95                 nsk_jvmti_setFailStatus();
  96         }
  97 
  98         NSK_DISPLAY0("Let threads to run and finish\n");
  99         if (!nsk_jvmti_resumeSync())
 100             return;
 101 
 102         NSK_DISPLAY0("Get state vector for each thread\n");
 103         for (i = 0; i < threadsCount; i++) {
 104             jint state = 0;
 105 
 106             NSK_DISPLAY2("  thread #%d (%p):\n", i, (void*)threads[i]);
 107             if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threads[i], &state))) {
 108                 nsk_jvmti_setFailStatus();
 109             }
 110             NSK_DISPLAY2("  ... got state vector: %s (%d)\n",
 111                             TranslateState(state), (int)state);
 112 
 113             if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
 114                 NSK_COMPLAIN3("SuspendThreadList() does not turn on flag SUSPENDED for thread #%i:\n"
 115                               "#   state: %s (%d)\n",
 116                                 i, TranslateState(state), (int)state);
 117                 nsk_jvmti_setFailStatus();
 118             }
 119         }
 120 
 121         NSK_DISPLAY0("Resume threads list\n");
 122         if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(threadsCount, threads, results))) {
 123             nsk_jvmti_setFailStatus();
 124             return;
 125         }
 126 
 127         NSK_DISPLAY0("Wait for thread to finish\n");
 128         if (!nsk_jvmti_waitForSync(timeout))
 129             return;
 130 
 131         NSK_DISPLAY0("Delete threads references\n");
 132         for (i = 0; i < threadsCount; i++) {
 133             if (threads[i] != NULL)
 134                 NSK_TRACE(jni->DeleteGlobalRef(threads[i]));
 135         }
 136 
 137         NSK_DISPLAY1("Deallocate threads array: %p\n", (void*)threads);
 138         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads))) {
 139             nsk_jvmti_setFailStatus();
 140         }
 141 
 142         NSK_DISPLAY1("Deallocate results array: %p\n", (void*)results);
 143         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)results))) {
 144             nsk_jvmti_setFailStatus();
 145         }
 146     }
 147 
 148     NSK_DISPLAY0("Let debugee to finish\n");
 149     if (!nsk_jvmti_resumeSync())
 150         return;
 151 }
 152 
 153 /* ============================================================================= */
 154 
 155 /** Find threads whose name starts with specified name prefix. */
 156 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
 157                         const char name[], int foundCount, jthread foundThreads[]) {
 158     jint count = 0;
 159     jthread* threads = NULL;
 160 
 161     size_t len = strlen(name);
 162     int found = 0;
 163     int i;
 164 
 165     for (i = 0; i < foundCount; i++) {
 166         foundThreads[i] = NULL;
 167     }
 168 
 169     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&count, &threads))) {
 170         nsk_jvmti_setFailStatus();
 171         return NSK_FALSE;
 172     }
 173 
 174     found = 0;
 175     for (i = 0; i < count; i++) {
 176         jvmtiThreadInfo info;
 177 
 178         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info))) {
 179             nsk_jvmti_setFailStatus();
 180             break;
 181         }
 182 
 183         if (info.name != NULL && strncmp(name, info.name, len) == 0) {
 184             NSK_DISPLAY3("  ... found thread #%d: %p (%s)\n",
 185                                     found, threads[i], info.name);
 186             if (found < foundCount)
 187                 foundThreads[found] = threads[i];
 188             found++;
 189         }
 190 
 191     }
 192 
 193     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads))) {
 194         nsk_jvmti_setFailStatus();
 195         return NSK_FALSE;
 196     }
 197 
 198     if (found != foundCount) {
 199         NSK_COMPLAIN3("Unexpected number of tested threads found:\n"
 200                       "#   name:     %s\n"
 201                       "#   found:    %d\n"
 202                       "#   expected: %d\n",
 203                       name, found, foundCount);
 204         nsk_jvmti_setFailStatus();
 205         return NSK_FALSE;
 206     }
 207 
 208     NSK_DISPLAY1("Make global references for threads: %d threads\n", foundCount);
 209     for (i = 0; i < foundCount; i++) {
 210         if (!NSK_JNI_VERIFY(jni, (foundThreads[i] = (jthread)
 211                     jni->NewGlobalRef(foundThreads[i])) != NULL)) {
 212             nsk_jvmti_setFailStatus();
 213             return NSK_FALSE;
 214         }
 215         NSK_DISPLAY2("  ... thread #%d: %p\n", i, foundThreads[i]);
 216     }
 217 
 218     return NSK_TRUE;
 219 }
 220 
 221 /* ============================================================================= */
 222 
 223 /** Agent library initialization. */
 224 #ifdef STATIC_BUILD
 225 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 226     return Agent_Initialize(jvm, options, reserved);
 227 }
 228 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 229     return Agent_Initialize(jvm, options, reserved);
 230 }
 231 JNIEXPORT jint JNI_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 232     return JNI_VERSION_1_8;
 233 }
 234 #endif
 235 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 236     jvmtiEnv* jvmti = NULL;
 237 
 238     /* init framework and parse options */
 239     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 240         return JNI_ERR;
 241 
 242     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 243 
 244     /* get options */
 245     threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_COUNT);
 246     if (!NSK_VERIFY(threadsCount > 0))
 247         return JNI_ERR;
 248 
 249     /* create JVMTI environment */
 250     if (!NSK_VERIFY((jvmti =
 251             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 252         return JNI_ERR;
 253 
 254     /* add specific capabilities for suspending thread */
 255     {
 256         jvmtiCapabilities suspendCaps;
 257         memset(&suspendCaps, 0, sizeof(suspendCaps));
 258         suspendCaps.can_suspend = 1;
 259         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))
 260             return JNI_ERR;
 261     }
 262 
 263     /* register agent proc and arg */
 264     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 265         return JNI_ERR;
 266 
 267     return JNI_OK;
 268 }
 269 
 270 /* ============================================================================= */
 271 
 272 }