Home | History | Annotate | Download | only in jdwp
      1 /*
      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 
     17 #ifndef ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
     18 #define ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
     19 
     20 #include <jni.h>
     21 #include <stdint.h>
     22 
     23 #include <map>
     24 
     25 #include "base/casts.h"
     26 #include "handle.h"
     27 #include "jdwp/jdwp.h"
     28 #include "obj_ptr.h"
     29 #include "safe_map.h"
     30 
     31 namespace art {
     32 
     33 namespace mirror {
     34   class Object;
     35   class Class;
     36 }  // namespace mirror
     37 
     38 struct 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;
     50 
     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;
     54 };
     55 std::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).
     62 class ObjectRegistry {
     63  public:
     64   ObjectRegistry();
     65   ~ObjectRegistry();
     66 
     67   JDWP::ObjectId Add(ObjPtr<mirror::Object> o)
     68       REQUIRES_SHARED(Locks::mutator_lock_)
     69       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
     70 
     71   JDWP::RefTypeId AddRefType(ObjPtr<mirror::Class> c)
     72       REQUIRES_SHARED(Locks::mutator_lock_)
     73       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
     74 
     75   template<class T>
     76   JDWP::ObjectId Add(Handle<T> obj_h)
     77       REQUIRES_SHARED(Locks::mutator_lock_)
     78       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
     79 
     80   JDWP::RefTypeId AddRefType(Handle<mirror::Class> c_h)
     81       REQUIRES_SHARED(Locks::mutator_lock_)
     82       REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, !lock_);
     83 
     84   template<typename T> T Get(JDWP::ObjectId id, JDWP::JdwpError* error)
     85       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_) {
     86     if (id == 0) {
     87       *error = JDWP::ERR_NONE;
     88       return nullptr;
     89     }
     90     return down_cast<T>(InternalGet(id, error));
     91   }
     92 
     93   void Clear() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
     94 
     95   void DisableCollection(JDWP::ObjectId id)
     96       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
     97 
     98   void EnableCollection(JDWP::ObjectId id)
     99       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
    100 
    101   bool IsCollected(JDWP::ObjectId id)
    102       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
    103 
    104   void DisposeObject(JDWP::ObjectId id, uint32_t reference_count)
    105       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
    106 
    107   // This is needed to get the jobject instead of the Object*.
    108   // Avoid using this and use standard Get when possible.
    109   jobject GetJObject(JDWP::ObjectId id) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
    110 
    111  private:
    112   template<class T>
    113   JDWP::ObjectId InternalAdd(Handle<T> obj_h)
    114       REQUIRES_SHARED(Locks::mutator_lock_)
    115       REQUIRES(!lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_);
    116 
    117   mirror::Object* InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error)
    118       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!lock_);
    119 
    120   void Demote(ObjectRegistryEntry& entry)
    121       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
    122 
    123   void Promote(ObjectRegistryEntry& entry)
    124       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(lock_);
    125 
    126   bool ContainsLocked(Thread* self,
    127                       ObjPtr<mirror::Object> o,
    128                       int32_t identity_hash_code,
    129                       ObjectRegistryEntry** out_entry)
    130       REQUIRES(lock_) REQUIRES_SHARED(Locks::mutator_lock_);
    131 
    132   Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
    133   std::multimap<int32_t, ObjectRegistryEntry*> object_to_entry_ GUARDED_BY(lock_);
    134   SafeMap<JDWP::ObjectId, ObjectRegistryEntry*> id_to_entry_ GUARDED_BY(lock_);
    135 
    136   size_t next_id_ GUARDED_BY(lock_);
    137 };
    138 
    139 }  // namespace art
    140 
    141 #endif  // ART_RUNTIME_JDWP_OBJECT_REGISTRY_H_
    142