Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 34 | #include "art_field-inl.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 40 | #include "jni/jni_internal.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 48 | #include "thread-current-inl.h" |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 49 | #include "thread_list.h" |
| 50 | #include "well_known_classes.h" |
| 51 | |
| 52 | namespace openjdkjvmti { |
| 53 | |
| 54 | |
| 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 | |
| 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 | if (soa.Env()->IsInstanceOf(group, art::WellKnownClasses::java_lang_ThreadGroup) == JNI_FALSE) { |
| 99 | return ERR(INVALID_THREAD_GROUP); |
| 100 | } |
| 101 | |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 102 | art::StackHandleScope<2> hs(soa.Self()); |
| 103 | art::Handle<art::mirror::Class> tg_class( |
| 104 | hs.NewHandle(soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_ThreadGroup))); |
| 105 | art::Handle<art::mirror::Object> obj(hs.NewHandle(soa.Decode<art::mirror::Object>(group))); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 106 | |
| 107 | // Do the name first. It's the only thing that can fail. |
| 108 | { |
| 109 | art::ArtField* name_field = |
| 110 | art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_name); |
| 111 | CHECK(name_field != nullptr); |
| 112 | art::ObjPtr<art::mirror::String> name_obj = |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 113 | art::ObjPtr<art::mirror::String>::DownCast(name_field->GetObject(obj.Get())); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 114 | std::string tmp_str; |
| 115 | const char* tmp_cstr; |
| 116 | if (name_obj == nullptr) { |
| 117 | tmp_cstr = ""; |
| 118 | } else { |
| 119 | tmp_str = name_obj->ToModifiedUtf8(); |
| 120 | tmp_cstr = tmp_str.c_str(); |
| 121 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 122 | jvmtiError result; |
| 123 | JvmtiUniquePtr<char[]> copy = CopyString(env, tmp_cstr, &result); |
| 124 | if (copy == nullptr) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 125 | return result; |
| 126 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 127 | info_ptr->name = copy.release(); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Parent. |
| 131 | { |
| 132 | art::ArtField* parent_field = |
| 133 | art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_parent); |
| 134 | CHECK(parent_field != nullptr); |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 135 | art::ObjPtr<art::mirror::Object> parent_group = parent_field->GetObject(obj.Get()); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 136 | info_ptr->parent = parent_group == nullptr |
| 137 | ? nullptr |
| 138 | : soa.AddLocalReference<jthreadGroup>(parent_group); |
| 139 | } |
| 140 | |
| 141 | // Max priority. |
| 142 | { |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 143 | art::ArtField* prio_field = tg_class->FindDeclaredInstanceField("maxPriority", "I"); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 144 | CHECK(prio_field != nullptr); |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 145 | info_ptr->max_priority = static_cast<jint>(prio_field->GetInt(obj.Get())); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Daemon. |
| 149 | { |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 150 | art::ArtField* daemon_field = tg_class->FindDeclaredInstanceField("daemon", "Z"); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 151 | CHECK(daemon_field != nullptr); |
Alex Light | 8ea9b37 | 2019-09-04 15:52:41 -0700 | [diff] [blame] | 152 | info_ptr->is_daemon = daemon_field->GetBoolean(obj.Get()) == 0 ? JNI_FALSE : JNI_TRUE; |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return ERR(NONE); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static bool IsInDesiredThreadGroup(art::Handle<art::mirror::Object> desired_thread_group, |
| 160 | art::ObjPtr<art::mirror::Object> peer) |
| 161 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 162 | CHECK(desired_thread_group != nullptr); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 163 | |
| 164 | art::ArtField* thread_group_field = |
| 165 | art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 166 | DCHECK(thread_group_field != nullptr); |
| 167 | art::ObjPtr<art::mirror::Object> group = thread_group_field->GetObject(peer); |
| 168 | return (group == desired_thread_group.Get()); |
| 169 | } |
| 170 | |
| 171 | static void GetThreads(art::Handle<art::mirror::Object> thread_group, |
| 172 | std::vector<art::ObjPtr<art::mirror::Object>>* thread_peers) |
| 173 | REQUIRES_SHARED(art::Locks::mutator_lock_) REQUIRES(!art::Locks::thread_list_lock_) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 174 | CHECK(thread_group != nullptr); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 175 | |
| 176 | art::MutexLock mu(art::Thread::Current(), *art::Locks::thread_list_lock_); |
| 177 | for (art::Thread* t : art::Runtime::Current()->GetThreadList()->GetList()) { |
| 178 | if (t->IsStillStarting()) { |
| 179 | continue; |
| 180 | } |
Andreas Gampe | 202f85a | 2017-02-06 10:23:26 -0800 | [diff] [blame] | 181 | art::ObjPtr<art::mirror::Object> peer = t->GetPeerFromOtherThread(); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 182 | if (peer == nullptr) { |
| 183 | continue; |
| 184 | } |
| 185 | if (IsInDesiredThreadGroup(thread_group, peer)) { |
| 186 | thread_peers->push_back(peer); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void GetChildThreadGroups(art::Handle<art::mirror::Object> thread_group, |
| 192 | std::vector<art::ObjPtr<art::mirror::Object>>* thread_groups) |
| 193 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 194 | CHECK(thread_group != nullptr); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 195 | |
| 196 | // Get the ThreadGroup[] "groups" out of this thread group... |
| 197 | art::ArtField* groups_field = |
| 198 | art::jni::DecodeArtField(art::WellKnownClasses::java_lang_ThreadGroup_groups); |
| 199 | art::ObjPtr<art::mirror::Object> groups_array = groups_field->GetObject(thread_group.Get()); |
| 200 | |
| 201 | if (groups_array == nullptr) { |
| 202 | return; |
| 203 | } |
| 204 | CHECK(groups_array->IsObjectArray()); |
| 205 | |
| 206 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> groups_array_as_array = |
| 207 | groups_array->AsObjectArray<art::mirror::Object>(); |
| 208 | |
| 209 | // Copy all non-null elements. |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 210 | for (auto entry : groups_array_as_array->Iterate()) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 211 | if (entry != nullptr) { |
| 212 | thread_groups->push_back(entry); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | jvmtiError 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::StackHandleScope<1> hs(soa.Self()); |
| 234 | art::Handle<art::mirror::Object> thread_group = hs.NewHandle( |
| 235 | soa.Decode<art::mirror::Object>(group)); |
| 236 | |
| 237 | art::ObjectLock<art::mirror::Object> thread_group_lock(soa.Self(), thread_group); |
| 238 | |
| 239 | std::vector<art::ObjPtr<art::mirror::Object>> thread_peers; |
| 240 | GetThreads(thread_group, &thread_peers); |
| 241 | |
| 242 | std::vector<art::ObjPtr<art::mirror::Object>> thread_groups; |
| 243 | GetChildThreadGroups(thread_group, &thread_groups); |
| 244 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 245 | JvmtiUniquePtr<jthread[]> peers_uptr; |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 246 | if (!thread_peers.empty()) { |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 247 | jvmtiError res; |
| 248 | peers_uptr = AllocJvmtiUniquePtr<jthread[]>(env, thread_peers.size(), &res); |
| 249 | if (peers_uptr == nullptr) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 250 | return res; |
| 251 | } |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 254 | JvmtiUniquePtr<jthreadGroup[]> group_uptr; |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 255 | if (!thread_groups.empty()) { |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 256 | jvmtiError res; |
| 257 | group_uptr = AllocJvmtiUniquePtr<jthreadGroup[]>(env, thread_groups.size(), &res); |
| 258 | if (group_uptr == nullptr) { |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 259 | return res; |
| 260 | } |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 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) { |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 267 | peers_uptr[i] = soa.AddLocalReference<jthread>(thread_peers[i]); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 268 | } |
| 269 | for (size_t i = 0; i != thread_groups.size(); ++i) { |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 270 | group_uptr[i] = soa.AddLocalReference<jthreadGroup>(thread_groups[i]); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | *thread_count_ptr = static_cast<jint>(thread_peers.size()); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 274 | *threads_ptr = peers_uptr.release(); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 275 | *group_count_ptr = static_cast<jint>(thread_groups.size()); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 276 | *groups_ptr = group_uptr.release(); |
Andreas Gampe | d18d9e2 | 2017-01-16 16:08:45 +0000 | [diff] [blame] | 277 | |
| 278 | return ERR(NONE); |
| 279 | } |
| 280 | |
| 281 | } // namespace openjdkjvmti |