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