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
20e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers#include <jni.h>
2164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include <stdint.h>
2264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
2364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include <map>
2464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
2564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/jdwp.h"
2664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "safe_map.h"
2764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
2864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesnamespace art {
2964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
30e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace mirror {
31e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class Object;
32e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class Class;
33e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace mirror
34e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
3564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstruct ObjectRegistryEntry {
3664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // Is jni_reference a weak global or a regular global reference?
3764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  jobjectRefType jni_reference_type;
3864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
3964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // The reference itself.
4064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  jobject jni_reference;
4164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
4264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // A reference count, so we can implement DisposeObject.
4364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  int32_t reference_count;
4464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
4564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // The corresponding id, so we only need one map lookup in Add.
4664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId id;
47b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
48b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // The identity hash code of the object. This is the same as the key
49b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // for object_to_entry_. Store this for DisposeObject().
50b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  int32_t identity_hash_code;
5164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes};
5264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstd::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
5364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
5464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// Tracks those objects currently known to the debugger, so we can use consistent ids when
5564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// referring to them. Normally we keep JNI weak global references to objects, so they can
5664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// still be garbage collected. The debugger can ask us to retain objects, though, so we can
5764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// also promote references to regular JNI global references (and demote them back again if
5864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// the debugger tells us that's okay).
5964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesclass ObjectRegistry {
6064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes public:
6164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  ObjectRegistry();
6264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
63b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::ObjectId Add(mirror::Object* o)
64b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_);
65d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz  JDWP::RefTypeId AddRefType(mirror::Class* c)
66d35776413901a6a9d478e06dc354ea4f7d962e04Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(Locks::thread_list_lock_);
6764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
6864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  template<typename T> T Get(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
6964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (id == 0) {
7064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      return NULL;
7164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    }
7264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    return reinterpret_cast<T>(InternalGet(id));
7364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  }
7464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
750f827169742aad6209d830db773a101849c32a83Elliott Hughes  void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
7664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
7795795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  void DisableCollection(JDWP::ObjectId id)
7895795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
7964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
8095795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  void EnableCollection(JDWP::ObjectId id)
8195795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
8295795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz
8395795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  bool IsCollected(JDWP::ObjectId id)
8495795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
8564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
8664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
8764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
8864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
8964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // Returned by Get when passed an invalid object id.
9064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  static mirror::Object* const kInvalidObject;
9164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
920920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes  // This is needed to get the jobject instead of the Object*.
93449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  // Avoid using this and use standard Get when possible.
94449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
9664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes private:
97b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::ObjectId InternalAdd(mirror::Object* o)
9895795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
9995795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      LOCKS_EXCLUDED(lock_, Locks::thread_list_lock_);
10095795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz
10195795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  mirror::Object* InternalGet(JDWP::ObjectId id)
10295795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
10395795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      LOCKS_EXCLUDED(lock_);
10495795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz
10595795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  void Demote(ObjectRegistryEntry& entry)
10695795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
10795795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      EXCLUSIVE_LOCKS_REQUIRED(lock_);
10895795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz
10995795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz  void Promote(ObjectRegistryEntry& entry)
11095795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
11195795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz      EXCLUSIVE_LOCKS_REQUIRED(lock_);
11295795e286145a4aece5c4a095fa2e7e88ee2115aSebastien Hertz
113b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
114b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                      ObjectRegistryEntry** out_entry)
115b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
11664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
11764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
118b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
11964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
12064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
12164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  size_t next_id_ GUARDED_BY(lock_);
12264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes};
12364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
12464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}  // namespace art
125fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom
126fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
127