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_object.h"
33 
34 #include "art_jvmti.h"
35 #include "mirror/object-inl.h"
36 #include "scoped_thread_state_change-inl.h"
37 #include "thread-current-inl.h"
38 #include "thread_list.h"
39 #include "ti_thread.h"
40 
41 namespace openjdkjvmti {
42 
GetObjectSize(jvmtiEnv * env ATTRIBUTE_UNUSED,jobject jobject,jlong * size_ptr)43 jvmtiError ObjectUtil::GetObjectSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
44                                      jobject jobject,
45                                      jlong* size_ptr) {
46   if (jobject == nullptr) {
47     return ERR(INVALID_OBJECT);
48   }
49   if (size_ptr == nullptr) {
50     return ERR(NULL_POINTER);
51   }
52 
53   art::ScopedObjectAccess soa(art::Thread::Current());
54   art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobject);
55 
56   *size_ptr = object->SizeOf();
57   return ERR(NONE);
58 }
59 
GetObjectHashCode(jvmtiEnv * env ATTRIBUTE_UNUSED,jobject jobject,jint * hash_code_ptr)60 jvmtiError ObjectUtil::GetObjectHashCode(jvmtiEnv* env ATTRIBUTE_UNUSED,
61                                          jobject jobject,
62                                          jint* hash_code_ptr) {
63   if (jobject == nullptr) {
64     return ERR(INVALID_OBJECT);
65   }
66   if (hash_code_ptr == nullptr) {
67     return ERR(NULL_POINTER);
68   }
69 
70   art::ScopedObjectAccess soa(art::Thread::Current());
71   art::ObjPtr<art::mirror::Object> object = soa.Decode<art::mirror::Object>(jobject);
72 
73   *hash_code_ptr = object->IdentityHashCode();
74 
75   return ERR(NONE);
76 }
77 
GetObjectMonitorUsage(jvmtiEnv * baseenv,jobject obj,jvmtiMonitorUsage * usage)78 jvmtiError ObjectUtil::GetObjectMonitorUsage(
79     jvmtiEnv* baseenv, jobject obj, jvmtiMonitorUsage* usage) {
80   ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(baseenv);
81   if (obj == nullptr) {
82     return ERR(INVALID_OBJECT);
83   }
84   if (usage == nullptr) {
85     return ERR(NULL_POINTER);
86   }
87   art::Thread* self = art::Thread::Current();
88   ThreadUtil::SuspendCheck(self);
89   art::JNIEnvExt* jni = self->GetJniEnv();
90   std::vector<jthread> wait;
91   std::vector<jthread> notify_wait;
92   {
93     art::ScopedObjectAccess soa(self);      // Now we know we have the shared lock.
94     art::ScopedThreadSuspension sts(self, art::kNative);
95     art::ScopedSuspendAll ssa("GetObjectMonitorUsage", /*long_suspend=*/false);
96     art::ObjPtr<art::mirror::Object> target(self->DecodeJObject(obj));
97     // This gets the list of threads trying to lock or wait on the monitor.
98     art::MonitorInfo info(target.Ptr());
99     usage->owner = info.owner_ != nullptr ?
100         jni->AddLocalReference<jthread>(info.owner_->GetPeerFromOtherThread()) : nullptr;
101     usage->entry_count = info.entry_count_;
102     for (art::Thread* thd : info.waiters_) {
103       // RI seems to consider waiting for notify to be included in those waiting to acquire the
104       // monitor. We will match this behavior.
105       notify_wait.push_back(jni->AddLocalReference<jthread>(thd->GetPeerFromOtherThread()));
106       wait.push_back(jni->AddLocalReference<jthread>(thd->GetPeerFromOtherThread()));
107     }
108     {
109       // Scan all threads to see which are waiting on this particular monitor.
110       art::MutexLock tll(self, *art::Locks::thread_list_lock_);
111       for (art::Thread* thd : art::Runtime::Current()->GetThreadList()->GetList()) {
112         if (thd != info.owner_ && target.Ptr() == thd->GetMonitorEnterObject()) {
113           wait.push_back(jni->AddLocalReference<jthread>(thd->GetPeerFromOtherThread()));
114         }
115       }
116     }
117   }
118   usage->waiter_count = wait.size();
119   usage->notify_waiter_count = notify_wait.size();
120   jvmtiError ret = CopyDataIntoJvmtiBuffer(env,
121                                            reinterpret_cast<const unsigned char*>(wait.data()),
122                                            wait.size() * sizeof(jthread),
123                                            reinterpret_cast<unsigned char**>(&usage->waiters));
124   if (ret != OK) {
125     return ret;
126   }
127   return CopyDataIntoJvmtiBuffer(env,
128                                  reinterpret_cast<const unsigned char*>(notify_wait.data()),
129                                  notify_wait.size() * sizeof(jthread),
130                                  reinterpret_cast<unsigned char**>(&usage->notify_waiters));
131 }
132 
133 }  // namespace openjdkjvmti
134