blob: 6ace4cea68862b2d2e92c5a030d244cf01a1555b [file] [log] [blame]
Andreas Gampe732b0ac2017-01-18 15:23:39 -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 <inttypes.h>
18
19#include "barrier.h"
20#include "base/logging.h"
21#include "base/macros.h"
22#include "jni.h"
23#include "openjdkjvmti/jvmti.h"
24#include "runtime.h"
25#include "ScopedLocalRef.h"
26#include "thread-inl.h"
27#include "well_known_classes.h"
28
29#include "ti-agent/common_helper.h"
30#include "ti-agent/common_load.h"
31
32namespace art {
33namespace Test930AgentThread {
34
35struct AgentData {
36 AgentData() : main_thread(nullptr),
37 jvmti_env(nullptr),
38 b(2) {
39 }
40
41 jthread main_thread;
42 jvmtiEnv* jvmti_env;
43 Barrier b;
44 jint priority;
45};
46
47static void AgentMain(jvmtiEnv* jenv, JNIEnv* env, void* arg) {
48 AgentData* data = reinterpret_cast<AgentData*>(arg);
49
50 // Check some basics.
51 // This thread is not the main thread.
52 jthread this_thread;
53 jvmtiError this_thread_result = jenv->GetCurrentThread(&this_thread);
54 CHECK(!JvmtiErrorToException(env, this_thread_result));
55 CHECK(!env->IsSameObject(this_thread, data->main_thread));
56
57 // The thread is a daemon.
58 jvmtiThreadInfo info;
59 jvmtiError info_result = jenv->GetThreadInfo(this_thread, &info);
60 CHECK(!JvmtiErrorToException(env, info_result));
61 CHECK(info.is_daemon);
62
63 // The thread has the requested priority.
64 // TODO: Our thread priorities do not work on the host.
65 // CHECK_EQ(info.priority, data->priority);
66
67 // Check further parts of the thread:
68 jint thread_count;
69 jthread* threads;
70 jvmtiError threads_result = jenv->GetAllThreads(&thread_count, &threads);
71 CHECK(!JvmtiErrorToException(env, threads_result));
72 bool found = false;
73 for (jint i = 0; i != thread_count; ++i) {
74 if (env->IsSameObject(threads[i], this_thread)) {
75 found = true;
76 break;
77 }
78 }
79 CHECK(found);
80
81 // Done, let the main thread progress.
82 data->b.Pass(Thread::Current());
83}
84
85extern "C" JNIEXPORT void JNICALL Java_Main_testAgentThread(
86 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
87 // Create a Thread object.
88 ScopedLocalRef<jobject> thread_name(env,
89 env->NewStringUTF("Agent Thread"));
90 if (thread_name.get() == nullptr) {
91 return;
92 }
93
94 ScopedLocalRef<jobject> thread(env, env->AllocObject(WellKnownClasses::java_lang_Thread));
95 if (thread.get() == nullptr) {
96 return;
97 }
98
99 env->CallNonvirtualVoidMethod(thread.get(),
100 WellKnownClasses::java_lang_Thread,
101 WellKnownClasses::java_lang_Thread_init,
102 Runtime::Current()->GetMainThreadGroup(),
103 thread_name.get(),
104 kMinThreadPriority,
105 JNI_FALSE);
106 if (env->ExceptionCheck()) {
107 return;
108 }
109
110 jthread main_thread;
111 jvmtiError main_thread_result = jvmti_env->GetCurrentThread(&main_thread);
112 if (JvmtiErrorToException(env, main_thread_result)) {
113 return;
114 }
115
116 AgentData data;
117 data.main_thread = env->NewGlobalRef(main_thread);
118 data.jvmti_env = jvmti_env;
119 data.priority = JVMTI_THREAD_MIN_PRIORITY;
120
121 jvmtiError result = jvmti_env->RunAgentThread(thread.get(), AgentMain, &data, data.priority);
122 if (JvmtiErrorToException(env, result)) {
123 return;
124 }
125
126 data.b.Wait(Thread::Current());
127
128 env->DeleteGlobalRef(data.main_thread);
129}
130
131} // namespace Test930AgentThread
132} // namespace art