1 /*
  2  * Copyright (c) 2021, 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 <atomic>
 26 
 27 #include "jvmti.h"
 28 #include "jvmti_common.h"
 29 
 30 extern "C" {
 31 
 32 static jvmtiEnv *jvmti = NULL;
 33 static jrawMonitorID monitor;
 34 
 35 JNIEXPORT void JNICALL
 36 Java_RawMonitorTest_rawMonitorEnter(JNIEnv *env, jobject obj) {
 37   jthread vthread = get_current_thread(jvmti, env);
 38   jthread cthread = get_carrier_thread(jvmti, env, vthread);
 39   char*   vt_name = get_thread_name(jvmti, env, vthread);
 40   char*   ct_name = get_thread_name(jvmti, env, cthread);
 41 
 42   jvmtiError err = jvmti->RawMonitorEnter(monitor);
 43   check_jvmti_status(env, err, "Fatal Error in RawMonitorEnter");
 44 
 45   LOG("\n%s/%s: rawMonitorEnter: entered\n", vt_name, ct_name);
 46 
 47   deallocate(jvmti, env, (void*)vt_name);
 48   deallocate(jvmti, env, (void*)ct_name);
 49 }
 50 
 51 JNIEXPORT void JNICALL
 52 Java_RawMonitorTest_rawMonitorExit(JNIEnv *env, jobject obj) {
 53   jthread vthread = get_current_thread(jvmti, env);
 54   jthread cthread = get_carrier_thread(jvmti, env, vthread);
 55   char*   vt_name = get_thread_name(jvmti, env, vthread);
 56   char*   ct_name = get_thread_name(jvmti, env, cthread);
 57 
 58   jvmtiError err = jvmti->RawMonitorExit(monitor);
 59   check_jvmti_status(env, err, "Fatal Error in RawMonitorExit");
 60 
 61   LOG("%s/%s: rawMonitorExit: exited\n", vt_name, ct_name);
 62 
 63   deallocate(jvmti, env, (void*)vt_name);
 64   deallocate(jvmti, env, (void*)ct_name);
 65 }
 66 
 67 JNIEXPORT void JNICALL
 68 Java_RawMonitorTest_rawMonitorWait(JNIEnv *env, jobject obj) {
 69   jthread vthread = get_current_thread(jvmti, env);
 70   jthread cthread = get_carrier_thread(jvmti, env, vthread);
 71   char*   vt_name = get_thread_name(jvmti, env, vthread);
 72   char*   ct_name = get_thread_name(jvmti, env, cthread);
 73 
 74   LOG("%s/%s: rawMonitorWait: before waiting\n", vt_name, ct_name);
 75 
 76   jvmtiError err = jvmti->RawMonitorWait(monitor, 1L);
 77   check_jvmti_status(env, err, "Fatal Error in RawMonitorWait");
 78 
 79   LOG("\n%s/%s: rawMonitorWait: after waiting\n", vt_name, ct_name);
 80 
 81   deallocate(jvmti, env, (void*)vt_name);
 82   deallocate(jvmti, env, (void*)ct_name);
 83 }
 84 
 85 JNIEXPORT void JNICALL
 86 Java_RawMonitorTest_rawMonitorNotifyAll(JNIEnv *env, jobject obj) {
 87   jvmtiError err = jvmti->RawMonitorNotifyAll(monitor);
 88   check_jvmti_status(env, err, "Fatal Error in RawMonitorNotifyAll");
 89 
 90 }
 91 
 92 extern JNIEXPORT jint JNICALL
 93 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 94   jvmtiCapabilities caps;
 95   jvmtiError err;
 96 
 97   LOG("Agent_OnLoad started\n");
 98   if (jvm->GetEnv((void **)(&jvmti), JVMTI_VERSION) != JNI_OK) {
 99     return JNI_ERR;
100   }
101   memset(&caps, 0, sizeof (caps));
102   caps.can_support_virtual_threads = 1;
103 
104   err = jvmti->AddCapabilities(&caps);
105   if (err != JVMTI_ERROR_NONE) {
106     LOG("error in JVMTI AddCapabilities: %d\n", err);
107     return JNI_ERR;
108   }
109   monitor = create_raw_monitor(jvmti, "Raw monitor to test");
110 
111   LOG("Agent_OnLoad finished\n");
112   return JNI_OK;
113 }
114 
115 } // extern "C"