blob: 3c6cb15f74b4631ff7ead5797df97d7b6881dcdb [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_field-inl.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080026#include "mirror/class.h"
27#include "mirror/class-inl.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080028#include "mirror/object-inl.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080029#include "object_callbacks.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080030#include "safe_map.h"
31
32namespace art {
33
34struct ObjectRegistryEntry {
35 // Is jni_reference a weak global or a regular global reference?
36 jobjectRefType jni_reference_type;
37
38 // The reference itself.
39 jobject jni_reference;
40
41 // A reference count, so we can implement DisposeObject.
42 int32_t reference_count;
43
44 // The corresponding id, so we only need one map lookup in Add.
45 JDWP::ObjectId id;
46};
47std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
48
49// Tracks those objects currently known to the debugger, so we can use consistent ids when
50// referring to them. Normally we keep JNI weak global references to objects, so they can
51// still be garbage collected. The debugger can ask us to retain objects, though, so we can
52// also promote references to regular JNI global references (and demote them back again if
53// the debugger tells us that's okay).
54class ObjectRegistry {
55 public:
56 ObjectRegistry();
57
58 JDWP::ObjectId Add(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
59 JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
60
61 template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62 if (id == 0) {
63 return NULL;
64 }
65 return reinterpret_cast<T>(InternalGet(id));
66 }
67
68 bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
69
Elliott Hughes0f827162013-02-26 12:12:58 -080070 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080071
Elliott Hughes0f827162013-02-26 12:12:58 -080072 void DisableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
73 void EnableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080074
Elliott Hughes0f827162013-02-26 12:12:58 -080075 bool IsCollected(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080076
77 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
78 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
79
80 // Returned by Get when passed an invalid object id.
81 static mirror::Object* const kInvalidObject;
82
Elliott Hughes09201632013-04-15 15:50:07 -070083 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -070084 // Avoid using this and use standard Get when possible.
85 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
86
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080087 // Visit, objects are treated as system weaks.
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080088 void UpdateObjectPointers(IsMarkedCallback* callback, void* arg)
Mathieu Chartier412c7fc2014-02-07 12:18:39 -080089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
90
91 // We have allow / disallow functionality since we use system weak sweeping logic to update moved
92 // objects inside of the object_to_entry_ map.
93 void AllowNewObjects() LOCKS_EXCLUDED(lock_);
94 void DisallowNewObjects() LOCKS_EXCLUDED(lock_);
95
Elliott Hughes64f574f2013-02-20 14:57:12 -080096 private:
97 JDWP::ObjectId InternalAdd(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98 mirror::Object* InternalGet(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0f827162013-02-26 12:12:58 -080099 void Demote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_);
100 void Promote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800101
102 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800103 bool allow_new_objects_ GUARDED_BY(lock_);
104 ConditionVariable condition_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800105
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800106 std::map<mirror::Object*, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800107 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
108
109 size_t next_id_ GUARDED_BY(lock_);
110};
111
112} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700113
114#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_