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