blob: d0ea59da71bd5f02d5a45f03eff9a27d9c5bb33c [file] [log] [blame]
Elliott Hughes64f574f2013-02-20 14:57:12 -08001/*
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#include <stdint.h>
18
19#include <map>
20
21#include "jdwp/jdwp.h"
22#include "mirror/class.h"
23#include "mirror/class-inl.h"
24#include "mirror/field-inl.h"
25#include "mirror/object-inl.h"
26#include "safe_map.h"
27
28namespace art {
29
30struct ObjectRegistryEntry {
31 // Is jni_reference a weak global or a regular global reference?
32 jobjectRefType jni_reference_type;
33
34 // The reference itself.
35 jobject jni_reference;
36
37 // A reference count, so we can implement DisposeObject.
38 int32_t reference_count;
39
40 // The corresponding id, so we only need one map lookup in Add.
41 JDWP::ObjectId id;
42};
43std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
44
45// Tracks those objects currently known to the debugger, so we can use consistent ids when
46// referring to them. Normally we keep JNI weak global references to objects, so they can
47// still be garbage collected. The debugger can ask us to retain objects, though, so we can
48// also promote references to regular JNI global references (and demote them back again if
49// the debugger tells us that's okay).
50class ObjectRegistry {
51 public:
52 ObjectRegistry();
53
54 JDWP::ObjectId Add(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
55 JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
56
57 template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
58 if (id == 0) {
59 return NULL;
60 }
61 return reinterpret_cast<T>(InternalGet(id));
62 }
63
64 bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
65
Elliott Hughes0f827162013-02-26 12:12:58 -080066 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080067
Elliott Hughes0f827162013-02-26 12:12:58 -080068 void DisableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69 void EnableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080070
Elliott Hughes0f827162013-02-26 12:12:58 -080071 bool IsCollected(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080072
73 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
74 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
75
76 // Returned by Get when passed an invalid object id.
77 static mirror::Object* const kInvalidObject;
78
Elliott Hughes09201632013-04-15 15:50:07 -070079 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -070080 // Avoid using this and use standard Get when possible.
81 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82
Elliott Hughes64f574f2013-02-20 14:57:12 -080083 private:
84 JDWP::ObjectId InternalAdd(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
85 mirror::Object* InternalGet(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0f827162013-02-26 12:12:58 -080086 void Demote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_);
87 void Promote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080088
89 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
90
91 typedef std::map<mirror::Object*, ObjectRegistryEntry>::iterator object_iterator;
92 std::map<mirror::Object*, ObjectRegistryEntry> object_to_entry_ GUARDED_BY(lock_);
93
94 typedef SafeMap<JDWP::ObjectId, ObjectRegistryEntry*>::iterator id_iterator;
95 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
96
97 size_t next_id_ GUARDED_BY(lock_);
98};
99
100} // namespace art