test/hotspot/jtreg/serviceability/jvmti/SuspendWithCurrentThread/libSuspendWithCurrentThread.cpp
author sspitsyn
Thu, 10 Oct 2019 01:10:26 +0000
changeset 58530 865c889ce351
permissions -rw-r--r--
8231595: [TEST] develop a test case for SuspendThreadList including current thread Summary: Extend test coverage for SuspendThreadList Reviewed-by: amenkov, cjplummer, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58530
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     1
/*
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     4
 *
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     8
 *
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    13
 * accompanied this code).
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    14
 *
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    18
 *
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    21
 * questions.
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    22
 */
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    23
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    24
#include <string.h>
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    25
#include "jvmti.h"
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    26
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    27
extern "C" {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    28
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    29
static jvmtiEnv* jvmti = NULL;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    30
static jthread* threads = NULL;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    31
static jsize threads_count = 0;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    32
static jrawMonitorID agent_monitor = NULL;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    33
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    34
#define LOG(...) \
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    35
  do { \
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    36
    printf(__VA_ARGS__); \
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    37
    printf("\n"); \
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    38
    fflush(stdout); \
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    39
  } while (0)
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    40
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    41
static void
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    42
check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    43
  if (err != JVMTI_ERROR_NONE) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    44
    LOG("check_jvmti_status: JVMTI function returned error: %d", err);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    45
    jni->FatalError(msg);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    46
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    47
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    48
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    49
static void
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    50
agent_lock(JNIEnv* jni) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    51
  jvmtiError err = jvmti->RawMonitorEnter(agent_monitor);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    52
  check_jvmti_status(jni, err, "monitor_enter: error in JVMTI RawMonitorEnter");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    53
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    54
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    55
static void
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    56
agent_unlock(JNIEnv* jni) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    57
  jvmtiError err = jvmti->RawMonitorExit(agent_monitor);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    58
  check_jvmti_status(jni, err, "monitor_exit: error in JVMTI RawMonitorExit");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    59
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    60
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    61
JNIEXPORT void JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    62
Java_SuspendWithCurrentThread_registerTestedThreads(JNIEnv *jni, jclass cls, jobjectArray threadsArr) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    63
  LOG("\nregisterTestedThreads: started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    64
  threads_count = jni->GetArrayLength(threadsArr);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    65
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    66
  jvmtiError err = jvmti->Allocate((threads_count * sizeof(jthread)),
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    67
                                   (unsigned char**)&threads);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    68
  check_jvmti_status(jni, err, "registerTestedThreads: error in JVMTI Allocate threads array");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    69
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    70
  for (int i = 0; i < threads_count; i++) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    71
    jobject elem = jni->GetObjectArrayElement(threadsArr, i);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    72
    threads[i] = (jthread)jni->NewGlobalRef(elem);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    73
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    74
  LOG("registerTestedThreads: finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    75
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    76
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    77
/* This function is executed on the suspender thread, not the Main thread */
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    78
JNIEXPORT void JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    79
Java_ThreadToSuspend_init(JNIEnv *jni, jclass cls) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    80
  jvmtiError err = jvmti->CreateRawMonitor("Agent monitor", &agent_monitor);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    81
  check_jvmti_status(jni, err, "Java_ThreadToSuspend_init: error in JVMTI CreateRawMonitor");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    82
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    83
  // Main thread has to wait for the suspender thread to complete tested threads suspension
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    84
  agent_lock(jni);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    85
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    86
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    87
/* This function is executed on the suspender thread which is not Main thread */
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    88
JNIEXPORT void JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    89
Java_ThreadToSuspend_suspendTestedThreads(JNIEnv *jni, jclass cls) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    90
  jvmtiError* results = NULL;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    91
  jvmtiError err;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    92
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    93
  LOG("\nsuspendTestedThreads: started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    94
  err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    95
                        (unsigned char**)&results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    96
  check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Allocate results array");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    97
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    98
  LOG("suspendTestedThreads: before JVMTI SuspendThreadList");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
    99
  err = jvmti->SuspendThreadList(threads_count, threads, results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   100
  check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI SuspendThreadList");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   101
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   102
  LOG("suspendTestedThreads: check and print SuspendThreadList results:");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   103
  for (int i = 0; i < threads_count; i++) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   104
    LOG("  thread #%d: (%d)", i, (int)results[i]);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   105
    check_jvmti_status(jni, results[i], "suspendTestedThreads: error in SuspendThreadList results[i]");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   106
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   107
  LOG("suspendTestedThreads: finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   108
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   109
  // Allow the Main thread to inspect the result of tested threads suspension
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   110
  agent_unlock(jni);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   111
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   112
  err = jvmti->Deallocate((unsigned char*)results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   113
  check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Deallocate results");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   114
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   115
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   116
JNIEXPORT jboolean JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   117
Java_SuspendWithCurrentThread_checkTestedThreadsSuspended(JNIEnv *jni, jclass cls) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   118
  LOG("checkTestedThreadsSuspended: started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   119
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   120
  // Block until the suspender thread competes the tested threads suspension
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   121
  agent_lock(jni);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   122
  agent_unlock(jni);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   123
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   124
  for (int i = 0; i < threads_count; i++) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   125
    jint state = 0;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   126
    jvmtiError err = jvmti->GetThreadState(threads[i], &state);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   127
    check_jvmti_status(jni, err, "checkTestedThreadsSuspended: error in GetThreadState");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   128
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   129
    if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   130
      LOG("thread #%d has not been suspended yet: "
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   131
             "#   state: (%#x)", i, (int)state);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   132
      jni->FatalError("checkTestedThreadsSuspended: error: expected all tested threads suspended");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   133
    }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   134
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   135
  LOG("checkTestedThreadsSuspended: finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   136
  return JNI_TRUE;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   137
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   138
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   139
JNIEXPORT void JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   140
Java_SuspendWithCurrentThread_resumeTestedThreads(JNIEnv *jni, jclass cls) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   141
  jvmtiError* results = NULL;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   142
  jvmtiError err;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   143
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   144
  LOG("\nresumeTestedThreads: started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   145
  err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   146
                        (unsigned char**)&results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   147
  check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Allocate results array");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   148
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   149
  LOG("resumeTestedThreads: before JVMTI ResumeThreadList");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   150
  err = jvmti->ResumeThreadList(threads_count, threads, results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   151
  check_jvmti_status(jni, err, "resumeTestedThreads: error in ResumeThreadList");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   152
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   153
  LOG("resumeTestedThreads: check and print ResumeThreadList results:");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   154
  for (int i = 0; i < threads_count; i++) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   155
    LOG("  thread #%d: (%d)", i, (int)results[i]);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   156
    check_jvmti_status(jni, results[i], "resumeTestedThreads: error in ResumeThreadList results[i]");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   157
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   158
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   159
  err = jvmti->Deallocate((unsigned char*)results);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   160
  check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Deallocate results");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   161
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   162
  LOG("resumeTestedThreads: finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   163
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   164
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   165
JNIEXPORT void JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   166
Java_SuspendWithCurrentThread_releaseTestedThreadsInfo(JNIEnv *jni, jclass cls) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   167
  jvmtiError err;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   168
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   169
  LOG("\nreleaseTestedThreadsInfo: started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   170
  err = jvmti->DestroyRawMonitor(agent_monitor);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   171
  check_jvmti_status(jni, err, "releaseTestedThreadsInfo: error in JVMTI DestroyRawMonitor");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   172
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   173
  for (int i = 0; i < threads_count; i++) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   174
    if (threads[i] != NULL) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   175
      jni->DeleteGlobalRef(threads[i]);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   176
    }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   177
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   178
  err = jvmti->Deallocate((unsigned char*)threads);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   179
  check_jvmti_status(jni, err, "releaseTestedThreadsInfo: error in JVMTI Deallocate threads");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   180
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   181
  LOG("releaseTestedThreadsInfo: finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   182
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   183
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   184
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   185
/** Agent library initialization. */
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   186
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   187
JNIEXPORT jint JNICALL
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   188
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   189
  LOG("\nAgent_OnLoad started");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   190
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   191
  // create JVMTI environment
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   192
  if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   193
    return JNI_ERR;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   194
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   195
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   196
  // add specific capabilities for suspending thread
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   197
  jvmtiCapabilities suspendCaps;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   198
  memset(&suspendCaps, 0, sizeof(suspendCaps));
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   199
  suspendCaps.can_suspend = 1;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   200
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   201
  jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   202
  if (err != JVMTI_ERROR_NONE) {
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   203
    return JNI_ERR;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   204
  }
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   205
  LOG("Agent_OnLoad finished\n");
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   206
  return JNI_OK;
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   207
}
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   208
865c889ce351 8231595: [TEST] develop a test case for SuspendThreadList including current thread
sspitsyn
parents:
diff changeset
   209
}