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