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