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 #ifndef ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 18 #define ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 19 20 #include <stdint.h> 21 22 #include <map> 23 24 #include "jdwp/jdwp.h" 25 #include "mirror/art_field-inl.h" 26 #include "mirror/class.h" 27 #include "mirror/class-inl.h" 28 #include "mirror/object-inl.h" 29 #include "safe_map.h" 30 31 namespace art { 32 33 struct ObjectRegistryEntry { 34 // Is jni_reference a weak global or a regular global reference? 35 jobjectRefType jni_reference_type; 36 37 // The reference itself. 38 jobject jni_reference; 39 40 // A reference count, so we can implement DisposeObject. 41 int32_t reference_count; 42 43 // The corresponding id, so we only need one map lookup in Add. 44 JDWP::ObjectId id; 45 }; 46 std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs); 47 48 // Tracks those objects currently known to the debugger, so we can use consistent ids when 49 // referring to them. Normally we keep JNI weak global references to objects, so they can 50 // still be garbage collected. The debugger can ask us to retain objects, though, so we can 51 // also promote references to regular JNI global references (and demote them back again if 52 // the debugger tells us that's okay). 53 class ObjectRegistry { 54 public: 55 ObjectRegistry(); 56 57 JDWP::ObjectId Add(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 58 JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 59 Get(JDWP::ObjectId id)60 template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 61 if (id == 0) { 62 return NULL; 63 } 64 return reinterpret_cast<T>(InternalGet(id)); 65 } 66 67 bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 68 69 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 70 71 void DisableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 72 void EnableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 73 74 bool IsCollected(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 75 76 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count) 77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 78 79 // Returned by Get when passed an invalid object id. 80 static mirror::Object* const kInvalidObject; 81 82 // This is needed to get the jobject instead of the Object*. 83 // Avoid using this and use standard Get when possible. 84 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 85 86 private: 87 JDWP::ObjectId InternalAdd(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 88 mirror::Object* InternalGet(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 89 void Demote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_); 90 void Promote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_); 91 92 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 93 94 typedef std::map<mirror::Object*, ObjectRegistryEntry>::iterator object_iterator; 95 std::map<mirror::Object*, ObjectRegistryEntry> object_to_entry_ GUARDED_BY(lock_); 96 97 typedef SafeMap<JDWP::ObjectId, ObjectRegistryEntry*>::iterator id_iterator; 98 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_); 99 100 size_t next_id_ GUARDED_BY(lock_); 101 }; 102 103 } // namespace art 104 105 #endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 106