blob: 9cacc66c3297994ddbe292fa3014c25e2f1b0f00 [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"
Mathieu Chartier3398c782016-09-30 10:27:43 -070028#include "obj_ptr.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080029#include "safe_map.h"
30
31namespace art {
32
Ian Rogerse63db272014-07-15 15:36:11 -070033namespace mirror {
34 class Object;
35 class Class;
36} // namespace mirror
37
Elliott Hughes64f574f2013-02-20 14:57:12 -080038struct ObjectRegistryEntry {
39 // Is jni_reference a weak global or a regular global reference?
40 jobjectRefType jni_reference_type;
41
42 // The reference itself.
43 jobject jni_reference;
44
45 // A reference count, so we can implement DisposeObject.
46 int32_t reference_count;
47
48 // The corresponding id, so we only need one map lookup in Add.
49 JDWP::ObjectId id;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070050
51 // The identity hash code of the object. This is the same as the key
52 // for object_to_entry_. Store this for DisposeObject().
53 int32_t identity_hash_code;
Elliott Hughes64f574f2013-02-20 14:57:12 -080054};
55std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
56
57// Tracks those objects currently known to the debugger, so we can use consistent ids when
58// referring to them. Normally we keep JNI weak global references to objects, so they can
59// still be garbage collected. The debugger can ask us to retain objects, though, so we can
60// also promote references to regular JNI global references (and demote them back again if
61// the debugger tells us that's okay).
62class ObjectRegistry {
63 public:
64 ObjectRegistry();
65
Mathieu Chartier3398c782016-09-30 10:27:43 -070066 JDWP::ObjectId Add(ObjPtr<mirror::Object> o)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070068 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020069
Mathieu Chartier3398c782016-09-30 10:27:43 -070070 JDWP::RefTypeId AddRefType(ObjPtr<mirror::Class> c)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070072 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080073
Sebastien Hertz261bc042015-04-08 09:36:07 +020074 template<class T>
75 JDWP::ObjectId Add(Handle<T> obj_h)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070076 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070077 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020078
79 JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070080 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070081 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020082
Ian Rogersc0542af2014-09-03 16:16:56 -070083 template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080085 if (id == 0) {
Ian Rogersc0542af2014-09-03 16:16:56 -070086 *error = JDWP::ERR_NONE;
87 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -080088 }
Ian Rogersc0542af2014-09-03 16:16:56 -070089 return down_cast<T>(InternalGet(id, error));
Elliott Hughes64f574f2013-02-20 14:57:12 -080090 }
91
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 void Clear() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080093
Sebastien Hertz4537c412014-08-28 14:41:50 +020094 void DisableCollection(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080096
Sebastien Hertz4537c412014-08-28 14:41:50 +020097 void EnableCollection(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070098 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +020099
100 bool IsCollected(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800102
103 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700104 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800105
Elliott Hughes09201632013-04-15 15:50:07 -0700106 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -0700107 // Avoid using this and use standard Get when possible.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700108 jobject GetJObject(JDWP::ObjectId id) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Jeff Hao449db332013-04-12 18:30:52 -0700109
Elliott Hughes64f574f2013-02-20 14:57:12 -0800110 private:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200111 template<class T>
112 JDWP::ObjectId InternalAdd(Handle<T> obj_h)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200115
Ian Rogersc0542af2014-09-03 16:16:56 -0700116 mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200118
119 void Demote(ObjectRegistryEntry& entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200121
122 void Promote(ObjectRegistryEntry& entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700123 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200124
Mathieu Chartier3398c782016-09-30 10:27:43 -0700125 bool ContainsLocked(Thread* self,
126 ObjPtr<mirror::Object> o,
127 int32_t identity_hash_code,
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700128 ObjectRegistryEntry** out_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES(lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800130
131 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700132 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800133 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
134
135 size_t next_id_ GUARDED_BY(lock_);
136};
137
138} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700139
140#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_