blob: 91239944feeb8bc8b48fde7803a7fa594548e19f [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() {
107 Thread* self = Thread::Current();
108 MutexLock mu(self, lock_);
109 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800110 // Delete all the JNI references.
111 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800112 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200113 const ObjectRegistryEntry* entry = pair.second;
114 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
115 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800116 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200117 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800118 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200119 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800120 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800121 // Clear the maps.
122 object_to_entry_.clear();
123 id_to_entry_.clear();
124}
125
Ian Rogersc0542af2014-09-03 16:16:56 -0700126mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800127 Thread* self = Thread::Current();
128 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800129 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800130 if (it == id_to_entry_.end()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700131 *error = JDWP::ERR_INVALID_OBJECT;
132 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800133 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800134 ObjectRegistryEntry& entry = *it->second;
Ian Rogersc0542af2014-09-03 16:16:56 -0700135 *error = JDWP::ERR_NONE;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800136 return self->DecodeJObject(entry.jni_reference);
137}
138
Jeff Hao449db332013-04-12 18:30:52 -0700139jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100140 if (id == 0) {
141 return NULL;
142 }
Jeff Hao449db332013-04-12 18:30:52 -0700143 Thread* self = Thread::Current();
144 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800145 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700146 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800147 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700148 return entry.jni_reference;
149}
150
Elliott Hughes64f574f2013-02-20 14:57:12 -0800151void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
152 Thread* self = Thread::Current();
153 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800154 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100155 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800156 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800157}
158
159void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
160 Thread* self = Thread::Current();
161 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800162 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100163 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800164 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800165}
166
167void 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
178void 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
189bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
190 Thread* self = Thread::Current();
191 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800192 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100193 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800194 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800195 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
196 JNIEnv* env = self->GetJniEnv();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700197 return env->IsSameObject(entry.jni_reference, NULL); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800198 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700199 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800200 }
201}
202
203void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
204 Thread* self = Thread::Current();
205 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800206 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800207 if (it == id_to_entry_.end()) {
208 return;
209 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800210 ObjectRegistryEntry* entry = it->second;
211 entry->reference_count -= reference_count;
212 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800213 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700214 // 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 Gampe277ccbd2014-11-03 21:36:10 -0800217 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 Yamauchib5a9e3d2014-06-09 12:11:20 -0700221 break;
222 }
223 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800224 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
225 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800226 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800227 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800228 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800229 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800230 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800231 }
232}
233
Elliott Hughes64f574f2013-02-20 14:57:12 -0800234} // namespace art