object_registry.h revision b5a9e3d1cc1fd66683e43e365afc8c900e2800c4
164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes/* 264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * you may not use this file except in compliance with the License. 664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * You may obtain a copy of the License at 764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * 1064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * See the License for the specific language governing permissions and 1464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes * limitations under the License. 1564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes */ 1664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 19fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom 2064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include <stdint.h> 2164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 2264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include <map> 2364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 2464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/jdwp.h" 25ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h" 2664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "mirror/class.h" 2764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "mirror/class-inl.h" 2864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "mirror/object-inl.h" 2983c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier#include "object_callbacks.h" 3064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "safe_map.h" 3164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 3264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesnamespace art { 3364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 3464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstruct ObjectRegistryEntry { 3564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // Is jni_reference a weak global or a regular global reference? 3664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes jobjectRefType jni_reference_type; 3764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 3864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // The reference itself. 3964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes jobject jni_reference; 4064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 4164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // A reference count, so we can implement DisposeObject. 4264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes int32_t reference_count; 4364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 4464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // The corresponding id, so we only need one map lookup in Add. 4564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JDWP::ObjectId id; 46b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi 47b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi // The identity hash code of the object. This is the same as the key 48b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi // for object_to_entry_. Store this for DisposeObject(). 49b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi int32_t identity_hash_code; 5064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}; 5164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstd::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs); 5264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 5364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// Tracks those objects currently known to the debugger, so we can use consistent ids when 5464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// referring to them. Normally we keep JNI weak global references to objects, so they can 5564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// still be garbage collected. The debugger can ask us to retain objects, though, so we can 5664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// also promote references to regular JNI global references (and demote them back again if 5764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// the debugger tells us that's okay). 5864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesclass ObjectRegistry { 5964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes public: 6064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes ObjectRegistry(); 6164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 62b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi JDWP::ObjectId Add(mirror::Object* o) 63b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_); 6464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes JDWP::RefTypeId AddRefType(mirror::Class* c) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 6564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 6664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 6764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes if (id == 0) { 6864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return NULL; 6964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 7064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes return reinterpret_cast<T>(InternalGet(id)); 7164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes } 7264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 73b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi bool Contains(mirror::Object* o) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 74b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi return Contains(o, nullptr); 75b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi } 7664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 770f827169742aad6209d830db773a101849c32a83Elliott Hughes void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 7864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 790f827169742aad6209d830db773a101849c32a83Elliott Hughes void DisableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 800f827169742aad6209d830db773a101849c32a83Elliott Hughes void EnableCollection(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 8164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 820f827169742aad6209d830db773a101849c32a83Elliott Hughes bool IsCollected(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 8364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 8464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes void DisposeObject(JDWP::ObjectId id, uint32_t reference_count) 8564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 8664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 8764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes // Returned by Get when passed an invalid object id. 8864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes static mirror::Object* const kInvalidObject; 8964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 900920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes // This is needed to get the jobject instead of the Object*. 91449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao // Avoid using this and use standard Get when possible. 92449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 93449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao 9464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes private: 95b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi JDWP::ObjectId InternalAdd(mirror::Object* o) 96b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_); 9764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes mirror::Object* InternalGet(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 980f827169742aad6209d830db773a101849c32a83Elliott Hughes void Demote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_); 990f827169742aad6209d830db773a101849c32a83Elliott Hughes void Promote(ObjectRegistryEntry& entry) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, lock_); 100b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi bool Contains(mirror::Object* o, ObjectRegistryEntry** out_entry) 101b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 102b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code, 103b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi ObjectRegistryEntry** out_entry) 104b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 10564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 10664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 107b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_); 10864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_); 10964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 11064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes size_t next_id_ GUARDED_BY(lock_); 11164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}; 11264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes 11364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes} // namespace art 114fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom 115fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_ 116