1 /*
2  * Copyright (C) 2016 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_OPENJDKJVMTI_TI_HEAP_H_
18 #define ART_OPENJDKJVMTI_TI_HEAP_H_
19 
20 #include <unordered_map>
21 
22 #include "jvmti.h"
23 
24 #include "base/locks.h"
25 
26 namespace art {
27 class Thread;
28 template<typename T> class ObjPtr;
29 class HashObjPtr;
30 namespace mirror {
31 class Object;
32 }  // namespace mirror
33 }  // namespace art
34 
35 namespace openjdkjvmti {
36 
37 class EventHandler;
38 class ObjectTagTable;
39 
40 class HeapUtil {
41  public:
HeapUtil(ObjectTagTable * tags)42   explicit HeapUtil(ObjectTagTable* tags) : tags_(tags) {
43   }
44 
45   jvmtiError GetLoadedClasses(jvmtiEnv* env, jint* class_count_ptr, jclass** classes_ptr);
46 
47   jvmtiError IterateOverInstancesOfClass(jvmtiEnv* env,
48                                          jclass klass,
49                                          jvmtiHeapObjectFilter filter,
50                                          jvmtiHeapObjectCallback cb,
51                                          const void* user_data);
52 
53   jvmtiError IterateThroughHeap(jvmtiEnv* env,
54                                 jint heap_filter,
55                                 jclass klass,
56                                 const jvmtiHeapCallbacks* callbacks,
57                                 const void* user_data);
58 
59   jvmtiError FollowReferences(jvmtiEnv* env,
60                               jint heap_filter,
61                               jclass klass,
62                               jobject initial_object,
63                               const jvmtiHeapCallbacks* callbacks,
64                               const void* user_data);
65 
66   static jvmtiError ForceGarbageCollection(jvmtiEnv* env);
67 
GetTags()68   ObjectTagTable* GetTags() {
69     return tags_;
70   }
71 
72   static void Register();
73   static void Unregister();
74 
75  private:
76   ObjectTagTable* tags_;
77 };
78 
79 class HeapExtensions {
80  public:
81   static void Register(EventHandler* eh);
82 
83   static jvmtiError JNICALL GetObjectHeapId(jvmtiEnv* env, jlong tag, jint* heap_id, ...);
84   static jvmtiError JNICALL GetHeapName(jvmtiEnv* env, jint heap_id, char** heap_name, ...);
85 
86   static jvmtiError JNICALL IterateThroughHeapExt(jvmtiEnv* env,
87                                                   jint heap_filter,
88                                                   jclass klass,
89                                                   const jvmtiHeapCallbacks* callbacks,
90                                                   const void* user_data);
91 
92   static jvmtiError JNICALL ChangeArraySize(jvmtiEnv* env, jobject arr, jsize new_size);
93 
94   static void ReplaceReferences(
95       art::Thread* self,
96       const std::unordered_map<art::ObjPtr<art::mirror::Object>,
97                                art::ObjPtr<art::mirror::Object>,
98                                art::HashObjPtr>& refs)
99         REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_);
100 
101   static void ReplaceReference(art::Thread* self,
102                                art::ObjPtr<art::mirror::Object> original,
103                                art::ObjPtr<art::mirror::Object> replacement)
104       REQUIRES(art::Locks::mutator_lock_,
105                art::Roles::uninterruptible_);
106 
107  private:
108   static EventHandler* gEventHandler;
109 };
110 
111 }  // namespace openjdkjvmti
112 
113 #endif  // ART_OPENJDKJVMTI_TI_HEAP_H_
114