blob: 17490f4db5801676d91f8b097918e5984e505ed0 [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)
Mathieu Chartier90443472015-07-16 20:32:27 -070066 SHARED_REQUIRES(Locks::mutator_lock_)
67 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020068
Sebastien Hertz070f7322014-09-09 12:08:49 +020069 JDWP::RefTypeId AddRefType(mirror::Class* c)
Mathieu Chartier90443472015-07-16 20:32:27 -070070 SHARED_REQUIRES(Locks::mutator_lock_)
71 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080072
Sebastien Hertz261bc042015-04-08 09:36:07 +020073 template<class T>
74 JDWP::ObjectId Add(Handle<T> obj_h)
Mathieu Chartier90443472015-07-16 20:32:27 -070075 SHARED_REQUIRES(Locks::mutator_lock_)
76 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020077
78 JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
Mathieu Chartier90443472015-07-16 20:32:27 -070079 SHARED_REQUIRES(Locks::mutator_lock_)
80 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020081
Ian Rogersc0542af2014-09-03 16:16:56 -070082 template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
Mathieu Chartier90443472015-07-16 20:32:27 -070083 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080084 if (id == 0) {
Ian Rogersc0542af2014-09-03 16:16:56 -070085 *error = JDWP::ERR_NONE;
86 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -080087 }
Ian Rogersc0542af2014-09-03 16:16:56 -070088 return down_cast<T>(InternalGet(id, error));
Elliott Hughes64f574f2013-02-20 14:57:12 -080089 }
90
Mathieu Chartier90443472015-07-16 20:32:27 -070091 void Clear() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080092
Sebastien Hertz4537c412014-08-28 14:41:50 +020093 void DisableCollection(JDWP::ObjectId id)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080095
Sebastien Hertz4537c412014-08-28 14:41:50 +020096 void EnableCollection(JDWP::ObjectId id)
Mathieu Chartier90443472015-07-16 20:32:27 -070097 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +020098
99 bool IsCollected(JDWP::ObjectId id)
Mathieu Chartier90443472015-07-16 20:32:27 -0700100 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800101
102 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800104
Elliott Hughes09201632013-04-15 15:50:07 -0700105 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -0700106 // Avoid using this and use standard Get when possible.
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 jobject GetJObject(JDWP::ObjectId id) SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Jeff Hao449db332013-04-12 18:30:52 -0700108
Elliott Hughes64f574f2013-02-20 14:57:12 -0800109 private:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200110 template<class T>
111 JDWP::ObjectId InternalAdd(Handle<T> obj_h)
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 SHARED_REQUIRES(Locks::mutator_lock_)
113 REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200114
Ian Rogersc0542af2014-09-03 16:16:56 -0700115 mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200117
118 void Demote(ObjectRegistryEntry& entry)
Mathieu Chartier90443472015-07-16 20:32:27 -0700119 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200120
121 void Promote(ObjectRegistryEntry& entry)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200123
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700124 bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
125 ObjectRegistryEntry** out_entry)
Mathieu Chartier90443472015-07-16 20:32:27 -0700126 REQUIRES(lock_) SHARED_REQUIRES(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800127
128 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700129 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800130 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
131
132 size_t next_id_ GUARDED_BY(lock_);
133};
134
135} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700136
137#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_