xref: /aosp_15_r20/art/openjdkjvmti/ti_object.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2017 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #include "ti_object.h"
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker #include "art_jvmti.h"
35*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
38*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
39*795d594fSAndroid Build Coastguard Worker #include "ti_thread.h"
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
42*795d594fSAndroid Build Coastguard Worker 
GetObjectSize(jvmtiEnv * env,jobject jobject,jlong * size_ptr)43*795d594fSAndroid Build Coastguard Worker jvmtiError ObjectUtil::GetObjectSize([[maybe_unused]] jvmtiEnv* env,
44*795d594fSAndroid Build Coastguard Worker                                      jobject jobject,
45*795d594fSAndroid Build Coastguard Worker                                      jlong* size_ptr) {
46*795d594fSAndroid Build Coastguard Worker   if (jobject == nullptr) {
47*795d594fSAndroid Build Coastguard Worker     return ERR(INVALID_OBJECT);
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker   if (size_ptr == nullptr) {
50*795d594fSAndroid Build Coastguard Worker     return ERR(NULL_POINTER);
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker   art::ScopedObjectAccess soa(art::Thread::Current());
54*795d594fSAndroid Build Coastguard Worker   art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobject);
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker   *size_ptr = object->SizeOf();
57*795d594fSAndroid Build Coastguard Worker   return ERR(NONE);
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker 
GetObjectHashCode(jvmtiEnv * env,jobject jobject,jint * hash_code_ptr)60*795d594fSAndroid Build Coastguard Worker jvmtiError ObjectUtil::GetObjectHashCode([[maybe_unused]] jvmtiEnv* env,
61*795d594fSAndroid Build Coastguard Worker                                          jobject jobject,
62*795d594fSAndroid Build Coastguard Worker                                          jint* hash_code_ptr) {
63*795d594fSAndroid Build Coastguard Worker   if (jobject == nullptr) {
64*795d594fSAndroid Build Coastguard Worker     return ERR(INVALID_OBJECT);
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker   if (hash_code_ptr == nullptr) {
67*795d594fSAndroid Build Coastguard Worker     return ERR(NULL_POINTER);
68*795d594fSAndroid Build Coastguard Worker   }
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker   art::ScopedObjectAccess soa(art::Thread::Current());
71*795d594fSAndroid Build Coastguard Worker   art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobject);
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   *hash_code_ptr = object->IdentityHashCode();
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   return ERR(NONE);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
GetObjectMonitorUsage(jvmtiEnv * baseenv,jobject obj,jvmtiMonitorUsage * usage)78*795d594fSAndroid Build Coastguard Worker jvmtiError ObjectUtil::GetObjectMonitorUsage(
79*795d594fSAndroid Build Coastguard Worker     jvmtiEnv* baseenv, jobject obj, jvmtiMonitorUsage* usage) {
80*795d594fSAndroid Build Coastguard Worker   ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(baseenv);
81*795d594fSAndroid Build Coastguard Worker   if (obj == nullptr) {
82*795d594fSAndroid Build Coastguard Worker     return ERR(INVALID_OBJECT);
83*795d594fSAndroid Build Coastguard Worker   }
84*795d594fSAndroid Build Coastguard Worker   if (usage == nullptr) {
85*795d594fSAndroid Build Coastguard Worker     return ERR(NULL_POINTER);
86*795d594fSAndroid Build Coastguard Worker   }
87*795d594fSAndroid Build Coastguard Worker   art::Thread* self = art::Thread::Current();
88*795d594fSAndroid Build Coastguard Worker   ThreadUtil::SuspendCheck(self);
89*795d594fSAndroid Build Coastguard Worker   art::JNIEnvExt* jni = self->GetJniEnv();
90*795d594fSAndroid Build Coastguard Worker   std::vector<jthread> wait;
91*795d594fSAndroid Build Coastguard Worker   std::vector<jthread> notify_wait;
92*795d594fSAndroid Build Coastguard Worker   {
93*795d594fSAndroid Build Coastguard Worker     art::ScopedObjectAccess soa(self);      // Now we know we have the shared lock.
94*795d594fSAndroid Build Coastguard Worker     art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
95*795d594fSAndroid Build Coastguard Worker     art::ScopedSuspendAll ssa("GetObjectMonitorUsage", /*long_suspend=*/false);
96*795d594fSAndroid Build Coastguard Worker     art::ObjPtr<art::mirror::Object> target(self->DecodeJObject(obj));
97*795d594fSAndroid Build Coastguard Worker     // This gets the list of threads trying to lock or wait on the monitor.
98*795d594fSAndroid Build Coastguard Worker     art::MonitorInfo info(target.Ptr());
99*795d594fSAndroid Build Coastguard Worker     usage->owner = info.owner_ != nullptr ?
100*795d594fSAndroid Build Coastguard Worker         jni->AddLocalReference<jthread>(info.owner_->GetPeerFromOtherThread()) : nullptr;
101*795d594fSAndroid Build Coastguard Worker     usage->entry_count = info.entry_count_;
102*795d594fSAndroid Build Coastguard Worker     for (art::Thread* thd : info.waiters_) {
103*795d594fSAndroid Build Coastguard Worker       // RI seems to consider waiting for notify to be included in those waiting to acquire the
104*795d594fSAndroid Build Coastguard Worker       // monitor. We will match this behavior.
105*795d594fSAndroid Build Coastguard Worker       notify_wait.push_back(jni->AddLocalReference<jthread>(thd->GetPeerFromOtherThread()));
106*795d594fSAndroid Build Coastguard Worker       wait.push_back(jni->AddLocalReference<jthread>(thd->GetPeerFromOtherThread()));
107*795d594fSAndroid Build Coastguard Worker     }
108*795d594fSAndroid Build Coastguard Worker     // Scan all threads to see which are waiting on this particular monitor.
109*795d594fSAndroid Build Coastguard Worker     std::list<art::Thread*> thread_list;
110*795d594fSAndroid Build Coastguard Worker     {
111*795d594fSAndroid Build Coastguard Worker       // Since we're in a SuspendAll, exiting threads are not a concern.
112*795d594fSAndroid Build Coastguard Worker       art::MutexLock tll(self, *art::Locks::thread_list_lock_);
113*795d594fSAndroid Build Coastguard Worker       thread_list = art::Runtime::Current()->GetThreadList()->GetList();
114*795d594fSAndroid Build Coastguard Worker     }
115*795d594fSAndroid Build Coastguard Worker     for (art::Thread* thd : thread_list) {
116*795d594fSAndroid Build Coastguard Worker       if (thd != info.owner_ && target.Ptr() == thd->GetMonitorEnterObject()) {
117*795d594fSAndroid Build Coastguard Worker         art::mirror::Object* peer = thd->GetPeerFromOtherThread();
118*795d594fSAndroid Build Coastguard Worker         wait.push_back(jni->AddLocalReference<jthread>(peer));
119*795d594fSAndroid Build Coastguard Worker       }
120*795d594fSAndroid Build Coastguard Worker     }
121*795d594fSAndroid Build Coastguard Worker   }
122*795d594fSAndroid Build Coastguard Worker   usage->waiter_count = wait.size();
123*795d594fSAndroid Build Coastguard Worker   usage->notify_waiter_count = notify_wait.size();
124*795d594fSAndroid Build Coastguard Worker   jvmtiError ret = CopyDataIntoJvmtiBuffer(env,
125*795d594fSAndroid Build Coastguard Worker                                            reinterpret_cast<const unsigned char*>(wait.data()),
126*795d594fSAndroid Build Coastguard Worker                                            wait.size() * sizeof(jthread),
127*795d594fSAndroid Build Coastguard Worker                                            reinterpret_cast<unsigned char**>(&usage->waiters));
128*795d594fSAndroid Build Coastguard Worker   if (ret != OK) {
129*795d594fSAndroid Build Coastguard Worker     return ret;
130*795d594fSAndroid Build Coastguard Worker   }
131*795d594fSAndroid Build Coastguard Worker   return CopyDataIntoJvmtiBuffer(env,
132*795d594fSAndroid Build Coastguard Worker                                  reinterpret_cast<const unsigned char*>(notify_wait.data()),
133*795d594fSAndroid Build Coastguard Worker                                  notify_wait.size() * sizeof(jthread),
134*795d594fSAndroid Build Coastguard Worker                                  reinterpret_cast<unsigned char**>(&usage->notify_waiters));
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
138