blob: 1487b7c64d5fa538a8c208578bdfc1c1332e31ae [file] [log] [blame]
Andreas Gampeaf13ab92017-01-11 20:57:40 -08001/*
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 Gampeaf13ab92017-01-11 20:57:40 -080017#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 Test924Threads {
31
32// private static native Thread getCurrentThread();
33// private static native Object[] getThreadInfo(Thread t);
34
35extern "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
45extern "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 Gampe72c19832017-01-12 13:22:16 -080093extern "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 Gampeaf13ab92017-01-11 20:57:40 -080099 }
Andreas Gampe72c19832017-01-12 13:22:16 -0800100 return state;
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800101}
102
Andreas Gampe85807442017-01-13 14:40:58 -0800103extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getAllThreads(
104 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
105 jint thread_count;
106 jthread* threads;
107
108 jvmtiError result = jvmti_env->GetAllThreads(&thread_count, &threads);
109 if (JvmtiErrorToException(env, result)) {
110 return nullptr;
111 }
112
113 auto callback = [&](jint index) {
114 return threads[index];
115 };
116 jobjectArray ret = CreateObjectArray(env, thread_count, "java/lang/Thread", callback);
117
118 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(threads));
119
120 return ret;
121}
122
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800123} // namespace Test924Threads
124} // namespace art