blob: f2f43c41e0f98ccb8bbbcbc8e3566d957bd16a77 [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
Ian Rogerse63db272014-07-15 15:36:11 -070020#include <jni.h>
Elliott Hughes64f574f2013-02-20 14:57:12 -080021#include <stdint.h>
22
23#include <map>
24
Ian Rogersc0542af2014-09-03 16:16:56 -070025#include "base/casts.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080026#include "jdwp/jdwp.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080027#include "safe_map.h"
28
29namespace art {
30
Ian Rogerse63db272014-07-15 15:36:11 -070031namespace mirror {
32 class Object;
33 class Class;
34} // namespace mirror
35
Elliott Hughes64f574f2013-02-20 14:57:12 -080036struct ObjectRegistryEntry {
37 // Is jni_reference a weak global or a regular global reference?
38 jobjectRefType jni_reference_type;
39
40 // The reference itself.
41 jobject jni_reference;
42
43 // A reference count, so we can implement DisposeObject.
44 int32_t reference_count;
45
46 // The corresponding id, so we only need one map lookup in Add.
47 JDWP::ObjectId id;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070048
49 // The identity hash code of the object. This is the same as the key
50 // for object_to_entry_. Store this for DisposeObject().
51 int32_t identity_hash_code;
Elliott Hughes64f574f2013-02-20 14:57:12 -080052};
53std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
54
55// Tracks those objects currently known to the debugger, so we can use consistent ids when
56// referring to them. Normally we keep JNI weak global references to objects, so they can
57// still be garbage collected. The debugger can ask us to retain objects, though, so we can
58// also promote references to regular JNI global references (and demote them back again if
59// the debugger tells us that's okay).
60class ObjectRegistry {
61 public:
62 ObjectRegistry();
63
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070064 JDWP::ObjectId Add(mirror::Object* o)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080066 JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
67
Ian Rogersc0542af2014-09-03 16:16:56 -070068 template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080070 if (id == 0) {
Ian Rogersc0542af2014-09-03 16:16:56 -070071 *error = JDWP::ERR_NONE;
72 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -080073 }
Ian Rogersc0542af2014-09-03 16:16:56 -070074 return down_cast<T>(InternalGet(id, error));
Elliott Hughes64f574f2013-02-20 14:57:12 -080075 }
76
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070077 bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78 return Contains(o, nullptr);
79 }
Elliott Hughes64f574f2013-02-20 14:57:12 -080080
Elliott Hughes0f827162013-02-26 12:12:58 -080081 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080082
Sebastien Hertz4537c412014-08-28 14:41:50 +020083 void DisableCollection(JDWP::ObjectId id)
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080085
Sebastien Hertz4537c412014-08-28 14:41:50 +020086 void EnableCollection(JDWP::ObjectId id)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
88
89 bool IsCollected(JDWP::ObjectId id)
90 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080091
92 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
93 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
Elliott Hughes09201632013-04-15 15:50:07 -070095 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -070096 // Avoid using this and use standard Get when possible.
97 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
Elliott Hughes64f574f2013-02-20 14:57:12 -080099 private:
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700100 JDWP::ObjectId InternalAdd(mirror::Object* o)
Sebastien Hertz4537c412014-08-28 14:41:50 +0200101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
102 LOCKS_EXCLUDED(lock_, Locks::thread_list_lock_);
103
Ian Rogersc0542af2014-09-03 16:16:56 -0700104 mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
Sebastien Hertz4537c412014-08-28 14:41:50 +0200105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
106 LOCKS_EXCLUDED(lock_);
107
108 void Demote(ObjectRegistryEntry& entry)
109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
110 EXCLUSIVE_LOCKS_REQUIRED(lock_);
111
112 void Promote(ObjectRegistryEntry& entry)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
114 EXCLUSIVE_LOCKS_REQUIRED(lock_);
115
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700116 bool Contains(mirror::Object* o, ObjectRegistryEntry** out_entry)
Sebastien Hertz4537c412014-08-28 14:41:50 +0200117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
118
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700119 bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
120 ObjectRegistryEntry** out_entry)
121 EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800122
123 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700124 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800125 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
126
127 size_t next_id_ GUARDED_BY(lock_);
128};
129
130} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700131
132#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_