Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame^] | 19 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 20 | #include "mirror/class.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 25 | std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) { |
| 26 | os << "ObjectRegistryEntry[" << rhs.jni_reference_type |
| 27 | << ",reference=" << rhs.jni_reference |
| 28 | << ",count=" << rhs.reference_count |
| 29 | << ",id=" << rhs.id << "]"; |
| 30 | return os; |
| 31 | } |
| 32 | |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 33 | ObjectRegistry::ObjectRegistry() |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 34 | : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | JDWP::RefTypeId ObjectRegistry::AddRefType(mirror::Class* c) { |
| 38 | return InternalAdd(c); |
| 39 | } |
| 40 | |
| 41 | JDWP::ObjectId ObjectRegistry::Add(mirror::Object* o) { |
| 42 | return InternalAdd(o); |
| 43 | } |
| 44 | |
| 45 | JDWP::ObjectId ObjectRegistry::InternalAdd(mirror::Object* o) { |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 46 | if (o == nullptr) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame^] | 50 | Thread* const self = Thread::Current(); |
| 51 | StackHandleScope<1> hs(self); |
| 52 | Handle<mirror::Object> obj_h(hs.NewHandle(o)); |
| 53 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 54 | // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock. |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame^] | 55 | int32_t identity_hash_code = obj_h->IdentityHashCode(); |
| 56 | |
| 57 | ScopedObjectAccessUnchecked soa(self); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 58 | MutexLock mu(soa.Self(), lock_); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 59 | ObjectRegistryEntry* entry = nullptr; |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame^] | 60 | if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 61 | // This object was already in our map. |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 62 | ++entry->reference_count; |
| 63 | } else { |
| 64 | entry = new ObjectRegistryEntry; |
| 65 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 66 | entry->jni_reference = nullptr; |
| 67 | entry->reference_count = 0; |
| 68 | entry->id = 0; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 69 | entry->identity_hash_code = identity_hash_code; |
| 70 | object_to_entry_.insert(std::make_pair(identity_hash_code, entry)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 71 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 72 | // This object isn't in the registry yet, so add it. |
| 73 | JNIEnv* env = soa.Env(); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 74 | |
Sebastien Hertz | e2d628b | 2014-10-23 15:39:33 +0200 | [diff] [blame^] | 75 | jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get()); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 76 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 77 | entry->jni_reference_type = JNIWeakGlobalRefType; |
| 78 | entry->jni_reference = env->NewWeakGlobalRef(local_reference); |
| 79 | entry->reference_count = 1; |
| 80 | entry->id = next_id_++; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 81 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 82 | id_to_entry_.Put(entry->id, entry); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 83 | |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 84 | env->DeleteLocalRef(local_reference); |
| 85 | } |
| 86 | return entry->id; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 89 | bool ObjectRegistry::ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code, |
| 90 | ObjectRegistryEntry** out_entry) { |
| 91 | DCHECK(o != nullptr); |
| 92 | for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end(); |
| 93 | it != end && it->first == identity_hash_code; ++it) { |
| 94 | ObjectRegistryEntry* entry = it->second; |
| 95 | if (o == self->DecodeJObject(entry->jni_reference)) { |
| 96 | if (out_entry != nullptr) { |
| 97 | *out_entry = entry; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | return false; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void ObjectRegistry::Clear() { |
| 106 | Thread* self = Thread::Current(); |
| 107 | MutexLock mu(self, lock_); |
| 108 | VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries"; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 109 | // Delete all the JNI references. |
| 110 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 111 | for (const auto& pair : object_to_entry_) { |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 112 | const ObjectRegistryEntry* entry = pair.second; |
| 113 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 114 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 115 | } else { |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 116 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 117 | } |
Sebastien Hertz | a032870 | 2014-06-25 22:06:12 +0200 | [diff] [blame] | 118 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 119 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 120 | // Clear the maps. |
| 121 | object_to_entry_.clear(); |
| 122 | id_to_entry_.clear(); |
| 123 | } |
| 124 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 125 | mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 126 | Thread* self = Thread::Current(); |
| 127 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 128 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 129 | if (it == id_to_entry_.end()) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 130 | *error = JDWP::ERR_INVALID_OBJECT; |
| 131 | return nullptr; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 132 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 133 | ObjectRegistryEntry& entry = *it->second; |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 134 | *error = JDWP::ERR_NONE; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 135 | return self->DecodeJObject(entry.jni_reference); |
| 136 | } |
| 137 | |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 138 | jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { |
Sebastien Hertz | 0630ab5 | 2013-11-28 18:53:35 +0100 | [diff] [blame] | 139 | if (id == 0) { |
| 140 | return NULL; |
| 141 | } |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 142 | Thread* self = Thread::Current(); |
| 143 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 144 | auto it = id_to_entry_.find(id); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 145 | CHECK(it != id_to_entry_.end()) << id; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 146 | ObjectRegistryEntry& entry = *it->second; |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 147 | return entry.jni_reference; |
| 148 | } |
| 149 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 150 | void ObjectRegistry::DisableCollection(JDWP::ObjectId id) { |
| 151 | Thread* self = Thread::Current(); |
| 152 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 153 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 154 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 155 | Promote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void ObjectRegistry::EnableCollection(JDWP::ObjectId id) { |
| 159 | Thread* self = Thread::Current(); |
| 160 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 161 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 162 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 163 | Demote(*it->second); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void ObjectRegistry::Demote(ObjectRegistryEntry& entry) { |
| 167 | if (entry.jni_reference_type == JNIGlobalRefType) { |
| 168 | Thread* self = Thread::Current(); |
| 169 | JNIEnv* env = self->GetJniEnv(); |
| 170 | jobject global = entry.jni_reference; |
| 171 | entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference); |
| 172 | entry.jni_reference_type = JNIWeakGlobalRefType; |
| 173 | env->DeleteGlobalRef(global); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void ObjectRegistry::Promote(ObjectRegistryEntry& entry) { |
| 178 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 179 | Thread* self = Thread::Current(); |
| 180 | JNIEnv* env = self->GetJniEnv(); |
| 181 | jobject weak = entry.jni_reference; |
| 182 | entry.jni_reference = env->NewGlobalRef(entry.jni_reference); |
| 183 | entry.jni_reference_type = JNIGlobalRefType; |
| 184 | env->DeleteWeakGlobalRef(weak); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool ObjectRegistry::IsCollected(JDWP::ObjectId id) { |
| 189 | Thread* self = Thread::Current(); |
| 190 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 191 | auto it = id_to_entry_.find(id); |
Sebastien Hertz | e96060a | 2013-12-11 12:06:28 +0100 | [diff] [blame] | 192 | CHECK(it != id_to_entry_.end()); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 193 | ObjectRegistryEntry& entry = *it->second; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 194 | if (entry.jni_reference_type == JNIWeakGlobalRefType) { |
| 195 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 196 | return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected? |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 197 | } else { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 198 | return false; // We hold a strong reference, so we know this is live. |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) { |
| 203 | Thread* self = Thread::Current(); |
| 204 | MutexLock mu(self, lock_); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 205 | auto it = id_to_entry_.find(id); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 206 | if (it == id_to_entry_.end()) { |
| 207 | return; |
| 208 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 209 | ObjectRegistryEntry* entry = it->second; |
| 210 | entry->reference_count -= reference_count; |
| 211 | if (entry->reference_count <= 0) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 212 | JNIEnv* env = self->GetJniEnv(); |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 213 | // Erase the object from the maps. Note object may be null if it's |
| 214 | // a weak ref and the GC has cleared it. |
| 215 | int32_t hash_code = entry->identity_hash_code; |
| 216 | for (auto it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end(); |
| 217 | it != end && it->first == hash_code; ++it) { |
| 218 | if (entry == it->second) { |
| 219 | object_to_entry_.erase(it); |
| 220 | break; |
| 221 | } |
| 222 | } |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 223 | if (entry->jni_reference_type == JNIWeakGlobalRefType) { |
| 224 | env->DeleteWeakGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 225 | } else { |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 226 | env->DeleteGlobalRef(entry->jni_reference); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 227 | } |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 228 | id_to_entry_.erase(id); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 229 | delete entry; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 233 | } // namespace art |