• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/casts.h"
26 #include "handle.h"
27 #include "jdwp/jdwp.h"
28 #include "safe_map.h"
29 
30 namespace art {
31 
32 namespace mirror {
33   class Object;
34   class Class;
35 }  // namespace mirror
36 
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;
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;
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 
65   JDWP::ObjectId Add(mirror::Object* o)
66       SHARED_REQUIRES(Locks::mutator_lock_)
67       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
68 
69   JDWP::RefTypeId AddRefType(mirror::Class* c)
70       SHARED_REQUIRES(Locks::mutator_lock_)
71       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
72 
73   template<class T>
74   JDWP::ObjectId Add(Handle<T> obj_h)
75       SHARED_REQUIRES(Locks::mutator_lock_)
76       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
77 
78   JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
79       SHARED_REQUIRES(Locks::mutator_lock_)
80       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
81 
Get(JDWP::ObjectId id,JDWP::JdwpError * error)82   template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
83       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_) {
84     if (id == 0) {
85       *error = JDWP::ERR_NONE;
86       return nullptr;
87     }
88     return down_cast<T>(InternalGet(id, error));
89   }
90 
91   void Clear() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
92 
93   void DisableCollection(JDWP::ObjectId id)
94       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
95 
96   void EnableCollection(JDWP::ObjectId id)
97       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
98 
99   bool IsCollected(JDWP::ObjectId id)
100       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
101 
102   void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
103       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
104 
105   // This is needed to get the jobject instead of the Object*.
106   // Avoid using this and use standard Get when possible.
107   jobject GetJObject(JDWP::ObjectId id) SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
108 
109  private:
110   template<class T>
111   JDWP::ObjectId InternalAdd(Handle<T> obj_h)
112       SHARED_REQUIRES(Locks::mutator_lock_)
113       REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
114 
115   mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
116       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
117 
118   void Demote(ObjectRegistryEntry& entry)
119       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
120 
121   void Promote(ObjectRegistryEntry& entry)
122       SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
123 
124   bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
125                       ObjectRegistryEntry** out_entry)
126       REQUIRES(lock_) SHARED_REQUIRES(Locks::mutator_lock_);
127 
128   Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
129   std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
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
136 
137 #endif  // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
138