xref: /aosp_15_r20/art/test/ti-agent/suspension_helper.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jni.h"
18*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <vector>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
23*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker namespace common_suspension {
27*795d594fSAndroid Build Coastguard Worker 
Java_art_Suspension_isSuspended(JNIEnv * env,jclass,jthread thr)28*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jboolean JNICALL Java_art_Suspension_isSuspended(
29*795d594fSAndroid Build Coastguard Worker     JNIEnv* env, jclass, jthread thr) {
30*795d594fSAndroid Build Coastguard Worker   jint state;
31*795d594fSAndroid Build Coastguard Worker   if (JvmtiErrorToException(env, jvmti_env, jvmti_env->GetThreadState(thr, &state))) {
32*795d594fSAndroid Build Coastguard Worker     return false;
33*795d594fSAndroid Build Coastguard Worker   }
34*795d594fSAndroid Build Coastguard Worker   return (state & JVMTI_THREAD_STATE_SUSPENDED) != 0;
35*795d594fSAndroid Build Coastguard Worker }
36*795d594fSAndroid Build Coastguard Worker 
CopyToVector(JNIEnv * env,jobjectArray thrs)37*795d594fSAndroid Build Coastguard Worker static std::vector<jthread> CopyToVector(JNIEnv* env, jobjectArray thrs) {
38*795d594fSAndroid Build Coastguard Worker   jsize len = env->GetArrayLength(thrs);
39*795d594fSAndroid Build Coastguard Worker   std::vector<jthread> ret;
40*795d594fSAndroid Build Coastguard Worker   ret.reserve(len);
41*795d594fSAndroid Build Coastguard Worker   for (jsize i = 0; i < len; i++) {
42*795d594fSAndroid Build Coastguard Worker     ret.push_back(reinterpret_cast<jthread>(env->GetObjectArrayElement(thrs, i)));
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker   return ret;
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker 
Java_art_Suspension_resumeList(JNIEnv * env,jclass,jobjectArray thr)47*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jintArray JNICALL Java_art_Suspension_resumeList(JNIEnv* env,
48*795d594fSAndroid Build Coastguard Worker                                                                       jclass,
49*795d594fSAndroid Build Coastguard Worker                                                                       jobjectArray thr) {
50*795d594fSAndroid Build Coastguard Worker   static_assert(sizeof(jvmtiError) == sizeof(jint), "cannot use jintArray as jvmtiError array");
51*795d594fSAndroid Build Coastguard Worker   std::vector<jthread> threads(CopyToVector(env, thr));
52*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
53*795d594fSAndroid Build Coastguard Worker     return nullptr;
54*795d594fSAndroid Build Coastguard Worker   }
55*795d594fSAndroid Build Coastguard Worker   jintArray ret = env->NewIntArray(threads.size());
56*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
57*795d594fSAndroid Build Coastguard Worker     return nullptr;
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker   jint* elems = env->GetIntArrayElements(ret, nullptr);
60*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env,
61*795d594fSAndroid Build Coastguard Worker                         jvmti_env->ResumeThreadList(threads.size(),
62*795d594fSAndroid Build Coastguard Worker                                                     threads.data(),
63*795d594fSAndroid Build Coastguard Worker                                                     reinterpret_cast<jvmtiError*>(elems)));
64*795d594fSAndroid Build Coastguard Worker   env->ReleaseIntArrayElements(ret, elems, 0);
65*795d594fSAndroid Build Coastguard Worker   return ret;
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker 
Java_art_Suspension_suspendList(JNIEnv * env,jclass,jobjectArray thrs)68*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jintArray JNICALL Java_art_Suspension_suspendList(JNIEnv* env,
69*795d594fSAndroid Build Coastguard Worker                                                                        jclass,
70*795d594fSAndroid Build Coastguard Worker                                                                        jobjectArray thrs) {
71*795d594fSAndroid Build Coastguard Worker   static_assert(sizeof(jvmtiError) == sizeof(jint), "cannot use jintArray as jvmtiError array");
72*795d594fSAndroid Build Coastguard Worker   std::vector<jthread> threads(CopyToVector(env, thrs));
73*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
74*795d594fSAndroid Build Coastguard Worker     return nullptr;
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker   jintArray ret = env->NewIntArray(threads.size());
77*795d594fSAndroid Build Coastguard Worker   if (env->ExceptionCheck()) {
78*795d594fSAndroid Build Coastguard Worker     return nullptr;
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker   jint* elems = env->GetIntArrayElements(ret, nullptr);
81*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env,
82*795d594fSAndroid Build Coastguard Worker                         jvmti_env->SuspendThreadList(threads.size(),
83*795d594fSAndroid Build Coastguard Worker                                                      threads.data(),
84*795d594fSAndroid Build Coastguard Worker                                                      reinterpret_cast<jvmtiError*>(elems)));
85*795d594fSAndroid Build Coastguard Worker   env->ReleaseIntArrayElements(ret, elems, 0);
86*795d594fSAndroid Build Coastguard Worker   return ret;
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker 
Java_art_Suspension_resume(JNIEnv * env,jclass,jthread thr)89*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Suspension_resume(JNIEnv* env, jclass, jthread thr) {
90*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->ResumeThread(thr));
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker 
Java_art_Suspension_suspend(JNIEnv * env,jclass,jthread thr)93*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Suspension_suspend(JNIEnv* env, jclass, jthread thr) {
94*795d594fSAndroid Build Coastguard Worker   JvmtiErrorToException(env, jvmti_env, jvmti_env->SuspendThread(thr));
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker }  // namespace common_suspension
98*795d594fSAndroid Build Coastguard Worker }  // namespace art
99*795d594fSAndroid Build Coastguard Worker 
100