blob: 4c149cdac7a1b7a580b543d6bdcbd1c9cc50bfd7 [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"
Sebastien Hertz261bc042015-04-08 09:36:07 +020026#include "handle.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080027#include "jdwp/jdwp.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080028#include "safe_map.h"
29
30namespace art {
31
Ian Rogerse63db272014-07-15 15:36:11 -070032namespace mirror {
33 class Object;
34 class Class;
35} // namespace mirror
36
Elliott Hughes64f574f2013-02-20 14:57:12 -080037struct ObjectRegistryEntry {
38 // Is jni_reference a weak global or a regular global reference?
39 jobjectRefType jni_reference_type;
40
41 // The reference itself.
42 jobject jni_reference;
43
44 // A reference count, so we can implement DisposeObject.
45 int32_t reference_count;
46
47 // The corresponding id, so we only need one map lookup in Add.
48 JDWP::ObjectId id;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070049
50 // The identity hash code of the object. This is the same as the key
51 // for object_to_entry_. Store this for DisposeObject().
52 int32_t identity_hash_code;
Elliott Hughes64f574f2013-02-20 14:57:12 -080053};
54std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
55
56// Tracks those objects currently known to the debugger, so we can use consistent ids when
57// referring to them. Normally we keep JNI weak global references to objects, so they can
58// still be garbage collected. The debugger can ask us to retain objects, though, so we can
59// also promote references to regular JNI global references (and demote them back again if
60// the debugger tells us that's okay).
61class ObjectRegistry {
62 public:
63 ObjectRegistry();
64
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070065 JDWP::ObjectId Add(mirror::Object* o)
Sebastien Hertz69206392015-04-07 15:54:25 +020066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
67 LOCKS_EXCLUDED(Locks::thread_list_lock_,
68 Locks::thread_suspend_count_lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020069
Sebastien Hertz070f7322014-09-09 12:08:49 +020070 JDWP::RefTypeId AddRefType(mirror::Class* c)
Sebastien Hertz69206392015-04-07 15:54:25 +020071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
72 LOCKS_EXCLUDED(Locks::thread_list_lock_,
73 Locks::thread_suspend_count_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080074
Sebastien Hertz261bc042015-04-08 09:36:07 +020075 template<class T>
76 JDWP::ObjectId Add(Handle<T> obj_h)
77 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
78 LOCKS_EXCLUDED(Locks::thread_list_lock_,
79 Locks::thread_suspend_count_lock_);
80
81 JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
83 LOCKS_EXCLUDED(Locks::thread_list_lock_,
84 Locks::thread_suspend_count_lock_);
85
Ian Rogersc0542af2014-09-03 16:16:56 -070086 template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080088 if (id == 0) {
Ian Rogersc0542af2014-09-03 16:16:56 -070089 *error = JDWP::ERR_NONE;
90 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -080091 }
Ian Rogersc0542af2014-09-03 16:16:56 -070092 return down_cast<T>(InternalGet(id, error));
Elliott Hughes64f574f2013-02-20 14:57:12 -080093 }
94
Elliott Hughes0f827162013-02-26 12:12:58 -080095 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080096
Sebastien Hertz4537c412014-08-28 14:41:50 +020097 void DisableCollection(JDWP::ObjectId id)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080099
Sebastien Hertz4537c412014-08-28 14:41:50 +0200100 void EnableCollection(JDWP::ObjectId id)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
102
103 bool IsCollected(JDWP::ObjectId id)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800105
106 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
Elliott Hughes09201632013-04-15 15:50:07 -0700109 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -0700110 // Avoid using this and use standard Get when possible.
111 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
Elliott Hughes64f574f2013-02-20 14:57:12 -0800113 private:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200114 template<class T>
115 JDWP::ObjectId InternalAdd(Handle<T> obj_h)
Sebastien Hertz4537c412014-08-28 14:41:50 +0200116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Sebastien Hertz69206392015-04-07 15:54:25 +0200117 LOCKS_EXCLUDED(lock_,
118 Locks::thread_list_lock_,
119 Locks::thread_suspend_count_lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200120
Ian Rogersc0542af2014-09-03 16:16:56 -0700121 mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
Sebastien Hertz4537c412014-08-28 14:41:50 +0200122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
123 LOCKS_EXCLUDED(lock_);
124
125 void Demote(ObjectRegistryEntry& entry)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
127 EXCLUSIVE_LOCKS_REQUIRED(lock_);
128
129 void Promote(ObjectRegistryEntry& entry)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
131 EXCLUSIVE_LOCKS_REQUIRED(lock_);
132
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700133 bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
134 ObjectRegistryEntry** out_entry)
135 EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800136
137 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700138 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800139 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
140
141 size_t next_id_ GUARDED_BY(lock_);
142};
143
144} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700145
146#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_