blob: 31c7582b5894a90d5c3f5540f9c2a9091d72478f [file] [log] [blame]
Andreas Gampe02afcde2017-01-12 17:34:39 -08001/* 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.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_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 "runtime.h"
46#include "scoped_thread_state_change-inl.h"
47#include "thread-inl.h"
48#include "thread_list.h"
49#include "well_known_classes.h"
50
51namespace openjdkjvmti {
52
53
54jvmtiError ThreadGroupUtil::GetTopThreadGroups(jvmtiEnv* env,
55 jint* group_count_ptr,
56 jthreadGroup** groups_ptr) {
57 // We only have a single top group. So we can take the current thread and move upwards.
58 if (group_count_ptr == nullptr || groups_ptr == nullptr) {
59 return ERR(NULL_POINTER);
60 }
61
62 art::Runtime* runtime = art::Runtime::Current();
63 if (runtime == nullptr) {
64 // Must be starting the runtime, or dying.
65 return ERR(WRONG_PHASE);
66 }
67
68 jobject sys_thread_group = runtime->GetSystemThreadGroup();
69 if (sys_thread_group == nullptr) {
70 // Seems we're still starting up.
71 return ERR(WRONG_PHASE);
72 }
73
74 unsigned char* data;
75 jvmtiError result = env->Allocate(sizeof(jthreadGroup), &data);
76 if (result != ERR(NONE)) {
77 return result;
78 }
79
80 jthreadGroup* groups = reinterpret_cast<jthreadGroup*>(data);
81 *groups =
82 reinterpret_cast<JNIEnv*>(art::Thread::Current()->GetJniEnv())->NewLocalRef(sys_thread_group);
83 *groups_ptr = groups;
84 *group_count_ptr = 1;
85
86 return ERR(NONE);
87}
88
89jvmtiError ThreadGroupUtil::GetThreadGroupInfo(jvmtiEnv* env,
90 jthreadGroup group,
91 jvmtiThreadGroupInfo* info_ptr) {
92 if (group == nullptr) {
93 return ERR(INVALID_THREAD_GROUP);
94 }
95
96 art::ScopedObjectAccess soa(art::Thread::Current());
97 if (soa.Env()->IsInstanceOf(group, art::WellKnownClasses::java_lang_ThreadGroup) == JNI_FALSE) {
98 return ERR(INVALID_THREAD_GROUP);
99 }
100
101 art::ObjPtr<art::mirror::Object> obj = soa.Decode<art::mirror::Object>(group);
102
103 // Do the name first. It's the only thing that can fail.
104 {
105 art::ArtField* name_field =
106 art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_name);
107 CHECK(name_field != nullptr);
108 art::ObjPtr<art::mirror::String> name_obj =
109 art::ObjPtr<art::mirror::String>::DownCast(name_field->GetObject(obj));
110 std::string tmp_str;
111 const char* tmp_cstr;
112 if (name_obj == nullptr) {
113 tmp_cstr = "";
114 } else {
115 tmp_str = name_obj->ToModifiedUtf8();
116 tmp_cstr = tmp_str.c_str();
117 }
118 jvmtiError result =
119 CopyString(env, tmp_cstr, reinterpret_cast<unsigned char**>(&info_ptr->name));
120 if (result != ERR(NONE)) {
121 return result;
122 }
123 }
124
125 // Parent.
126 {
127 art::ArtField* parent_field =
128 art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_parent);
129 CHECK(parent_field != nullptr);
130 art::ObjPtr<art::mirror::Object> parent_group = parent_field->GetObject(obj);
131 info_ptr->parent = parent_group == nullptr
132 ? nullptr
133 : soa.AddLocalReference<jthreadGroup>(parent_group);
134 }
135
136 // Max priority.
137 {
138 art::ArtField* prio_field = obj->GetClass()->FindDeclaredInstanceField("maxPriority", "I");
139 CHECK(prio_field != nullptr);
140 info_ptr->max_priority = static_cast<jint>(prio_field->GetInt(obj));
141 }
142
143 // Daemon.
144 {
145 art::ArtField* daemon_field = obj->GetClass()->FindDeclaredInstanceField("daemon", "Z");
146 CHECK(daemon_field != nullptr);
147 info_ptr->is_daemon = daemon_field->GetBoolean(obj) == 0 ? JNI_FALSE : JNI_TRUE;
148 }
149
150 return ERR(NONE);
151}
152
153
154static bool IsInDesiredThreadGroup(art::ObjPtr<art::mirror::Object> desired_thread_group,
155 art::ObjPtr<art::mirror::Object> peer)
156 REQUIRES_SHARED(art::Locks::mutator_lock_) {
157 CHECK(desired_thread_group != nullptr);
158
159 art::ArtField* thread_group_field =
160 art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group);
161 DCHECK(thread_group_field != nullptr);
162 art::ObjPtr<art::mirror::Object> group = thread_group_field->GetObject(peer);
163 return (group == desired_thread_group);
164}
165
166static void GetThreads(art::ObjPtr<art::mirror::Object> thread_group,
167 std::vector<art::ObjPtr<art::mirror::Object>>* thread_peers)
168 REQUIRES_SHARED(art::Locks::mutator_lock_) REQUIRES(!art::Locks::thread_list_lock_) {
169 CHECK(thread_group != nullptr);
170
171 std::list<art::Thread*> all_threads_list;
172 {
173 art::MutexLock mu(art::Thread::Current(), *art::Locks::thread_list_lock_);
174 all_threads_list = art::Runtime::Current()->GetThreadList()->GetList();
175 }
176 for (art::Thread* t : all_threads_list) {
177 if (t->IsStillStarting()) {
178 continue;
179 }
180 art::ObjPtr<art::mirror::Object> peer = t->GetPeer();
181 if (peer == nullptr) {
182 continue;
183 }
184 if (IsInDesiredThreadGroup(thread_group, peer)) {
185 thread_peers->push_back(peer);
186 }
187 }
188}
189
190static void GetChildThreadGroups(art::ObjPtr<art::mirror::Object> thread_group,
191 std::vector<art::ObjPtr<art::mirror::Object>>* thread_groups)
192 REQUIRES_SHARED(art::Locks::mutator_lock_) {
193 CHECK(thread_group != nullptr);
194
195 // Get the ThreadGroup[] "groups" out of this thread group...
196 art::ArtField* groups_field =
197 art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_groups);
198 art::ObjPtr<art::mirror::Object> groups_array = groups_field->GetObject(thread_group);
199
200 if (groups_array == nullptr) {
201 return;
202 }
203 CHECK(groups_array->IsObjectArray());
204
205 art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> groups_array_as_array =
206 groups_array->AsObjectArray<art::mirror::Object>();
207
208 // Copy all non-null elements.
209 for (int32_t i = 0; i < groups_array_as_array->GetLength(); ++i) {
210 art::ObjPtr<art::mirror::Object> entry = groups_array_as_array->Get(i);
211 if (entry != nullptr) {
212 thread_groups->push_back(entry);
213 }
214 }
215}
216
217jvmtiError ThreadGroupUtil::GetThreadGroupChildren(jvmtiEnv* env,
218 jthreadGroup group,
219 jint* thread_count_ptr,
220 jthread** threads_ptr,
221 jint* group_count_ptr,
222 jthreadGroup** groups_ptr) {
223 if (group == nullptr) {
224 return ERR(INVALID_THREAD_GROUP);
225 }
226
227 art::ScopedObjectAccess soa(art::Thread::Current());
228
229 if (!soa.Env()->IsInstanceOf(group, art::WellKnownClasses::java_lang_ThreadGroup)) {
230 return ERR(INVALID_THREAD_GROUP);
231 }
232
233 art::ObjPtr<art::mirror::Object> thread_group = soa.Decode<art::mirror::Object>(group);
234
235 std::vector<art::ObjPtr<art::mirror::Object>> thread_peers;
236 GetThreads(thread_group, &thread_peers);
237
238 std::vector<art::ObjPtr<art::mirror::Object>> thread_groups;
239 GetChildThreadGroups(thread_group, &thread_groups);
240
241 jthread* thread_data = nullptr;
242 JvmtiUniquePtr peers_uptr;
243 if (!thread_peers.empty()) {
244 unsigned char* data;
245 jvmtiError res = env->Allocate(sizeof(jthread) * thread_peers.size(), &data);
246 if (res != ERR(NONE)) {
247 return res;
248 }
249 thread_data = reinterpret_cast<jthread*>(data);
250 peers_uptr = MakeJvmtiUniquePtr(env, data);
251 }
252
253 jthreadGroup* group_data = nullptr;
254 if (!thread_groups.empty()) {
255 unsigned char* data;
256 jvmtiError res = env->Allocate(sizeof(jthreadGroup) * thread_groups.size(), &data);
257 if (res != ERR(NONE)) {
258 return res;
259 }
260 group_data = reinterpret_cast<jthreadGroup*>(data);
261 }
262
263 // Can't fail anymore from here on.
264
265 // Copy data into out buffers.
266 for (size_t i = 0; i != thread_peers.size(); ++i) {
267 thread_data[i] = soa.AddLocalReference<jthread>(thread_peers[i]);
268 }
269 for (size_t i = 0; i != thread_groups.size(); ++i) {
270 group_data[i] = soa.AddLocalReference<jthreadGroup>(thread_groups[i]);
271 }
272
273 *thread_count_ptr = static_cast<jint>(thread_peers.size());
274 *threads_ptr = thread_data;
275 *group_count_ptr = static_cast<jint>(thread_groups.size());
276 *groups_ptr = group_data;
277
278 // Everything's fine.
279 peers_uptr.release();
280
281 return ERR(NONE);
282}
283
284} // namespace openjdkjvmti