Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame^] | 19 | #include "android-base/logging.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 21 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 22 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame^] | 23 | #include "scoped_local_ref.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 24 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 25 | // Test infrastructure |
| 26 | #include "jni_helper.h" |
| 27 | #include "jvmti_helper.h" |
| 28 | #include "test_env.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame^] | 29 | #include "ti_macros.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 | |
| 38 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_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); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 43 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 | |
| 57 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getThreadGroupInfo( |
| 58 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthreadGroup group) { |
| 59 | jvmtiThreadGroupInfo info; |
| 60 | jvmtiError result = jvmti_env->GetThreadGroupInfo(group, &info); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 61 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 | |
| 89 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_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); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 101 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 |