Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/SuspendWithCurrentThread/libSuspendWithCurrentThread.cpp
41155 views
1
/*
2
* Copyright (c) 2019, 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
#include "jvmti.h"
27
28
extern "C" {
29
30
static jvmtiEnv* jvmti = NULL;
31
static jthread* threads = NULL;
32
static jsize threads_count = 0;
33
static std::atomic<bool> is_exited_from_suspend;
34
35
#define LOG(...) \
36
do { \
37
printf(__VA_ARGS__); \
38
printf("\n"); \
39
fflush(stdout); \
40
} while (0)
41
42
static void
43
check_jvmti_status(JNIEnv* jni, jvmtiError err, const char* msg) {
44
if (err != JVMTI_ERROR_NONE) {
45
LOG("check_jvmti_status: JVMTI function returned error: %d", err);
46
jni->FatalError(msg);
47
}
48
}
49
50
JNIEXPORT void JNICALL
51
Java_SuspendWithCurrentThread_registerTestedThreads(JNIEnv *jni, jclass cls, jobjectArray threadsArr) {
52
LOG("\nregisterTestedThreads: started");
53
threads_count = jni->GetArrayLength(threadsArr);
54
55
jvmtiError err = jvmti->Allocate((threads_count * sizeof(jthread)),
56
(unsigned char**)&threads);
57
check_jvmti_status(jni, err, "registerTestedThreads: error in JVMTI Allocate threads array");
58
59
for (int i = 0; i < threads_count; i++) {
60
jobject elem = jni->GetObjectArrayElement(threadsArr, i);
61
threads[i] = (jthread)jni->NewGlobalRef(elem);
62
}
63
LOG("registerTestedThreads: finished\n");
64
}
65
66
/* This function is executed on the suspender thread which is not Main thread */
67
JNIEXPORT void JNICALL
68
Java_ThreadToSuspend_suspendTestedThreads(JNIEnv *jni, jclass cls) {
69
jvmtiError* results = NULL;
70
jvmtiError err;
71
72
LOG("\nsuspendTestedThreads: started");
73
err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
74
(unsigned char**)&results);
75
check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Allocate results array");
76
77
LOG("suspendTestedThreads: before JVMTI SuspendThreadList");
78
err = jvmti->SuspendThreadList(threads_count, threads, results);
79
is_exited_from_suspend.store(true);
80
check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI SuspendThreadList");
81
82
LOG("suspendTestedThreads: check and print SuspendThreadList results:");
83
for (int i = 0; i < threads_count; i++) {
84
LOG(" thread #%d: (%d)", i, (int)results[i]);
85
check_jvmti_status(jni, results[i], "suspendTestedThreads: error in SuspendThreadList results[i]");
86
}
87
LOG("suspendTestedThreads: finished\n");
88
89
err = jvmti->Deallocate((unsigned char*)results);
90
check_jvmti_status(jni, err, "suspendTestedThreads: error in JVMTI Deallocate results");
91
}
92
93
JNIEXPORT jboolean JNICALL
94
Java_SuspendWithCurrentThread_checkTestedThreadsSuspended(JNIEnv *jni, jclass cls) {
95
LOG("checkTestedThreadsSuspended: started");
96
97
for (int i = 0; i < threads_count; i++) {
98
jint state = 0;
99
jvmtiError err = jvmti->GetThreadState(threads[i], &state);
100
check_jvmti_status(jni, err, "checkTestedThreadsSuspended: error in GetThreadState");
101
102
if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
103
LOG("thread #%d has not been suspended yet: "
104
"# state: (%#x)", i, (int)state);
105
return JNI_FALSE;
106
}
107
}
108
if (is_exited_from_suspend.load()) {
109
LOG("Thread didn't stop in self suspend.");
110
return JNI_FALSE;
111
}
112
LOG("checkTestedThreadsSuspended: finished\n");
113
return JNI_TRUE;
114
}
115
116
JNIEXPORT void JNICALL
117
Java_SuspendWithCurrentThread_resumeTestedThreads(JNIEnv *jni, jclass cls) {
118
jvmtiError* results = NULL;
119
jvmtiError err;
120
121
LOG("\nresumeTestedThreads: started");
122
err = jvmti->Allocate((threads_count * sizeof(jvmtiError)),
123
(unsigned char**)&results);
124
check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Allocate results array");
125
126
LOG("resumeTestedThreads: before JVMTI ResumeThreadList");
127
err = jvmti->ResumeThreadList(threads_count, threads, results);
128
check_jvmti_status(jni, err, "resumeTestedThreads: error in ResumeThreadList");
129
130
LOG("resumeTestedThreads: check and print ResumeThreadList results:");
131
for (int i = 0; i < threads_count; i++) {
132
LOG(" thread #%d: (%d)", i, (int)results[i]);
133
check_jvmti_status(jni, results[i], "resumeTestedThreads: error in ResumeThreadList results[i]");
134
}
135
136
err = jvmti->Deallocate((unsigned char*)results);
137
check_jvmti_status(jni, err, "resumeTestedThreads: error in JVMTI Deallocate results");
138
139
LOG("resumeTestedThreads: finished\n");
140
}
141
142
JNIEXPORT void JNICALL
143
Java_SuspendWithCurrentThread_releaseTestedThreadsInfo(JNIEnv *jni, jclass cls) {
144
jvmtiError err;
145
146
LOG("\nreleaseTestedThreadsInfo: started");
147
148
for (int i = 0; i < threads_count; i++) {
149
if (threads[i] != NULL) {
150
jni->DeleteGlobalRef(threads[i]);
151
}
152
}
153
err = jvmti->Deallocate((unsigned char*)threads);
154
check_jvmti_status(jni, err, "releaseTestedThreadsInfo: error in JVMTI Deallocate threads");
155
156
LOG("releaseTestedThreadsInfo: finished\n");
157
}
158
159
160
/** Agent library initialization. */
161
162
JNIEXPORT jint JNICALL
163
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
164
LOG("\nAgent_OnLoad started");
165
is_exited_from_suspend.store(false);
166
// create JVMTI environment
167
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
168
return JNI_ERR;
169
}
170
171
// add specific capabilities for suspending thread
172
jvmtiCapabilities suspendCaps;
173
memset(&suspendCaps, 0, sizeof(suspendCaps));
174
suspendCaps.can_suspend = 1;
175
176
jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
177
if (err != JVMTI_ERROR_NONE) {
178
return JNI_ERR;
179
}
180
LOG("Agent_OnLoad finished\n");
181
return JNI_OK;
182
}
183
184
}
185
186