Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -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 | |
| 17 | #include <inttypes.h> |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 18 | #include <pthread.h> |
Andreas Gampe | e022015 | 2017-02-07 14:59:08 -0800 | [diff] [blame] | 19 | #include <sched.h> |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 20 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 21 | #include "android-base/logging.h" |
| 22 | #include "android-base/macros.h" |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 23 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 24 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 25 | #include "scoped_local_ref.h" |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 26 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 27 | // Test infrastructure |
| 28 | #include "jvmti_helper.h" |
| 29 | #include "test_env.h" |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace Test930AgentThread { |
| 33 | |
| 34 | struct AgentData { |
| 35 | AgentData() : main_thread(nullptr), |
| 36 | jvmti_env(nullptr), |
Andreas Gampe | d9911ee | 2017-03-27 13:27:24 -0700 | [diff] [blame] | 37 | priority(0) { |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | jthread main_thread; |
| 41 | jvmtiEnv* jvmti_env; |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 42 | pthread_barrier_t b; |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 43 | jint priority; |
| 44 | }; |
| 45 | |
| 46 | static void AgentMain(jvmtiEnv* jenv, JNIEnv* env, void* arg) { |
| 47 | AgentData* data = reinterpret_cast<AgentData*>(arg); |
| 48 | |
| 49 | // Check some basics. |
| 50 | // This thread is not the main thread. |
| 51 | jthread this_thread; |
| 52 | jvmtiError this_thread_result = jenv->GetCurrentThread(&this_thread); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 53 | CheckJvmtiError(jenv, this_thread_result); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 54 | CHECK(!env->IsSameObject(this_thread, data->main_thread)); |
| 55 | |
| 56 | // The thread is a daemon. |
| 57 | jvmtiThreadInfo info; |
| 58 | jvmtiError info_result = jenv->GetThreadInfo(this_thread, &info); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 59 | CheckJvmtiError(jenv, info_result); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 60 | CHECK(info.is_daemon); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 61 | CheckJvmtiError(jenv, jenv->Deallocate(reinterpret_cast<unsigned char*>(info.name))); |
| 62 | if (info.thread_group != nullptr) { |
| 63 | env->DeleteLocalRef(info.thread_group); |
| 64 | } |
| 65 | if (info.context_class_loader != nullptr) { |
| 66 | env->DeleteLocalRef(info.context_class_loader); |
| 67 | } |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 68 | |
| 69 | // The thread has the requested priority. |
| 70 | // TODO: Our thread priorities do not work on the host. |
| 71 | // CHECK_EQ(info.priority, data->priority); |
| 72 | |
| 73 | // Check further parts of the thread: |
| 74 | jint thread_count; |
| 75 | jthread* threads; |
| 76 | jvmtiError threads_result = jenv->GetAllThreads(&thread_count, &threads); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 77 | CheckJvmtiError(jenv, threads_result); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 78 | bool found = false; |
| 79 | for (jint i = 0; i != thread_count; ++i) { |
| 80 | if (env->IsSameObject(threads[i], this_thread)) { |
| 81 | found = true; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | CHECK(found); |
| 86 | |
| 87 | // Done, let the main thread progress. |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 88 | int wait_result = pthread_barrier_wait(&data->b); |
| 89 | CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 92 | extern "C" JNIEXPORT void JNICALL Java_art_Test931_testAgentThread( |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 93 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) { |
| 94 | // Create a Thread object. |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 95 | ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF("Agent Thread")); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 96 | if (thread_name.get() == nullptr) { |
| 97 | return; |
| 98 | } |
| 99 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 100 | ScopedLocalRef<jclass> thread_klass(env, env->FindClass("java/lang/Thread")); |
| 101 | if (thread_klass.get() == nullptr) { |
| 102 | return; |
| 103 | } |
| 104 | ScopedLocalRef<jobject> thread(env, env->AllocObject(thread_klass.get())); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 105 | if (thread.get() == nullptr) { |
| 106 | return; |
| 107 | } |
| 108 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 109 | // Get a ThreadGroup from the current thread. We need a non-null one as we're gonna call a |
| 110 | // runtime-only constructor (so we can set priority and daemon state). |
| 111 | jvmtiThreadInfo cur_thread_info; |
| 112 | jvmtiError info_result = jvmti_env->GetThreadInfo(nullptr, &cur_thread_info); |
| 113 | if (JvmtiErrorToException(env, jvmti_env, info_result)) { |
| 114 | return; |
| 115 | } |
| 116 | CheckJvmtiError(jvmti_env, |
| 117 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(cur_thread_info.name))); |
| 118 | ScopedLocalRef<jobject> thread_group(env, cur_thread_info.thread_group); |
| 119 | if (cur_thread_info.context_class_loader != nullptr) { |
| 120 | env->DeleteLocalRef(cur_thread_info.context_class_loader); |
| 121 | } |
| 122 | |
| 123 | jmethodID initID = env->GetMethodID(thread_klass.get(), |
| 124 | "<init>", |
| 125 | "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V"); |
| 126 | if (initID == nullptr) { |
| 127 | return; |
| 128 | } |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 129 | env->CallNonvirtualVoidMethod(thread.get(), |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 130 | thread_klass.get(), |
| 131 | initID, |
| 132 | thread_group.get(), |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 133 | thread_name.get(), |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 134 | 0, |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 135 | JNI_FALSE); |
| 136 | if (env->ExceptionCheck()) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | jthread main_thread; |
| 141 | jvmtiError main_thread_result = jvmti_env->GetCurrentThread(&main_thread); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 142 | if (JvmtiErrorToException(env, jvmti_env, main_thread_result)) { |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| 146 | AgentData data; |
| 147 | data.main_thread = env->NewGlobalRef(main_thread); |
| 148 | data.jvmti_env = jvmti_env; |
| 149 | data.priority = JVMTI_THREAD_MIN_PRIORITY; |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 150 | CHECK_EQ(0, pthread_barrier_init(&data.b, nullptr, 2)); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 151 | |
| 152 | jvmtiError result = jvmti_env->RunAgentThread(thread.get(), AgentMain, &data, data.priority); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 153 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 157 | int wait_result = pthread_barrier_wait(&data.b); |
| 158 | CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 159 | |
Andreas Gampe | e022015 | 2017-02-07 14:59:08 -0800 | [diff] [blame] | 160 | // Scheduling may mean that the agent thread is put to sleep. Wait until it's dead in an effort |
| 161 | // to not unload the plugin and crash. |
| 162 | for (;;) { |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 163 | sleep(1); |
Andreas Gampe | e022015 | 2017-02-07 14:59:08 -0800 | [diff] [blame] | 164 | jint thread_state; |
| 165 | jvmtiError state_result = jvmti_env->GetThreadState(thread.get(), &thread_state); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 166 | if (JvmtiErrorToException(env, jvmti_env, state_result)) { |
Andreas Gampe | e022015 | 2017-02-07 14:59:08 -0800 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | if (thread_state == 0 || // Was never alive. |
| 170 | (thread_state & JVMTI_THREAD_STATE_TERMINATED) != 0) { // Was alive and died. |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | // Yield and sleep a bit more, to give the plugin time to tear down the native thread structure. |
| 175 | sched_yield(); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 176 | sleep(1); |
Andreas Gampe | e022015 | 2017-02-07 14:59:08 -0800 | [diff] [blame] | 177 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 178 | env->DeleteGlobalRef(data.main_thread); |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 179 | |
| 180 | pthread_barrier_destroy(&data.b); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | } // namespace Test930AgentThread |
| 184 | } // namespace art |