blob: bcd813f1ce8bb5491d6e74fc048eeccd9f3a6fa9 [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
17#include "threads.h"
18
19#include <stdio.h>
20
21#include "android-base/stringprintf.h"
22#include "base/macros.h"
23#include "base/logging.h"
24#include "jni.h"
25#include "openjdkjvmti/jvmti.h"
26#include "ScopedLocalRef.h"
27
28#include "ti-agent/common_helper.h"
29#include "ti-agent/common_load.h"
30
31namespace art {
32namespace Test924Threads {
33
34// private static native Thread getCurrentThread();
35// private static native Object[] getThreadInfo(Thread t);
36
37extern "C" JNIEXPORT jthread JNICALL Java_Main_getCurrentThread(
38 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
39 jthread thread = nullptr;
40 jvmtiError result = jvmti_env->GetCurrentThread(&thread);
41 if (JvmtiErrorToException(env, result)) {
42 return nullptr;
43 }
44 return thread;
45}
46
47extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getThreadInfo(
48 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) {
49 jvmtiThreadInfo info;
50 memset(&info, 0, sizeof(jvmtiThreadInfo));
51
52 jvmtiError result = jvmti_env->GetThreadInfo(thread, &info);
53 if (JvmtiErrorToException(env, result)) {
54 return nullptr;
55 }
56
57 auto callback = [&](jint component_index) -> jobject {
58 switch (component_index) {
59 // The name.
60 case 0:
61 return (info.name == nullptr) ? nullptr : env->NewStringUTF(info.name);
62
63 // The priority. Use a string for simplicity of construction.
64 case 1:
65 return env->NewStringUTF(android::base::StringPrintf("%d", info.priority).c_str());
66
67 // Whether it's a daemon. Use a string for simplicity of construction.
68 case 2:
69 return env->NewStringUTF(info.is_daemon == JNI_TRUE ? "true" : "false");
70
71 // The thread group;
72 case 3:
73 return env->NewLocalRef(info.thread_group);
74
75 // The context classloader.
76 case 4:
77 return env->NewLocalRef(info.context_class_loader);
78 }
79 LOG(FATAL) << "Should not reach here";
80 UNREACHABLE();
81 };
82 jobjectArray ret = CreateObjectArray(env, 5, "java/lang/Object", callback);
83
84 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(info.name));
85 if (info.thread_group != nullptr) {
86 env->DeleteLocalRef(info.thread_group);
87 }
88 if (info.context_class_loader != nullptr) {
89 env->DeleteLocalRef(info.context_class_loader);
90 }
91
92 return ret;
93}
94
95// Don't do anything
96jint OnLoad(JavaVM* vm,
97 char* options ATTRIBUTE_UNUSED,
98 void* reserved ATTRIBUTE_UNUSED) {
99 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
100 printf("Unable to get jvmti env!\n");
101 return 1;
102 }
103 SetAllCapabilities(jvmti_env);
104 return 0;
105}
106
107} // namespace Test924Threads
108} // namespace art