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