Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [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 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 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 | |
| 29 | namespace art { |
| 30 | namespace Test924Threads { |
| 31 | |
| 32 | // private static native Thread getCurrentThread(); |
| 33 | // private static native Object[] getThreadInfo(Thread t); |
| 34 | |
| 35 | extern "C" JNIEXPORT jthread JNICALL Java_Main_getCurrentThread( |
| 36 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) { |
| 37 | jthread thread = nullptr; |
| 38 | jvmtiError result = jvmti_env->GetCurrentThread(&thread); |
| 39 | if (JvmtiErrorToException(env, result)) { |
| 40 | return nullptr; |
| 41 | } |
| 42 | return thread; |
| 43 | } |
| 44 | |
| 45 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getThreadInfo( |
| 46 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) { |
| 47 | jvmtiThreadInfo info; |
| 48 | memset(&info, 0, sizeof(jvmtiThreadInfo)); |
| 49 | |
| 50 | jvmtiError result = jvmti_env->GetThreadInfo(thread, &info); |
| 51 | if (JvmtiErrorToException(env, result)) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
| 55 | auto callback = [&](jint component_index) -> jobject { |
| 56 | switch (component_index) { |
| 57 | // The name. |
| 58 | case 0: |
| 59 | return (info.name == nullptr) ? nullptr : env->NewStringUTF(info.name); |
| 60 | |
| 61 | // The priority. Use a string for simplicity of construction. |
| 62 | case 1: |
| 63 | return env->NewStringUTF(android::base::StringPrintf("%d", info.priority).c_str()); |
| 64 | |
| 65 | // Whether it's a daemon. Use a string for simplicity of construction. |
| 66 | case 2: |
| 67 | return env->NewStringUTF(info.is_daemon == JNI_TRUE ? "true" : "false"); |
| 68 | |
| 69 | // The thread group; |
| 70 | case 3: |
| 71 | return env->NewLocalRef(info.thread_group); |
| 72 | |
| 73 | // The context classloader. |
| 74 | case 4: |
| 75 | return env->NewLocalRef(info.context_class_loader); |
| 76 | } |
| 77 | LOG(FATAL) << "Should not reach here"; |
| 78 | UNREACHABLE(); |
| 79 | }; |
| 80 | jobjectArray ret = CreateObjectArray(env, 5, "java/lang/Object", callback); |
| 81 | |
| 82 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(info.name)); |
| 83 | if (info.thread_group != nullptr) { |
| 84 | env->DeleteLocalRef(info.thread_group); |
| 85 | } |
| 86 | if (info.context_class_loader != nullptr) { |
| 87 | env->DeleteLocalRef(info.context_class_loader); |
| 88 | } |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 93 | extern "C" JNIEXPORT jint JNICALL Java_Main_getThreadState( |
| 94 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) { |
| 95 | jint state; |
| 96 | jvmtiError result = jvmti_env->GetThreadState(thread, &state); |
| 97 | if (JvmtiErrorToException(env, result)) { |
| 98 | return 0; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 99 | } |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 100 | return state; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace Test924Threads |
| 104 | } // namespace art |