blob: e415c3d1cd403f91375860c9b247d1a5b43d6cbc [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();
52 StackHandleScope<1> hs(self);
53 Handle<mirror::Object> obj_h(hs.NewHandle(o));
54
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070055 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
Sebastien Hertze2d628b2014-10-23 15:39:33 +020056 int32_t identity_hash_code = obj_h->IdentityHashCode();
57
58 ScopedObjectAccessUnchecked soa(self);
Elliott Hughes64f574f2013-02-20 14:57:12 -080059 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070060 ObjectRegistryEntry* entry = nullptr;
Sebastien Hertze2d628b2014-10-23 15:39:33 +020061 if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080062 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080063 ++entry->reference_count;
64 } else {
65 entry = new ObjectRegistryEntry;
66 entry->jni_reference_type = JNIWeakGlobalRefType;
67 entry->jni_reference = nullptr;
68 entry->reference_count = 0;
69 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070070 entry->identity_hash_code = identity_hash_code;
71 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -080072
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080073 // This object isn't in the registry yet, so add it.
74 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -080075
Sebastien Hertze2d628b2014-10-23 15:39:33 +020076 jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get());
Elliott Hughes64f574f2013-02-20 14:57:12 -080077
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080078 entry->jni_reference_type = JNIWeakGlobalRefType;
79 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
80 entry->reference_count = 1;
81 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -080082
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080083 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -080084
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080085 env->DeleteLocalRef(local_reference);
86 }
87 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -080088}
89
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070090bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
91 ObjectRegistryEntry** out_entry) {
92 DCHECK(o != nullptr);
93 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
94 it != end && it->first == identity_hash_code; ++it) {
95 ObjectRegistryEntry* entry = it->second;
96 if (o == self->DecodeJObject(entry->jni_reference)) {
97 if (out_entry != nullptr) {
98 *out_entry = entry;
99 }
100 return true;
101 }
102 }
103 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800104}
105
106void ObjectRegistry::Clear() {
Sebastien Hertz55f65342015-01-13 22:48:34 +0100107 Thread* const self = Thread::Current();
108
109 // We must not hold the mutator lock exclusively if we want to delete weak global
110 // references. Otherwise this can lead to a deadlock with a running GC:
111 // 1. GC thread disables access to weak global references, then releases
112 // mutator lock.
113 // 2. JDWP thread takes mutator lock exclusively after suspending all
114 // threads.
115 // 3. GC thread waits for shared mutator lock which is held by JDWP
116 // thread.
117 // 4. JDWP thread clears weak global references but need to wait for GC
118 // thread to re-enable access to them.
119 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
120
Elliott Hughes64f574f2013-02-20 14:57:12 -0800121 MutexLock mu(self, lock_);
122 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800123 // Delete all the JNI references.
124 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800125 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200126 const ObjectRegistryEntry* entry = pair.second;
127 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
128 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800129 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200130 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800131 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200132 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800133 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800134 // Clear the maps.
135 object_to_entry_.clear();
136 id_to_entry_.clear();
137}
138
Ian Rogersc0542af2014-09-03 16:16:56 -0700139mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800140 Thread* self = Thread::Current();
141 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800142 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800143 if (it == id_to_entry_.end()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700144 *error = JDWP::ERR_INVALID_OBJECT;
145 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800146 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800147 ObjectRegistryEntry& entry = *it->second;
Ian Rogersc0542af2014-09-03 16:16:56 -0700148 *error = JDWP::ERR_NONE;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800149 return self->DecodeJObject(entry.jni_reference);
150}
151
Jeff Hao449db332013-04-12 18:30:52 -0700152jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100153 if (id == 0) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200154 return nullptr;
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100155 }
Jeff Hao449db332013-04-12 18:30:52 -0700156 Thread* self = Thread::Current();
157 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800158 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700159 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800160 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700161 return entry.jni_reference;
162}
163
Elliott Hughes64f574f2013-02-20 14:57:12 -0800164void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
165 Thread* self = Thread::Current();
166 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800167 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100168 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800169 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800170}
171
172void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
173 Thread* self = Thread::Current();
174 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800175 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100176 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800177 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800178}
179
180void ObjectRegistry::Demote(ObjectRegistryEntry& entry) {
181 if (entry.jni_reference_type == JNIGlobalRefType) {
182 Thread* self = Thread::Current();
183 JNIEnv* env = self->GetJniEnv();
184 jobject global = entry.jni_reference;
185 entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference);
186 entry.jni_reference_type = JNIWeakGlobalRefType;
187 env->DeleteGlobalRef(global);
188 }
189}
190
191void ObjectRegistry::Promote(ObjectRegistryEntry& entry) {
192 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
193 Thread* self = Thread::Current();
194 JNIEnv* env = self->GetJniEnv();
195 jobject weak = entry.jni_reference;
196 entry.jni_reference = env->NewGlobalRef(entry.jni_reference);
197 entry.jni_reference_type = JNIGlobalRefType;
198 env->DeleteWeakGlobalRef(weak);
199 }
200}
201
202bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
203 Thread* self = Thread::Current();
204 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800205 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100206 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800207 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800208 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
209 JNIEnv* env = self->GetJniEnv();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200210 return env->IsSameObject(entry.jni_reference, nullptr); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800211 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700212 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800213 }
214}
215
216void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
217 Thread* self = Thread::Current();
218 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800219 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800220 if (it == id_to_entry_.end()) {
221 return;
222 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800223 ObjectRegistryEntry* entry = it->second;
224 entry->reference_count -= reference_count;
225 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800226 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700227 // Erase the object from the maps. Note object may be null if it's
228 // a weak ref and the GC has cleared it.
229 int32_t hash_code = entry->identity_hash_code;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800230 for (auto inner_it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end();
231 inner_it != end && inner_it->first == hash_code; ++inner_it) {
232 if (entry == inner_it->second) {
233 object_to_entry_.erase(inner_it);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700234 break;
235 }
236 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800237 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
238 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800239 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800240 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800241 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800242 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800243 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800244 }
245}
246
Elliott Hughes64f574f2013-02-20 14:57:12 -0800247} // namespace art