object_registry.h revision 261bc044a3575512869586593e99e97cd8b1c321
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
25c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers#include "base/casts.h"
26261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz#include "handle.h"
2764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/jdwp.h"
2864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "safe_map.h"
2964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
3064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesnamespace art {
3164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
32e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogersnamespace mirror {
33e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class Object;
34e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers  class Class;
35e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers}  // namespace mirror
36e63db27db913f1a88e2095a1ee8239b2bb9124e8Ian Rogers
3764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstruct ObjectRegistryEntry {
3864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // Is jni_reference a weak global or a regular global reference?
3964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  jobjectRefType jni_reference_type;
4064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
4164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // The reference itself.
4264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  jobject jni_reference;
4364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
4464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // A reference count, so we can implement DisposeObject.
4564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  int32_t reference_count;
4664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
4764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // The corresponding id, so we only need one map lookup in Add.
4864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId id;
49b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
50b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // The identity hash code of the object. This is the same as the key
51b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // for object_to_entry_. Store this for DisposeObject().
52b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  int32_t identity_hash_code;
5364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes};
5464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesstd::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs);
5564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
5664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// Tracks those objects currently known to the debugger, so we can use consistent ids when
5764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// referring to them. Normally we keep JNI weak global references to objects, so they can
5864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// still be garbage collected. The debugger can ask us to retain objects, though, so we can
5964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// also promote references to regular JNI global references (and demote them back again if
6064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes// the debugger tells us that's okay).
6164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesclass ObjectRegistry {
6264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes public:
6364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  ObjectRegistry();
6464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
65b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::ObjectId Add(mirror::Object* o)
66692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
67692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_,
68692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz                     Locks::thread_suspend_count_lock_);
69261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
70070f732bc270f6b9a579838d3418eb13b9cdf8ffSebastien Hertz  JDWP::RefTypeId AddRefType(mirror::Class* c)
71692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
72692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_,
73692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz                     Locks::thread_suspend_count_lock_);
7464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
75261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  template<class T>
76261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::ObjectId Add(Handle<T> obj_h)
77261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
78261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_,
79261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz                     Locks::thread_suspend_count_lock_);
80261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
81261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
82261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
83261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_,
84261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz                     Locks::thread_suspend_count_lock_);
85261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
86c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers  template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
87c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (id == 0) {
89c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers      *error = JDWP::ERR_NONE;
90c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers      return nullptr;
9164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    }
92c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers    return down_cast<T>(InternalGet(id, error));
9364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  }
9464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
950f827169742aad6209d830db773a101849c32a83Elliott Hughes  void Clear() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
9664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
974537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void DisableCollection(JDWP::ObjectId id)
984537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
9964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
1004537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void EnableCollection(JDWP::ObjectId id)
1014537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
1024537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
1034537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  bool IsCollected(JDWP::ObjectId id)
1044537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
10564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
10764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
10864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
1090920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes  // This is needed to get the jobject instead of the Object*.
110449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  // Avoid using this and use standard Get when possible.
111449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  jobject GetJObject(JDWP::ObjectId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
11364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes private:
114261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  template<class T>
115261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::ObjectId InternalAdd(Handle<T> obj_h)
1164537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
117692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz      LOCKS_EXCLUDED(lock_,
118692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz                     Locks::thread_list_lock_,
119692063955ae845d8bd9fa2d22a50a1e06513bdcfSebastien Hertz                     Locks::thread_suspend_count_lock_);
1204537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
121c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers  mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
1224537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1234537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      LOCKS_EXCLUDED(lock_);
1244537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
1254537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void Demote(ObjectRegistryEntry& entry)
1264537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1274537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      EXCLUSIVE_LOCKS_REQUIRED(lock_);
1284537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
1294537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void Promote(ObjectRegistryEntry& entry)
1304537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1314537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz      EXCLUSIVE_LOCKS_REQUIRED(lock_);
1324537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
133b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
134b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                      ObjectRegistryEntry** out_entry)
135b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      EXCLUSIVE_LOCKS_REQUIRED(lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
13664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
13764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
138b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
13964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
14064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
14164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  size_t next_id_ GUARDED_BY(lock_);
14264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes};
14364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
14464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}  // namespace art
145fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom
146fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
147