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