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