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 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 103 | extern "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 Gampe | 7b3b326 | 2017-01-19 20:40:42 -0800 | [diff] [blame] | 123 | extern "C" JNIEXPORT jlong JNICALL Java_Main_getTLS( |
| 124 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread) { |
| 125 | void* tls; |
| 126 | jvmtiError result = jvmti_env->GetThreadLocalStorage(thread, &tls); |
| 127 | if (JvmtiErrorToException(env, result)) { |
| 128 | return 0; |
| 129 | } |
| 130 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(tls)); |
| 131 | } |
| 132 | |
| 133 | extern "C" JNIEXPORT void JNICALL Java_Main_setTLS( |
| 134 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jthread thread, jlong val) { |
| 135 | const void* tls = reinterpret_cast<void*>(static_cast<uintptr_t>(val)); |
| 136 | jvmtiError result = jvmti_env->SetThreadLocalStorage(thread, tls); |
| 137 | JvmtiErrorToException(env, result); |
| 138 | } |
| 139 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame^] | 140 | static void JNICALL ThreadEvent(jvmtiEnv* jvmti_env, |
| 141 | JNIEnv* jni_env, |
| 142 | jthread thread, |
| 143 | bool is_start) { |
| 144 | jvmtiThreadInfo info; |
| 145 | jvmtiError result = jvmti_env->GetThreadInfo(thread, &info); |
| 146 | if (result != JVMTI_ERROR_NONE) { |
| 147 | printf("Error getting thread info"); |
| 148 | return; |
| 149 | } |
| 150 | printf("Thread(%s): %s\n", info.name, is_start ? "start" : "end"); |
| 151 | |
| 152 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(info.name)); |
| 153 | jni_env->DeleteLocalRef(info.thread_group); |
| 154 | jni_env->DeleteLocalRef(info.context_class_loader); |
| 155 | } |
| 156 | |
| 157 | static void JNICALL ThreadStart(jvmtiEnv* jvmti_env, |
| 158 | JNIEnv* jni_env, |
| 159 | jthread thread) { |
| 160 | ThreadEvent(jvmti_env, jni_env, thread, true); |
| 161 | } |
| 162 | |
| 163 | static void JNICALL ThreadEnd(jvmtiEnv* jvmti_env, |
| 164 | JNIEnv* jni_env, |
| 165 | jthread thread) { |
| 166 | ThreadEvent(jvmti_env, jni_env, thread, false); |
| 167 | } |
| 168 | |
| 169 | extern "C" JNIEXPORT void JNICALL Java_Main_enableThreadEvents( |
| 170 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jboolean b) { |
| 171 | if (b == JNI_FALSE) { |
| 172 | jvmtiError ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, |
| 173 | JVMTI_EVENT_THREAD_START, |
| 174 | nullptr); |
| 175 | if (JvmtiErrorToException(env, ret)) { |
| 176 | return; |
| 177 | } |
| 178 | ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, |
| 179 | JVMTI_EVENT_THREAD_END, |
| 180 | nullptr); |
| 181 | JvmtiErrorToException(env, ret); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | jvmtiEventCallbacks callbacks; |
| 186 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 187 | callbacks.ThreadStart = ThreadStart; |
| 188 | callbacks.ThreadEnd = ThreadEnd; |
| 189 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 190 | if (JvmtiErrorToException(env, ret)) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, |
| 195 | JVMTI_EVENT_THREAD_START, |
| 196 | nullptr); |
| 197 | if (JvmtiErrorToException(env, ret)) { |
| 198 | return; |
| 199 | } |
| 200 | ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, |
| 201 | JVMTI_EVENT_THREAD_END, |
| 202 | nullptr); |
| 203 | JvmtiErrorToException(env, ret); |
| 204 | } |
| 205 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 206 | } // namespace Test924Threads |
| 207 | } // namespace art |