blob: 99a005df21377f19fd594a03010374bb2bc038f2 [file] [log] [blame]
Elliott Hughes64f574f2013-02-20 14:57:12 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "object_registry.h"
18
Sebastien Hertze2d628b2014-10-23 15:39:33 +020019#include "handle_scope-inl.h"
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070020#include "jni_internal.h"
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "mirror/class.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080022#include "scoped_thread_state_change.h"
23
24namespace art {
25
Elliott Hughes64f574f2013-02-20 14:57:12 -080026std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) {
27 os << "ObjectRegistryEntry[" << rhs.jni_reference_type
28 << ",reference=" << rhs.jni_reference
29 << ",count=" << rhs.reference_count
30 << ",id=" << rhs.id << "]";
31 return os;
32}
33
Elliott Hughes0f827162013-02-26 12:12:58 -080034ObjectRegistry::ObjectRegistry()
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070035 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080036}
37
38JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) {
39 return InternalAdd(c);
40}
41
42JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) {
43 return InternalAdd(o);
44}
45
46JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070047 if (o == nullptr) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080048 return 0;
49 }
50
Sebastien Hertze2d628b2014-10-23 15:39:33 +020051 Thread* const self = Thread::Current();
Sebastien Hertze4266c52014-10-29 12:06:51 +010052 self->AssertNoPendingException();
53
Sebastien Hertze2d628b2014-10-23 15:39:33 +020054 StackHandleScope<1> hs(self);
55 Handle<mirror::Object> obj_h(hs.NewHandle(o));
56
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070057 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
Sebastien Hertze2d628b2014-10-23 15:39:33 +020058 int32_t identity_hash_code = obj_h->IdentityHashCode();
59
60 ScopedObjectAccessUnchecked soa(self);
Elliott Hughes64f574f2013-02-20 14:57:12 -080061 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070062 ObjectRegistryEntry* entry = nullptr;
Sebastien Hertze2d628b2014-10-23 15:39:33 +020063 if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080064 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080065 ++entry->reference_count;
66 } else {
67 entry = new ObjectRegistryEntry;
68 entry->jni_reference_type = JNIWeakGlobalRefType;
69 entry->jni_reference = nullptr;
70 entry->reference_count = 0;
71 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070072 entry->identity_hash_code = identity_hash_code;
73 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -080074
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080075 // This object isn't in the registry yet, so add it.
76 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080077
Sebastien Hertze2d628b2014-10-23 15:39:33 +020078 jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get());
Elliott Hughes64f574f2013-02-20 14:57:12 -080079
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080080 entry->jni_reference_type = JNIWeakGlobalRefType;
81 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
82 entry->reference_count = 1;
83 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080084
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080085 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080086
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080087 env->DeleteLocalRef(local_reference);
88 }
89 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080090}
91
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070092bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
93 ObjectRegistryEntry** out_entry) {
94 DCHECK(o != nullptr);
95 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
96 it != end && it->first == identity_hash_code; ++it) {
97 ObjectRegistryEntry* entry = it->second;
98 if (o == self->DecodeJObject(entry->jni_reference)) {
99 if (out_entry != nullptr) {
100 *out_entry = entry;
101 }
102 return true;
103 }
104 }
105 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800106}
107
108void ObjectRegistry::Clear() {
Sebastien Hertz55f65342015-01-13 22:48:34 +0100109 Thread* const self = Thread::Current();
110
111 // We must not hold the mutator lock exclusively if we want to delete weak global
112 // references. Otherwise this can lead to a deadlock with a running GC:
113 // 1. GC thread disables access to weak global references, then releases
114 // mutator lock.
115 // 2. JDWP thread takes mutator lock exclusively after suspending all
116 // threads.
117 // 3. GC thread waits for shared mutator lock which is held by JDWP
118 // thread.
119 // 4. JDWP thread clears weak global references but need to wait for GC
120 // thread to re-enable access to them.
121 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
122
Elliott Hughes64f574f2013-02-20 14:57:12 -0800123 MutexLock mu(self, lock_);
124 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800125 // Delete all the JNI references.
126 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800127 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200128 const ObjectRegistryEntry* entry = pair.second;
129 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
130 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800131 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200132 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800133 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200134 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800135 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800136 // Clear the maps.
137 object_to_entry_.clear();
138 id_to_entry_.clear();
139}
140
Ian Rogersc0542af2014-09-03 16:16:56 -0700141mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800142 Thread* self = Thread::Current();
143 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800144 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800145 if (it == id_to_entry_.end()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700146 *error = JDWP::ERR_INVALID_OBJECT;
147 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800148 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800149 ObjectRegistryEntry& entry = *it->second;
Ian Rogersc0542af2014-09-03 16:16:56 -0700150 *error = JDWP::ERR_NONE;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800151 return self->DecodeJObject(entry.jni_reference);
152}
153
Jeff Hao449db332013-04-12 18:30:52 -0700154jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100155 if (id == 0) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200156 return nullptr;
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100157 }
Jeff Hao449db332013-04-12 18:30:52 -0700158 Thread* self = Thread::Current();
159 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800160 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700161 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800162 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700163 return entry.jni_reference;
164}
165
Elliott Hughes64f574f2013-02-20 14:57:12 -0800166void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
167 Thread* self = Thread::Current();
168 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800169 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100170 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800171 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800172}
173
174void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
175 Thread* self = Thread::Current();
176 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800177 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100178 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800179 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800180}
181
182void ObjectRegistry::Demote(ObjectRegistryEntry& entry) {
183 if (entry.jni_reference_type == JNIGlobalRefType) {
184 Thread* self = Thread::Current();
185 JNIEnv* env = self->GetJniEnv();
186 jobject global = entry.jni_reference;
187 entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference);
188 entry.jni_reference_type = JNIWeakGlobalRefType;
189 env->DeleteGlobalRef(global);
190 }
191}
192
193void ObjectRegistry::Promote(ObjectRegistryEntry& entry) {
194 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
195 Thread* self = Thread::Current();
196 JNIEnv* env = self->GetJniEnv();
197 jobject weak = entry.jni_reference;
198 entry.jni_reference = env->NewGlobalRef(entry.jni_reference);
199 entry.jni_reference_type = JNIGlobalRefType;
200 env->DeleteWeakGlobalRef(weak);
201 }
202}
203
204bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
205 Thread* self = Thread::Current();
206 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800207 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100208 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800209 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800210 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
211 JNIEnv* env = self->GetJniEnv();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200212 return env->IsSameObject(entry.jni_reference, nullptr); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800213 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700214 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800215 }
216}
217
218void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
219 Thread* self = Thread::Current();
220 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800221 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800222 if (it == id_to_entry_.end()) {
223 return;
224 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800225 ObjectRegistryEntry* entry = it->second;
226 entry->reference_count -= reference_count;
227 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800228 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700229 // Erase the object from the maps. Note object may be null if it's
230 // a weak ref and the GC has cleared it.
231 int32_t hash_code = entry->identity_hash_code;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800232 for (auto inner_it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end();
233 inner_it != end && inner_it->first == hash_code; ++inner_it) {
234 if (entry == inner_it->second) {
235 object_to_entry_.erase(inner_it);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700236 break;
237 }
238 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800239 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
240 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800241 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800242 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800243 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800244 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800245 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800246 }
247}
248
Elliott Hughes64f574f2013-02-20 14:57:12 -0800249} // namespace art