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)
6690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_)
6790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
68261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
69070f732bc270f6b9a579838d3418eb13b9cdf8ffSebastien Hertz  JDWP::RefTypeId AddRefType(mirror::Class* c)
7090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_)
7190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
7264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
73261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  template<class T>
74261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::ObjectId Add(Handle<T> obj_h)
7590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_)
7690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
77261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
78261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
7990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_)
8090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
81261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz
82c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers  template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
8390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_) {
8464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (id == 0) {
85c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers      *error = JDWP::ERR_NONE;
86c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers      return nullptr;
8764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    }
88c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers    return down_cast<T>(InternalGet(id, error));
8964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  }
9064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
9190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Clear() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
9264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
934537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void DisableCollection(JDWP::ObjectId id)
9490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
9564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
964537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void EnableCollection(JDWP::ObjectId id)
9790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
984537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
994537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  bool IsCollected(JDWP::ObjectId id)
10090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
10164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
10390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
10464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
1050920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes  // This is needed to get the jobject instead of the Object*.
106449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  // Avoid using this and use standard Get when possible.
10790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  jobject GetJObject(JDWP::ObjectId id) SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
108449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
10964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes private:
110261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  template<class T>
111261bc044a3575512869586593e99e97cd8b1c321Sebastien Hertz  JDWP::ObjectId InternalAdd(Handle<T> obj_h)
11290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_)
11390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
1144537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
115c0542af3e2170143ba40d89136e284997e16bf64Ian Rogers  mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
11690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
1174537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
1184537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void Demote(ObjectRegistryEntry& entry)
11990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
1204537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
1214537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz  void Promote(ObjectRegistryEntry& entry)
12290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(lock_);
1234537c41b9a58c2280b3ad8bcf0130ed11c7a54f6Sebastien Hertz
124b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  bool ContainsLocked(Thread* self, mirror::Object* o, int32_t identity_hash_code,
125b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                      ObjectRegistryEntry** out_entry)
12690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(lock_) SHARED_REQUIRES(Locks::mutator_lock_);
12764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
12864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
129b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
13064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
13164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
13264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  size_t next_id_ GUARDED_BY(lock_);
13364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes};
13464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
13564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}  // namespace art
136fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom
137fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
138