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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 20 | #include <jni.h> |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <map> |
| 24 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 25 | #include "base/casts.h" |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 26 | #include "handle.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 27 | #include "jdwp/jdwp.h" |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 28 | #include "safe_map.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 32 | namespace mirror { |
| 33 | class Object; |
| 34 | class Class; |
| 35 | } // namespace mirror |
| 36 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 37 | struct ObjectRegistryEntry { |
| 38 | // Is jni_reference a weak global or a regular global reference? |
| 39 | jobjectRefType jni_reference_type; |
| 40 | |
| 41 | // The reference itself. |
| 42 | jobject jni_reference; |
| 43 | |
| 44 | // A reference count, so we can implement DisposeObject. |
| 45 | int32_t reference_count; |
| 46 | |
| 47 | // The corresponding id, so we only need one map lookup in Add. |
| 48 | JDWP::ObjectId id; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 49 | |
| 50 | // The identity hash code of the object. This is the same as the key |
| 51 | // for object_to_entry_. Store this for DisposeObject(). |
| 52 | int32_t identity_hash_code; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 53 | }; |
| 54 | std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs); |
| 55 | |
| 56 | // Tracks those objects currently known to the debugger, so we can use consistent ids when |
| 57 | // referring to them. Normally we keep JNI weak global references to objects, so they can |
| 58 | // still be garbage collected. The debugger can ask us to retain objects, though, so we can |
| 59 | // also promote references to regular JNI global references (and demote them back again if |
| 60 | // the debugger tells us that's okay). |
| 61 | class ObjectRegistry { |
| 62 | public: |
| 63 | ObjectRegistry(); |
| 64 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 65 | JDWP::ObjectId Add(mirror::Object* o) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 66 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 67 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 68 | |
Sebastien Hertz | 070f732 | 2014-09-09 12:08:49 +0200 | [diff] [blame] | 69 | JDWP::RefTypeId AddRefType(mirror::Class* c) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 70 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 71 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 72 | |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 73 | template<class T> |
| 74 | JDWP::ObjectId Add(Handle<T> obj_h) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 75 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 76 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 77 | |
| 78 | JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 79 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 80 | REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_); |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 81 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 82 | template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 83 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_) { |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 84 | if (id == 0) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 85 | *error = JDWP::ERR_NONE; |
| 86 | return nullptr; |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 87 | } |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 88 | return down_cast<T>(InternalGet(id, error)); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 91 | void Clear() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 92 | |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 93 | void DisableCollection(JDWP::ObjectId id) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 94 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 95 | |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 96 | void EnableCollection(JDWP::ObjectId id) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 97 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 98 | |
| 99 | bool IsCollected(JDWP::ObjectId id) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 100 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 101 | |
| 102 | void DisposeObject(JDWP::ObjectId id, uint32_t reference_count) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 103 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 104 | |
Elliott Hughes | 0920163 | 2013-04-15 15:50:07 -0700 | [diff] [blame] | 105 | // This is needed to get the jobject instead of the Object*. |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 106 | // Avoid using this and use standard Get when possible. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 107 | jobject GetJObject(JDWP::ObjectId id) SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Jeff Hao | 449db33 | 2013-04-12 18:30:52 -0700 | [diff] [blame] | 108 | |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 109 | private: |
Sebastien Hertz | 261bc04 | 2015-04-08 09:36:07 +0200 | [diff] [blame] | 110 | template<class T> |
| 111 | JDWP::ObjectId InternalAdd(Handle<T> obj_h) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 112 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 113 | REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 114 | |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 115 | mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 116 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 117 | |
| 118 | void Demote(ObjectRegistryEntry& entry) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 120 | |
| 121 | void Promote(ObjectRegistryEntry& entry) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 122 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_); |
Sebastien Hertz | 4537c41 | 2014-08-28 14:41:50 +0200 | [diff] [blame] | 123 | |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 124 | bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code, |
| 125 | ObjectRegistryEntry** out_entry) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 126 | REQUIRES(lock_) SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 127 | |
| 128 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Hiroshi Yamauchi | b5a9e3d | 2014-06-09 12:11:20 -0700 | [diff] [blame] | 129 | std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_); |
Elliott Hughes | 64f574f | 2013-02-20 14:57:12 -0800 | [diff] [blame] | 130 | SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_); |
| 131 | |
| 132 | size_t next_id_ GUARDED_BY(lock_); |
| 133 | }; |
| 134 | |
| 135 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 136 | |
| 137 | #endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ |