blob: 0c627d6af844671488ee315c9528da35353042f8 [file] [log] [blame]
Andreas Gampe8da6d032016-10-31 19:31:03 -07001/*
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#include "heaps.h"
18
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070019#include <inttypes.h>
Andreas Gampe8da6d032016-10-31 19:31:03 -070020#include <stdio.h>
21#include <string.h>
22
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070023#include <vector>
24
25#include "base/logging.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070026#include "base/macros.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070027#include "base/stringprintf.h"
Andreas Gampe5d139fc2016-11-09 22:54:25 -080028#include "jit/jit.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070029#include "jni.h"
Andreas Gampe216090d2016-11-15 12:33:18 -080030#include "native_stack_dump.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070031#include "openjdkjvmti/jvmti.h"
Andreas Gampe5d139fc2016-11-09 22:54:25 -080032#include "runtime.h"
33#include "thread-inl.h"
Andreas Gampe216090d2016-11-15 12:33:18 -080034#include "thread_list.h"
Alex Lighte6574242016-08-17 09:56:24 -070035
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070036#include "ti-agent/common_helper.h"
Andreas Gampe8da6d032016-10-31 19:31:03 -070037#include "ti-agent/common_load.h"
38
39namespace art {
40namespace Test913Heaps {
41
42extern "C" JNIEXPORT void JNICALL Java_Main_forceGarbageCollection(JNIEnv* env ATTRIBUTE_UNUSED,
43 jclass klass ATTRIBUTE_UNUSED) {
44 jvmtiError ret = jvmti_env->ForceGarbageCollection();
45 if (ret != JVMTI_ERROR_NONE) {
46 char* err;
47 jvmti_env->GetErrorName(ret, &err);
48 printf("Error forcing a garbage collection: %s\n", err);
49 }
50}
51
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070052class IterationConfig {
53 public:
54 IterationConfig() {}
55 virtual ~IterationConfig() {}
56
57 virtual jint Handle(jvmtiHeapReferenceKind reference_kind,
58 const jvmtiHeapReferenceInfo* reference_info,
59 jlong class_tag,
60 jlong referrer_class_tag,
61 jlong size,
62 jlong* tag_ptr,
63 jlong* referrer_tag_ptr,
64 jint length,
65 void* user_data) = 0;
66};
67
68static jint JNICALL HeapReferenceCallback(jvmtiHeapReferenceKind reference_kind,
69 const jvmtiHeapReferenceInfo* reference_info,
70 jlong class_tag,
71 jlong referrer_class_tag,
72 jlong size,
73 jlong* tag_ptr,
74 jlong* referrer_tag_ptr,
75 jint length,
76 void* user_data) {
77 IterationConfig* config = reinterpret_cast<IterationConfig*>(user_data);
78 return config->Handle(reference_kind,
79 reference_info,
80 class_tag,
81 referrer_class_tag,
82 size,
83 tag_ptr,
84 referrer_tag_ptr,
85 length,
86 user_data);
87}
88
89static bool Run(jint heap_filter,
90 jclass klass_filter,
91 jobject initial_object,
92 IterationConfig* config) {
93 jvmtiHeapCallbacks callbacks;
94 memset(&callbacks, 0, sizeof(jvmtiHeapCallbacks));
95 callbacks.heap_reference_callback = HeapReferenceCallback;
96
97 jvmtiError ret = jvmti_env->FollowReferences(heap_filter,
98 klass_filter,
99 initial_object,
100 &callbacks,
101 config);
102 if (ret != JVMTI_ERROR_NONE) {
103 char* err;
104 jvmti_env->GetErrorName(ret, &err);
105 printf("Failure running FollowReferences: %s\n", err);
106 return false;
107 }
108 return true;
109}
110
111extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_followReferences(JNIEnv* env,
112 jclass klass ATTRIBUTE_UNUSED,
113 jint heap_filter,
114 jclass klass_filter,
115 jobject initial_object,
116 jint stop_after,
117 jint follow_set,
118 jobject jniRef) {
119 class PrintIterationConfig FINAL : public IterationConfig {
120 public:
121 PrintIterationConfig(jint _stop_after, jint _follow_set)
122 : counter_(0),
123 stop_after_(_stop_after),
124 follow_set_(_follow_set) {
125 }
126
127 jint Handle(jvmtiHeapReferenceKind reference_kind,
128 const jvmtiHeapReferenceInfo* reference_info,
129 jlong class_tag,
130 jlong referrer_class_tag,
131 jlong size,
132 jlong* tag_ptr,
133 jlong* referrer_tag_ptr,
134 jint length,
135 void* user_data ATTRIBUTE_UNUSED) OVERRIDE {
136 jlong tag = *tag_ptr;
137 // Only check tagged objects.
138 if (tag == 0) {
139 return JVMTI_VISIT_OBJECTS;
140 }
141
142 Print(reference_kind,
143 reference_info,
144 class_tag,
145 referrer_class_tag,
146 size,
147 tag_ptr,
148 referrer_tag_ptr,
149 length);
150
151 counter_++;
152 if (counter_ == stop_after_) {
153 return JVMTI_VISIT_ABORT;
154 }
155
156 if (tag > 0 && tag < 32) {
157 bool should_visit_references = (follow_set_ & (1 << static_cast<int32_t>(tag))) != 0;
158 return should_visit_references ? JVMTI_VISIT_OBJECTS : 0;
159 }
160
161 return JVMTI_VISIT_OBJECTS;
162 }
163
164 void Print(jvmtiHeapReferenceKind reference_kind,
165 const jvmtiHeapReferenceInfo* reference_info,
166 jlong class_tag,
167 jlong referrer_class_tag,
168 jlong size,
169 jlong* tag_ptr,
170 jlong* referrer_tag_ptr,
171 jint length) {
172 std::string referrer_str;
173 if (referrer_tag_ptr == nullptr) {
174 referrer_str = "root@root";
175 } else {
176 referrer_str = StringPrintf("%" PRId64 "@%" PRId64, *referrer_tag_ptr, referrer_class_tag);
177 }
178
179 jlong adapted_size = size;
180 if (*tag_ptr >= 1000) {
181 // This is a class or interface, the size of which will be dependent on the architecture.
182 // Do not print the size, but detect known values and "normalize" for the golden file.
183 if ((sizeof(void*) == 4 && size == 180) || (sizeof(void*) == 8 && size == 232)) {
184 adapted_size = 123;
185 }
186 }
187
188 lines_.push_back(
189 StringPrintf("%s --(%s)--> %" PRId64 "@%" PRId64 " [size=%" PRId64 ", length=%d]",
190 referrer_str.c_str(),
191 GetReferenceTypeStr(reference_kind, reference_info).c_str(),
192 *tag_ptr,
193 class_tag,
194 adapted_size,
195 length));
Andreas Gampe216090d2016-11-15 12:33:18 -0800196
197 if (reference_kind == JVMTI_HEAP_REFERENCE_THREAD && *tag_ptr == 1000) {
198 DumpStacks();
199 }
200 }
201
202 static void DumpStacks() NO_THREAD_SAFETY_ANALYSIS {
203 auto dump_function = [](art::Thread* t, void* data ATTRIBUTE_UNUSED) {
204 std::string name;
205 t->GetThreadName(name);
206 LOG(ERROR) << name;
207 art::DumpNativeStack(LOG_STREAM(ERROR), t->GetTid());
208 };
209 art::Runtime::Current()->GetThreadList()->ForEach(dump_function, nullptr);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700210 }
211
212 static std::string GetReferenceTypeStr(jvmtiHeapReferenceKind reference_kind,
213 const jvmtiHeapReferenceInfo* reference_info) {
214 switch (reference_kind) {
215 case JVMTI_HEAP_REFERENCE_CLASS:
216 return "class";
217 case JVMTI_HEAP_REFERENCE_FIELD:
218 return StringPrintf("field@%d", reference_info->field.index);
219 case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT:
220 return StringPrintf("array-element@%d", reference_info->array.index);
221 case JVMTI_HEAP_REFERENCE_CLASS_LOADER:
222 return "classloader";
223 case JVMTI_HEAP_REFERENCE_SIGNERS:
224 return "signers";
225 case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:
226 return "protection-domain";
227 case JVMTI_HEAP_REFERENCE_INTERFACE:
228 return "interface";
229 case JVMTI_HEAP_REFERENCE_STATIC_FIELD:
230 return StringPrintf("static-field@%d", reference_info->field.index);
231 case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:
232 return "constant-pool";
233 case JVMTI_HEAP_REFERENCE_SUPERCLASS:
234 return "superclass";
235 case JVMTI_HEAP_REFERENCE_JNI_GLOBAL:
236 return "jni-global";
237 case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS:
238 return "system-class";
239 case JVMTI_HEAP_REFERENCE_MONITOR:
240 return "monitor";
241 case JVMTI_HEAP_REFERENCE_STACK_LOCAL:
242 return "stack-local";
243 case JVMTI_HEAP_REFERENCE_JNI_LOCAL:
244 return "jni-local";
245 case JVMTI_HEAP_REFERENCE_THREAD:
246 return "thread";
247 case JVMTI_HEAP_REFERENCE_OTHER:
248 return "other";
249 }
250 return "unknown";
251 }
252
253 const std::vector<std::string>& GetLines() const {
254 return lines_;
255 }
256
257 private:
258 jint counter_;
259 const jint stop_after_;
260 const jint follow_set_;
261 std::vector<std::string> lines_;
262 };
263
264 // If jniRef isn't null, add a local and a global ref.
265 ScopedLocalRef<jobject> jni_local_ref(env, nullptr);
266 jobject jni_global_ref = nullptr;
267 if (jniRef != nullptr) {
268 jni_local_ref.reset(env->NewLocalRef(jniRef));
269 jni_global_ref = env->NewGlobalRef(jniRef);
270 }
271
272 PrintIterationConfig config(stop_after, follow_set);
273 Run(heap_filter, klass_filter, initial_object, &config);
274
275 const std::vector<std::string>& lines = config.GetLines();
276 jobjectArray ret = CreateObjectArray(env,
277 static_cast<jint>(lines.size()),
278 "java/lang/String",
279 [&](jint i) {
280 return env->NewStringUTF(lines[i].c_str());
281 });
282
283 if (jni_global_ref != nullptr) {
284 env->DeleteGlobalRef(jni_global_ref);
285 }
286
287 return ret;
288}
289
Andreas Gampe8da6d032016-10-31 19:31:03 -0700290// Don't do anything
291jint OnLoad(JavaVM* vm,
292 char* options ATTRIBUTE_UNUSED,
293 void* reserved ATTRIBUTE_UNUSED) {
294 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
295 printf("Unable to get jvmti env!\n");
296 return 1;
297 }
Alex Lighte6574242016-08-17 09:56:24 -0700298 SetAllCapabilities(jvmti_env);
Andreas Gampe8da6d032016-10-31 19:31:03 -0700299 return 0;
300}
301
Andreas Gampe5d139fc2016-11-09 22:54:25 -0800302extern "C" JNIEXPORT void JNICALL Java_Main_waitForJitCompilation(JNIEnv*, jclass) {
303 jit::Jit* jit = Runtime::Current()->GetJit();
304 if (jit != nullptr) {
305 jit->WaitForCompilationToFinish(Thread::Current());
306 }
307}
308
Andreas Gampe8da6d032016-10-31 19:31:03 -0700309} // namespace Test913Heaps
310} // namespace art