blob: 1728a7317822ef95ed30cb9b5ed050fa0ec6fda7 [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"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "base/safe_map.h"
Sebastien Hertz261bc042015-04-08 09:36:07 +020027#include "handle.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080028#include "jdwp/jdwp.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070029#include "obj_ptr.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080030
31namespace art {
32
Ian Rogerse63db272014-07-15 15:36:11 -070033namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080034class Object;
35class Class;
Ian Rogerse63db272014-07-15 15:36:11 -070036} // 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();
Hiroshi Yamauchi8a433242017-03-07 14:39:22 -080065 ~ObjectRegistry();
Elliott Hughes64f574f2013-02-20 14:57:12 -080066
Mathieu Chartier3398c782016-09-30 10:27:43 -070067 JDWP::ObjectId Add(ObjPtr<mirror::Object> o)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070069 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020070
Mathieu Chartier3398c782016-09-30 10:27:43 -070071 JDWP::RefTypeId AddRefType(ObjPtr<mirror::Class> c)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070073 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !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)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070078 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020079
80 JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070082 REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
Sebastien Hertz261bc042015-04-08 09:36:07 +020083
Ian Rogersc0542af2014-09-03 16:16:56 -070084 template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080086 if (id == 0) {
Ian Rogersc0542af2014-09-03 16:16:56 -070087 *error = JDWP::ERR_NONE;
88 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -080089 }
Ian Rogersc0542af2014-09-03 16:16:56 -070090 return down_cast<T>(InternalGet(id, error));
Elliott Hughes64f574f2013-02-20 14:57:12 -080091 }
92
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 void Clear() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080094
Sebastien Hertz4537c412014-08-28 14:41:50 +020095 void DisableCollection(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080097
Sebastien Hertz4537c412014-08-28 14:41:50 +020098 void EnableCollection(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070099 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200100
101 bool IsCollected(JDWP::ObjectId id)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800103
104 void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700105 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800106
Elliott Hughes09201632013-04-15 15:50:07 -0700107 // This is needed to get the jobject instead of the Object*.
Jeff Hao449db332013-04-12 18:30:52 -0700108 // Avoid using this and use standard Get when possible.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 jobject GetJObject(JDWP::ObjectId id) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Jeff Hao449db332013-04-12 18:30:52 -0700110
Elliott Hughes64f574f2013-02-20 14:57:12 -0800111 private:
Sebastien Hertz261bc042015-04-08 09:36:07 +0200112 template<class T>
113 JDWP::ObjectId InternalAdd(Handle<T> obj_h)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200116
Ian Rogersc0542af2014-09-03 16:16:56 -0700117 mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200119
120 void Demote(ObjectRegistryEntry& entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200122
123 void Promote(ObjectRegistryEntry& entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
Sebastien Hertz4537c412014-08-28 14:41:50 +0200125
Mathieu Chartier3398c782016-09-30 10:27:43 -0700126 bool ContainsLocked(Thread* self,
127 ObjPtr<mirror::Object> o,
128 int32_t identity_hash_code,
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700129 ObjectRegistryEntry** out_entry)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 REQUIRES(lock_) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800131
132 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700133 std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800134 SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
135
136 size_t next_id_ GUARDED_BY(lock_);
137};
138
139} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700140
141#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_