1 /*
 2  * Copyright (c) 2020, 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 
34 static void JNICALL
35 agent_proc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
36   fatal(jni, "agent function was not expected to be called");
37 }
38 
39 JNIEXPORT jboolean JNICALL
40 Java_InterruptThreadTest_testJvmtiFunctionsInJNICall(JNIEnv *jni, jobject obji, jthread vthread) {
41   jvmtiError err = JVMTI_ERROR_NONE;
42 
43   LOG("testJvmtiFunctionsInJNICall: started\n");
44 
45   // test JVMTI InterruptThread
46   err = jvmti->InterruptThread(vthread);
47   check_jvmti_status(jni, err, "InterruptThread");
48 
49   LOG("testJvmtiFunctionsInJNICall: finished\n");
50 
51   return JNI_TRUE;
52 }
53 
54 extern JNIEXPORT jint JNICALL
55 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
56   jvmtiCapabilities caps;
57   jvmtiError err;
58 
59   LOG("Agent_OnLoad started\n");
60   if (jvm->GetEnv((void **)(&jvmti), JVMTI_VERSION) != JNI_OK) {
61     return JNI_ERR;
62   }
63 
64   memset(&caps, 0, sizeof (caps));
65   caps.can_signal_thread = 1;
66   caps.can_support_virtual_threads = 1;
67 
68   err = jvmti->AddCapabilities(&caps);
69   if (err != JVMTI_ERROR_NONE) {
70     LOG("error in JVMTI AddCapabilities: %d\n", err);
71     return JNI_ERR;
72   }
73 
74   LOG("Agent_OnLoad finished\n");
75   return JNI_OK;
76 }
77 
78 } // extern "C"