blob: 0d39806f9ed2b5ab0a5a621ee0305269ddcaac76 [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
66 void Clear();
67
68 void DisableCollection(JDWP::ObjectId id);
69 void EnableCollection(JDWP::ObjectId id);
70
71 bool IsCollected(JDWP::ObjectId id);
72
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
79 private:
80 JDWP::ObjectId InternalAdd(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81 mirror::Object* InternalGet(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82 void Demote(ObjectRegistryEntry& entry);
83 void Promote(ObjectRegistryEntry& entry);
84
85 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
86
87 typedef std::map<mirror::Object*, ObjectRegistryEntry>::iterator object_iterator;
88 std::map<mirror::Object*, ObjectRegistryEntry> object_to_entry_ GUARDED_BY(lock_);
89
90 typedef SafeMap<JDWP::ObjectId, ObjectRegistryEntry*>::iterator id_iterator;
91 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
92
93 size_t next_id_ GUARDED_BY(lock_);
94};
95
96} // namespace art