blob: f0314a382f3e9b9fe156fa99c0cab10cf53c65a5 [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
25#include "jdwp/jdwp.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080026#include "safe_map.h"
27
28namespace art {
29
Ian Rogerse63db272014-07-15 15:36:11 -070030namespace mirror {
31 class Object;
32 class Class;
33} // namespace mirror
34
Elliott Hughes64f574f2013-02-20 14:57:12 -080035struct ObjectRegistryEntry {
36 // Is jni_reference a weak global or a regular global reference?
37 jobjectRefType jni_reference_type;
38
39 // The reference itself.
40 jobject jni_reference;
41
42 // A reference count, so we can implement DisposeObject.
43 int32_t reference_count;
44
45 // The corresponding id, so we only need one map lookup in Add.
46 JDWP::ObjectId id;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070047
48 // The identity hash code of the object. This is the same as the key
49 // for object_to_entry_. Store this for DisposeObject().
50 int32_t identity_hash_code;
Elliott Hughes64f574f2013-02-20 14:57:12 -080051};
52std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
53
54// Tracks those objects currently known to the debugger, so we can use consistent ids when
55// referring to them. Normally we keep JNI weak global references to objects, so they can
56// still be garbage collected. The debugger can ask us to retain objects, though, so we can
57// also promote references to regular JNI global references (and demote them back again if
58// the debugger tells us that's okay).
59class ObjectRegistry {
60 public:
61 ObjectRegistry();
62
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070063 JDWP::ObjectId Add(mirror::Object* o)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080065 JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
66
67 template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 if (id == 0) {
69 return NULL;
70 }
71 return reinterpret_cast<T>(InternalGet(id));
72 }
73
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070074 bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
75 return Contains(o, nullptr);
76 }
Elliott Hughes64f574f2013-02-20 14:57:12 -080077
Elliott Hughes0f827162013-02-26 12:12:58 -080078 void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080079
Elliott Hughes0f827162013-02-26 12:12:58 -080080 void DisableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
81 void EnableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080082
Elliott Hughes0f827162013-02-26 12:12:58 -080083 bool IsCollected(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080084
85 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
86 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
87
88 // Returned by Get when passed an invalid object id.
89 static mirror::Object* const kInvalidObject;
90
Elliott Hughes09201632013-04-15 15:50:07 -070091 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -070092 // Avoid using this and use standard Get when possible.
93 jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94
Elliott Hughes64f574f2013-02-20 14:57:12 -080095 private:
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070096 JDWP::ObjectId InternalAdd(mirror::Object* o)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080098 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_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700101 bool Contains(mirror::Object* o, ObjectRegistryEntry** out_entry)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103 bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
104 ObjectRegistryEntry** out_entry)
105 EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800106
107 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700108 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800109 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
110
111 size_t next_id_ GUARDED_BY(lock_);
112};
113
114} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700115
116#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_