1 /* Copyright (C) 2017 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32 #include "ti_threadgroup.h"
33
34 #include "art_field-inl.h"
35 #include "art_jvmti.h"
36 #include "base/logging.h"
37 #include "base/macros.h"
38 #include "base/mutex.h"
39 #include "handle_scope-inl.h"
40 #include "jni/jni_internal.h"
41 #include "mirror/class.h"
42 #include "mirror/object-inl.h"
43 #include "mirror/string.h"
44 #include "obj_ptr.h"
45 #include "object_lock.h"
46 #include "runtime.h"
47 #include "scoped_thread_state_change-inl.h"
48 #include "thread-current-inl.h"
49 #include "thread_list.h"
50 #include "well_known_classes-inl.h"
51
52 namespace openjdkjvmti {
53
54
GetTopThreadGroups(jvmtiEnv * env,jint * group_count_ptr,jthreadGroup ** groups_ptr)55 jvmtiError ThreadGroupUtil::GetTopThreadGroups(jvmtiEnv* env,
56 jint* group_count_ptr,
57 jthreadGroup** groups_ptr) {
58 // We only have a single top group. So we can take the current thread and move upwards.
59 if (group_count_ptr == nullptr || groups_ptr == nullptr) {
60 return ERR(NULL_POINTER);
61 }
62
63 art::Runtime* runtime = art::Runtime::Current();
64 if (runtime == nullptr) {
65 // Must be starting the runtime, or dying.
66 return ERR(WRONG_PHASE);
67 }
68
69 jobject sys_thread_group = runtime->GetSystemThreadGroup();
70 if (sys_thread_group == nullptr) {
71 // Seems we're still starting up.
72 return ERR(WRONG_PHASE);
73 }
74
75 unsigned char* data;
76 jvmtiError result = env->Allocate(sizeof(jthreadGroup), &data);
77 if (result != ERR(NONE)) {
78 return result;
79 }
80
81 jthreadGroup* groups = reinterpret_cast<jthreadGroup*>(data);
82 *groups =
83 reinterpret_cast<JNIEnv*>(art::Thread::Current()->GetJniEnv())->NewLocalRef(sys_thread_group);
84 *groups_ptr = groups;
85 *group_count_ptr = 1;
86
87 return ERR(NONE);
88 }
89
GetThreadGroupInfo(jvmtiEnv * env,jthreadGroup group,jvmtiThreadGroupInfo * info_ptr)90 jvmtiError ThreadGroupUtil::GetThreadGroupInfo(jvmtiEnv* env,
91 jthreadGroup group,
92 jvmtiThreadGroupInfo* info_ptr) {
93 if (group == nullptr) {
94 return ERR(INVALID_THREAD_GROUP);
95 }
96
97 art::ScopedObjectAccess soa(art::Thread::Current());
98 art::StackHandleScope<2> hs(soa.Self());
99 art::Handle<art::mirror::Class> tg_class =
100 hs.NewHandle(art::WellKnownClasses::java_lang_ThreadGroup.Get());
101 art::Handle<art::mirror::Object> thread_group =
102 hs.NewHandle(soa.Decode<art::mirror::Object>(group));
103 if (!thread_group->InstanceOf(tg_class.Get())) {
104 return ERR(INVALID_THREAD_GROUP);
105 }
106
107 // Do the name first. It's the only thing that can fail.
108 {
109 art::ArtField* name_field = art::WellKnownClasses::java_lang_ThreadGroup_name;
110 CHECK(name_field != nullptr);
111 art::ObjPtr<art::mirror::String> name_obj =
112 art::ObjPtr<art::mirror::String>::DownCast(name_field->GetObject(thread_group.Get()));
113 std::string tmp_str;
114 const char* tmp_cstr;
115 if (name_obj == nullptr) {
116 tmp_cstr = "";
117 } else {
118 tmp_str = name_obj->ToModifiedUtf8();
119 tmp_cstr = tmp_str.c_str();
120 }
121 jvmtiError result;
122 JvmtiUniquePtr<char[]> copy = CopyString(env, tmp_cstr, &result);
123 if (copy == nullptr) {
124 return result;
125 }
126 info_ptr->name = copy.release();
127 }
128
129 // Parent.
130 {
131 art::ArtField* parent_field = art::WellKnownClasses::java_lang_ThreadGroup_parent;
132 CHECK(parent_field != nullptr);
133 art::ObjPtr<art::mirror::Object> parent_group = parent_field->GetObject(thread_group.Get());
134 info_ptr->parent = parent_group == nullptr
135 ? nullptr
136 : soa.AddLocalReference<jthreadGroup>(parent_group);
137 }
138
139 // Max priority.
140 {
141 art::ArtField* prio_field = tg_class->FindDeclaredInstanceField("maxPriority", "I");
142 CHECK(prio_field != nullptr);
143 info_ptr->max_priority = static_cast<jint>(prio_field->GetInt(thread_group.Get()));
144 }
145
146 // Daemon.
147 {
148 art::ArtField* daemon_field = tg_class->FindDeclaredInstanceField("daemon", "Z");
149 CHECK(daemon_field != nullptr);
150 info_ptr->is_daemon = daemon_field->GetBoolean(thread_group.Get()) == 0 ? JNI_FALSE : JNI_TRUE;
151 }
152
153 return ERR(NONE);
154 }
155
156
IsInDesiredThreadGroup(art::Handle<art::mirror::Object> desired_thread_group,art::ObjPtr<art::mirror::Object> peer)157 static bool IsInDesiredThreadGroup(art::Handle<art::mirror::Object> desired_thread_group,
158 art::ObjPtr<art::mirror::Object> peer)
159 REQUIRES_SHARED(art::Locks::mutator_lock_) {
160 CHECK(desired_thread_group != nullptr);
161
162 art::ArtField* thread_group_field = art::WellKnownClasses::java_lang_Thread_group;
163 DCHECK(thread_group_field != nullptr);
164 art::ObjPtr<art::mirror::Object> group = thread_group_field->GetObject(peer);
165 return (group == desired_thread_group.Get());
166 }
167
GetThreads(art::Handle<art::mirror::Object> thread_group,std::vector<art::ObjPtr<art::mirror::Object>> * thread_peers)168 static void GetThreads(art::Handle<art::mirror::Object> thread_group,
169 std::vector<art::ObjPtr<art::mirror::Object>>* thread_peers)
170 REQUIRES_SHARED(art::Locks::mutator_lock_) REQUIRES(!art::Locks::thread_list_lock_) {
171 CHECK(thread_group != nullptr);
172
173 art::MutexLock mu(art::Thread::Current(), *art::Locks::thread_list_lock_);
174 std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList();
175 // We have to be careful with threads exiting while we build this list.
176 std::vector<art::ThreadExitFlag> tefs(thread_list.size());
177 auto i = tefs.begin();
178 for (art::Thread* thd : thread_list) {
179 thd->NotifyOnThreadExit(&*i++);
180 }
181 DCHECK(i == tefs.end());
182
183 i = tefs.begin();
184 for (art::Thread* t : thread_list) {
185 art::ThreadExitFlag* tef = &*i++;
186 art::ObjPtr<art::mirror::Object> peer = t->LockedGetPeerFromOtherThread(tef);
187 if (peer != nullptr && !tef->HasExited() && !t->IsStillStarting() &&
188 IsInDesiredThreadGroup(thread_group, peer)) {
189 thread_peers->push_back(peer);
190 }
191 t->UnregisterThreadExitFlag(tef);
192 }
193 DCHECK(i == tefs.end());
194 }
195
GetChildThreadGroups(art::Handle<art::mirror::Object> thread_group,std::vector<art::ObjPtr<art::mirror::Object>> * thread_groups)196 static void GetChildThreadGroups(art::Handle<art::mirror::Object> thread_group,
197 std::vector<art::ObjPtr<art::mirror::Object>>* thread_groups)
198 REQUIRES_SHARED(art::Locks::mutator_lock_) {
199 CHECK(thread_group != nullptr);
200
201 // Get the ThreadGroup[] "groups" out of this thread group...
202 art::ArtField* groups_field = art::WellKnownClasses::java_lang_ThreadGroup_groups;
203 art::ObjPtr<art::mirror::Object> groups_array = groups_field->GetObject(thread_group.Get());
204
205 if (groups_array == nullptr) {
206 return;
207 }
208 CHECK(groups_array->IsObjectArray());
209
210 art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> groups_array_as_array =
211 groups_array->AsObjectArray<art::mirror::Object>();
212
213 // Copy all non-null elements.
214 for (auto entry : groups_array_as_array->Iterate()) {
215 if (entry != nullptr) {
216 thread_groups->push_back(entry);
217 }
218 }
219 }
220
GetThreadGroupChildren(jvmtiEnv * env,jthreadGroup group,jint * thread_count_ptr,jthread ** threads_ptr,jint * group_count_ptr,jthreadGroup ** groups_ptr)221 jvmtiError ThreadGroupUtil::GetThreadGroupChildren(jvmtiEnv* env,
222 jthreadGroup group,
223 jint* thread_count_ptr,
224 jthread** threads_ptr,
225 jint* group_count_ptr,
226 jthreadGroup** groups_ptr) {
227 if (group == nullptr) {
228 return ERR(INVALID_THREAD_GROUP);
229 }
230
231 art::ScopedObjectAccess soa(art::Thread::Current());
232 art::StackHandleScope<1> hs(soa.Self());
233 art::Handle<art::mirror::Object> thread_group =
234 hs.NewHandle(soa.Decode<art::mirror::Object>(group));
235 if (!thread_group->InstanceOf(art::WellKnownClasses::java_lang_ThreadGroup.Get())) {
236 return ERR(INVALID_THREAD_GROUP);
237 }
238
239
240 art::ObjectLock<art::mirror::Object> thread_group_lock(soa.Self(), thread_group);
241
242 std::vector<art::ObjPtr<art::mirror::Object>> thread_peers;
243 GetThreads(thread_group, &thread_peers);
244
245 std::vector<art::ObjPtr<art::mirror::Object>> thread_groups;
246 GetChildThreadGroups(thread_group, &thread_groups);
247
248 JvmtiUniquePtr<jthread[]> peers_uptr;
249 if (!thread_peers.empty()) {
250 jvmtiError res;
251 peers_uptr = AllocJvmtiUniquePtr<jthread[]>(env, thread_peers.size(), &res);
252 if (peers_uptr == nullptr) {
253 return res;
254 }
255 }
256
257 JvmtiUniquePtr<jthreadGroup[]> group_uptr;
258 if (!thread_groups.empty()) {
259 jvmtiError res;
260 group_uptr = AllocJvmtiUniquePtr<jthreadGroup[]>(env, thread_groups.size(), &res);
261 if (group_uptr == nullptr) {
262 return res;
263 }
264 }
265
266 // Can't fail anymore from here on.
267
268 // Copy data into out buffers.
269 for (size_t i = 0; i != thread_peers.size(); ++i) {
270 peers_uptr[i] = soa.AddLocalReference<jthread>(thread_peers[i]);
271 }
272 for (size_t i = 0; i != thread_groups.size(); ++i) {
273 group_uptr[i] = soa.AddLocalReference<jthreadGroup>(thread_groups[i]);
274 }
275
276 *thread_count_ptr = static_cast<jint>(thread_peers.size());
277 *threads_ptr = peers_uptr.release();
278 *group_count_ptr = static_cast<jint>(thread_groups.size());
279 *groups_ptr = group_uptr.release();
280
281 return ERR(NONE);
282 }
283
284 } // namespace openjdkjvmti
285