1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include <inttypes.h>
18*795d594fSAndroid Build Coastguard Worker #include <pthread.h>
19*795d594fSAndroid Build Coastguard Worker #include <sched.h>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
22*795d594fSAndroid Build Coastguard Worker #include "android-base/macros.h"
23*795d594fSAndroid Build Coastguard Worker #include "jni.h"
24*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
25*795d594fSAndroid Build Coastguard Worker #include "scoped_local_ref.h"
26*795d594fSAndroid Build Coastguard Worker
27*795d594fSAndroid Build Coastguard Worker // Test infrastructure
28*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
29*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker namespace art {
32*795d594fSAndroid Build Coastguard Worker namespace Test930AgentThread {
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker struct AgentData {
AgentDataart::Test930AgentThread::AgentData35*795d594fSAndroid Build Coastguard Worker AgentData() : main_thread(nullptr),
36*795d594fSAndroid Build Coastguard Worker jvmti_env(nullptr),
37*795d594fSAndroid Build Coastguard Worker priority(0) {
38*795d594fSAndroid Build Coastguard Worker }
39*795d594fSAndroid Build Coastguard Worker
40*795d594fSAndroid Build Coastguard Worker jthread main_thread;
41*795d594fSAndroid Build Coastguard Worker jvmtiEnv* jvmti_env;
42*795d594fSAndroid Build Coastguard Worker pthread_barrier_t b;
43*795d594fSAndroid Build Coastguard Worker jint priority;
44*795d594fSAndroid Build Coastguard Worker };
45*795d594fSAndroid Build Coastguard Worker
AgentMain(jvmtiEnv * jenv,JNIEnv * env,void * arg)46*795d594fSAndroid Build Coastguard Worker static void AgentMain(jvmtiEnv* jenv, JNIEnv* env, void* arg) {
47*795d594fSAndroid Build Coastguard Worker AgentData* data = reinterpret_cast<AgentData*>(arg);
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker // Check some basics.
50*795d594fSAndroid Build Coastguard Worker // This thread is not the main thread.
51*795d594fSAndroid Build Coastguard Worker jthread this_thread;
52*795d594fSAndroid Build Coastguard Worker jvmtiError this_thread_result = jenv->GetCurrentThread(&this_thread);
53*795d594fSAndroid Build Coastguard Worker CheckJvmtiError(jenv, this_thread_result);
54*795d594fSAndroid Build Coastguard Worker CHECK(!env->IsSameObject(this_thread, data->main_thread));
55*795d594fSAndroid Build Coastguard Worker
56*795d594fSAndroid Build Coastguard Worker // The thread is a daemon.
57*795d594fSAndroid Build Coastguard Worker jvmtiThreadInfo info;
58*795d594fSAndroid Build Coastguard Worker jvmtiError info_result = jenv->GetThreadInfo(this_thread, &info);
59*795d594fSAndroid Build Coastguard Worker CheckJvmtiError(jenv, info_result);
60*795d594fSAndroid Build Coastguard Worker CHECK(info.is_daemon);
61*795d594fSAndroid Build Coastguard Worker CheckJvmtiError(jenv, jenv->Deallocate(reinterpret_cast<unsigned char*>(info.name)));
62*795d594fSAndroid Build Coastguard Worker if (info.thread_group != nullptr) {
63*795d594fSAndroid Build Coastguard Worker env->DeleteLocalRef(info.thread_group);
64*795d594fSAndroid Build Coastguard Worker }
65*795d594fSAndroid Build Coastguard Worker if (info.context_class_loader != nullptr) {
66*795d594fSAndroid Build Coastguard Worker env->DeleteLocalRef(info.context_class_loader);
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker // The thread has the requested priority.
70*795d594fSAndroid Build Coastguard Worker // TODO: Our thread priorities do not work on the host.
71*795d594fSAndroid Build Coastguard Worker // CHECK_EQ(info.priority, data->priority);
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard Worker // Check further parts of the thread:
74*795d594fSAndroid Build Coastguard Worker jint thread_count;
75*795d594fSAndroid Build Coastguard Worker jthread* threads;
76*795d594fSAndroid Build Coastguard Worker jvmtiError threads_result = jenv->GetAllThreads(&thread_count, &threads);
77*795d594fSAndroid Build Coastguard Worker CheckJvmtiError(jenv, threads_result);
78*795d594fSAndroid Build Coastguard Worker bool found = false;
79*795d594fSAndroid Build Coastguard Worker for (jint i = 0; i != thread_count; ++i) {
80*795d594fSAndroid Build Coastguard Worker if (env->IsSameObject(threads[i], this_thread)) {
81*795d594fSAndroid Build Coastguard Worker found = true;
82*795d594fSAndroid Build Coastguard Worker break;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker }
85*795d594fSAndroid Build Coastguard Worker CHECK(found);
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Worker // Done, let the main thread progress.
88*795d594fSAndroid Build Coastguard Worker int wait_result = pthread_barrier_wait(&data->b);
89*795d594fSAndroid Build Coastguard Worker CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0);
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
Java_art_Test931_testAgentThread(JNIEnv * env,jclass Main_klass)92*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT void JNICALL Java_art_Test931_testAgentThread(
93*795d594fSAndroid Build Coastguard Worker JNIEnv* env, [[maybe_unused]] jclass Main_klass) {
94*795d594fSAndroid Build Coastguard Worker // Create a Thread object.
95*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jobject> thread_name(env, env->NewStringUTF("Agent Thread"));
96*795d594fSAndroid Build Coastguard Worker if (thread_name.get() == nullptr) {
97*795d594fSAndroid Build Coastguard Worker return;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jclass> thread_klass(env, env->FindClass("java/lang/Thread"));
101*795d594fSAndroid Build Coastguard Worker if (thread_klass.get() == nullptr) {
102*795d594fSAndroid Build Coastguard Worker return;
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jobject> thread(env, env->AllocObject(thread_klass.get()));
105*795d594fSAndroid Build Coastguard Worker if (thread.get() == nullptr) {
106*795d594fSAndroid Build Coastguard Worker return;
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker
109*795d594fSAndroid Build Coastguard Worker // Get a ThreadGroup from the current thread. We need a non-null one as we're gonna call a
110*795d594fSAndroid Build Coastguard Worker // runtime-only constructor (so we can set priority and daemon state).
111*795d594fSAndroid Build Coastguard Worker jvmtiThreadInfo cur_thread_info;
112*795d594fSAndroid Build Coastguard Worker jvmtiError info_result = jvmti_env->GetThreadInfo(nullptr, &cur_thread_info);
113*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, info_result)) {
114*795d594fSAndroid Build Coastguard Worker return;
115*795d594fSAndroid Build Coastguard Worker }
116*795d594fSAndroid Build Coastguard Worker CheckJvmtiError(jvmti_env,
117*795d594fSAndroid Build Coastguard Worker jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(cur_thread_info.name)));
118*795d594fSAndroid Build Coastguard Worker ScopedLocalRef<jobject> thread_group(env, cur_thread_info.thread_group);
119*795d594fSAndroid Build Coastguard Worker if (cur_thread_info.context_class_loader != nullptr) {
120*795d594fSAndroid Build Coastguard Worker env->DeleteLocalRef(cur_thread_info.context_class_loader);
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker jmethodID initID = env->GetMethodID(
124*795d594fSAndroid Build Coastguard Worker thread_klass.get(), "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V");
125*795d594fSAndroid Build Coastguard Worker if (initID == nullptr) {
126*795d594fSAndroid Build Coastguard Worker return;
127*795d594fSAndroid Build Coastguard Worker }
128*795d594fSAndroid Build Coastguard Worker env->CallNonvirtualVoidMethod(
129*795d594fSAndroid Build Coastguard Worker thread.get(), thread_klass.get(), initID, thread_group.get(), thread_name.get());
130*795d594fSAndroid Build Coastguard Worker if (env->ExceptionCheck()) {
131*795d594fSAndroid Build Coastguard Worker return;
132*795d594fSAndroid Build Coastguard Worker }
133*795d594fSAndroid Build Coastguard Worker
134*795d594fSAndroid Build Coastguard Worker jthread main_thread;
135*795d594fSAndroid Build Coastguard Worker jvmtiError main_thread_result = jvmti_env->GetCurrentThread(&main_thread);
136*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, main_thread_result)) {
137*795d594fSAndroid Build Coastguard Worker return;
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker
140*795d594fSAndroid Build Coastguard Worker AgentData data;
141*795d594fSAndroid Build Coastguard Worker data.main_thread = env->NewGlobalRef(main_thread);
142*795d594fSAndroid Build Coastguard Worker data.jvmti_env = jvmti_env;
143*795d594fSAndroid Build Coastguard Worker data.priority = JVMTI_THREAD_MIN_PRIORITY;
144*795d594fSAndroid Build Coastguard Worker CHECK_EQ(0, pthread_barrier_init(&data.b, nullptr, 2));
145*795d594fSAndroid Build Coastguard Worker
146*795d594fSAndroid Build Coastguard Worker jvmtiError result = jvmti_env->RunAgentThread(thread.get(), AgentMain, &data, data.priority);
147*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, result)) {
148*795d594fSAndroid Build Coastguard Worker return;
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker
151*795d594fSAndroid Build Coastguard Worker int wait_result = pthread_barrier_wait(&data.b);
152*795d594fSAndroid Build Coastguard Worker CHECK(wait_result == PTHREAD_BARRIER_SERIAL_THREAD || wait_result == 0);
153*795d594fSAndroid Build Coastguard Worker
154*795d594fSAndroid Build Coastguard Worker // Scheduling may mean that the agent thread is put to sleep. Wait until it's dead in an effort
155*795d594fSAndroid Build Coastguard Worker // to not unload the plugin and crash.
156*795d594fSAndroid Build Coastguard Worker for (;;) {
157*795d594fSAndroid Build Coastguard Worker sleep(1);
158*795d594fSAndroid Build Coastguard Worker jint thread_state;
159*795d594fSAndroid Build Coastguard Worker jvmtiError state_result = jvmti_env->GetThreadState(thread.get(), &thread_state);
160*795d594fSAndroid Build Coastguard Worker if (JvmtiErrorToException(env, jvmti_env, state_result)) {
161*795d594fSAndroid Build Coastguard Worker return;
162*795d594fSAndroid Build Coastguard Worker }
163*795d594fSAndroid Build Coastguard Worker if (thread_state == 0 || // Was never alive.
164*795d594fSAndroid Build Coastguard Worker (thread_state & JVMTI_THREAD_STATE_TERMINATED) != 0) { // Was alive and died.
165*795d594fSAndroid Build Coastguard Worker break;
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker }
168*795d594fSAndroid Build Coastguard Worker // Yield and sleep a bit more, to give the plugin time to tear down the native thread structure.
169*795d594fSAndroid Build Coastguard Worker sched_yield();
170*795d594fSAndroid Build Coastguard Worker sleep(1);
171*795d594fSAndroid Build Coastguard Worker
172*795d594fSAndroid Build Coastguard Worker env->DeleteGlobalRef(data.main_thread);
173*795d594fSAndroid Build Coastguard Worker
174*795d594fSAndroid Build Coastguard Worker pthread_barrier_destroy(&data.b);
175*795d594fSAndroid Build Coastguard Worker }
176*795d594fSAndroid Build Coastguard Worker
177*795d594fSAndroid Build Coastguard Worker } // namespace Test930AgentThread
178*795d594fSAndroid Build Coastguard Worker } // namespace art
179