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 <stdio.h>
18
19 #include "android-base/logging.h"
20 #include "android-base/stringprintf.h"
21 #include "jni.h"
22 #include "jvmti.h"
23 #include "scoped_local_ref.h"
24
25 // Test infrastructure
26 #include "jni_helper.h"
27 #include "jvmti_helper.h"
28 #include "test_env.h"
29 #include "ti_macros.h"
30
31 namespace art {
32 namespace Test925ThreadGroups {
33
34 // private static native Object[] getThreadGroupInfo();
35 // // Returns an array where element 0 is an array of threads and element 1 is an array of groups.
36 // private static native Object[] getThreadGroupChildren();
37
Java_art_Test925_getTopThreadGroups(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED)38 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getTopThreadGroups(
39 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
40 jthreadGroup* groups;
41 jint group_count;
42 jvmtiError result = jvmti_env->GetTopThreadGroups(&group_count, &groups);
43 if (JvmtiErrorToException(env, jvmti_env, result)) {
44 return nullptr;
45 }
46
47 auto callback = [&](jint index) -> jobject {
48 return groups[index];
49 };
50 jobjectArray ret = CreateObjectArray(env, group_count, "java/lang/ThreadGroup", callback);
51
52 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(groups));
53
54 return ret;
55 }
56
Java_art_Test925_getThreadGroupInfo(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED,jthreadGroup group)57 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getThreadGroupInfo(
58 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthreadGroup group) {
59 jvmtiThreadGroupInfo info;
60 jvmtiError result = jvmti_env->GetThreadGroupInfo(group, &info);
61 if (JvmtiErrorToException(env, jvmti_env, result)) {
62 return nullptr;
63 }
64
65 auto callback = [&](jint index) -> jobject {
66 switch (index) {
67 // The parent.
68 case 0:
69 return info.parent;
70
71 // The name.
72 case 1:
73 return (info.name == nullptr) ? nullptr : env->NewStringUTF(info.name);
74
75 // The priority. Use a string for simplicity of construction.
76 case 2:
77 return env->NewStringUTF(android::base::StringPrintf("%d", info.max_priority).c_str());
78
79 // Whether it's a daemon. Use a string for simplicity of construction.
80 case 3:
81 return env->NewStringUTF(info.is_daemon == JNI_TRUE ? "true" : "false");
82 }
83 LOG(FATAL) << "Should not reach here";
84 UNREACHABLE();
85 };
86 return CreateObjectArray(env, 4, "java/lang/Object", callback);
87 }
88
Java_art_Test925_getThreadGroupChildren(JNIEnv * env,jclass Main_klass ATTRIBUTE_UNUSED,jthreadGroup group)89 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test925_getThreadGroupChildren(
90 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthreadGroup group) {
91 jint thread_count;
92 jthread* threads;
93 jint threadgroup_count;
94 jthreadGroup* groups;
95
96 jvmtiError result = jvmti_env->GetThreadGroupChildren(group,
97 &thread_count,
98 &threads,
99 &threadgroup_count,
100 &groups);
101 if (JvmtiErrorToException(env, jvmti_env, result)) {
102 return nullptr;
103 }
104
105 auto callback = [&](jint component_index) -> jobject {
106 if (component_index == 0) {
107 // Threads.
108 auto inner_callback = [&](jint index) {
109 return threads[index];
110 };
111 return CreateObjectArray(env, thread_count, "java/lang/Thread", inner_callback);
112 } else {
113 // Groups.
114 auto inner_callback = [&](jint index) {
115 return groups[index];
116 };
117 return CreateObjectArray(env, threadgroup_count, "java/lang/ThreadGroup", inner_callback);
118 }
119 };
120 jobjectArray ret = CreateObjectArray(env, 2, "java/lang/Object", callback);
121
122 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(threads));
123 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(groups));
124
125 return ret;
126 }
127
128 } // namespace Test925ThreadGroups
129 } // namespace art
130