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