blob: 340671da19fb3a5ab74aac80c514652ab9e17e3d [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
Andreas Gampedce591b2016-11-17 11:23:35 -0800188 std::string referree_str = StringPrintf("%" PRId64 "@%" PRId64, *tag_ptr, class_tag);
189
190 lines_.push_back(CreateElem(referrer_str,
191 referree_str,
192 reference_kind,
193 reference_info,
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
Andreas Gampedce591b2016-11-17 11:23:35 -0800202 std::vector<std::string> GetLines() const {
203 std::vector<std::string> ret;
204 for (const std::unique_ptr<Elem>& e : lines_) {
205 ret.push_back(e->Print());
206 }
207 return ret;
208 }
209
210 private:
211 // We need to postpone some printing, as required functions are not callback-safe.
212 class Elem {
213 public:
214 Elem(const std::string& referrer, const std::string& referree, jlong size, jint length)
215 : referrer_(referrer), referree_(referree), size_(size), length_(length) {}
216 virtual ~Elem() {}
217
218 std::string Print() const {
219 return StringPrintf("%s --(%s)--> %s [size=%" PRId64 ", length=%d]",
220 referrer_.c_str(),
221 PrintArrowType().c_str(),
222 referree_.c_str(),
223 size_,
224 length_);
225 }
226
227 protected:
228 virtual std::string PrintArrowType() const = 0;
229
230 private:
231 std::string referrer_;
232 std::string referree_;
233 jlong size_;
234 jint length_;
235 };
236
Andreas Gampe93c30902016-11-18 13:30:30 -0800237 class JNILocalElement : public Elem {
238 public:
239 JNILocalElement(const std::string& referrer,
240 const std::string& referree,
241 jlong size,
242 jint length,
243 const jvmtiHeapReferenceInfo* reference_info)
244 : Elem(referrer, referree, size, length) {
245 memcpy(&info_, reference_info, sizeof(jvmtiHeapReferenceInfo));
246 }
247
248 protected:
249 std::string PrintArrowType() const OVERRIDE {
250 char* name = nullptr;
251 if (info_.jni_local.method != nullptr) {
252 jvmti_env->GetMethodName(info_.jni_local.method, &name, nullptr, nullptr);
253 }
254 std::string ret = StringPrintf("jni-local[id=%" PRId64 ",tag=%" PRId64 ",depth=%d,"
255 "method=%s]",
256 info_.jni_local.thread_id,
257 info_.jni_local.thread_tag,
258 info_.jni_local.depth,
259 name == nullptr ? "<null>" : name);
260 if (name != nullptr) {
261 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
262 }
263
264 return ret;
265 }
266
267 private:
268 const std::string string_;
269 jvmtiHeapReferenceInfo info_;
270 };
271
Andreas Gampedce591b2016-11-17 11:23:35 -0800272 // For simple or unimplemented cases.
273 class StringElement : public Elem {
274 public:
275 StringElement(const std::string& referrer,
276 const std::string& referree,
277 jlong size,
278 jint length,
279 const std::string& string)
280 : Elem(referrer, referree, size, length), string_(string) {}
281
282 protected:
283 std::string PrintArrowType() const OVERRIDE {
284 return string_;
285 }
286
287 private:
288 const std::string string_;
289 };
290
291 static std::unique_ptr<Elem> CreateElem(const std::string& referrer,
292 const std::string& referree,
293 jvmtiHeapReferenceKind reference_kind,
294 const jvmtiHeapReferenceInfo* reference_info,
295 jlong size,
296 jint length) {
297 switch (reference_kind) {
298 case JVMTI_HEAP_REFERENCE_CLASS:
299 return std::unique_ptr<Elem>(new StringElement(referrer,
300 referree,
301 size,
302 length,
303 "class"));
304 case JVMTI_HEAP_REFERENCE_FIELD: {
305 std::string tmp = StringPrintf("field@%d", reference_info->field.index);
306 return std::unique_ptr<Elem>(new StringElement(referrer,
307 referree,
308 size,
309 length,
310 tmp));
311 }
312 case JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT: {
313 std::string tmp = StringPrintf("array-element@%d", reference_info->array.index);
314 return std::unique_ptr<Elem>(new StringElement(referrer,
315 referree,
316 size,
317 length,
318 tmp));
319 }
320 case JVMTI_HEAP_REFERENCE_CLASS_LOADER:
321 return std::unique_ptr<Elem>(new StringElement(referrer,
322 referree,
323 size,
324 length,
325 "classloader"));
326 case JVMTI_HEAP_REFERENCE_SIGNERS:
327 return std::unique_ptr<Elem>(new StringElement(referrer,
328 referree,
329 size,
330 length,
331 "signers"));
332 case JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN:
333 return std::unique_ptr<Elem>(new StringElement(referrer,
334 referree,
335 size,
336 length,
337 "protection-domain"));
338 case JVMTI_HEAP_REFERENCE_INTERFACE:
339 return std::unique_ptr<Elem>(new StringElement(referrer,
340 referree,
341 size,
342 length,
343 "interface"));
344 case JVMTI_HEAP_REFERENCE_STATIC_FIELD: {
345 std::string tmp = StringPrintf("array-element@%d", reference_info->array.index);
346 return std::unique_ptr<Elem>(new StringElement(referrer,
347 referree,
348 size,
349 length,
350 tmp));;
351 }
352 case JVMTI_HEAP_REFERENCE_CONSTANT_POOL:
353 return std::unique_ptr<Elem>(new StringElement(referrer,
354 referree,
355 size,
356 length,
357 "constant-pool"));
358 case JVMTI_HEAP_REFERENCE_SUPERCLASS:
359 return std::unique_ptr<Elem>(new StringElement(referrer,
360 referree,
361 size,
362 length,
363 "superclass"));
364 case JVMTI_HEAP_REFERENCE_JNI_GLOBAL:
365 return std::unique_ptr<Elem>(new StringElement(referrer,
366 referree,
367 size,
368 length,
369 "jni-global"));
370 case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS:
371 return std::unique_ptr<Elem>(new StringElement(referrer,
372 referree,
373 size,
374 length,
375 "system-class"));
376 case JVMTI_HEAP_REFERENCE_MONITOR:
377 return std::unique_ptr<Elem>(new StringElement(referrer,
378 referree,
379 size,
380 length,
381 "monitor"));
382 case JVMTI_HEAP_REFERENCE_STACK_LOCAL:
383 return std::unique_ptr<Elem>(new StringElement(referrer,
384 referree,
385 size,
386 length,
387 "stack-local"));
388 case JVMTI_HEAP_REFERENCE_JNI_LOCAL:
Andreas Gampe93c30902016-11-18 13:30:30 -0800389 return std::unique_ptr<Elem>(new JNILocalElement(referrer,
390 referree,
391 size,
392 length,
393 reference_info));
Andreas Gampedce591b2016-11-17 11:23:35 -0800394 case JVMTI_HEAP_REFERENCE_THREAD:
395 return std::unique_ptr<Elem>(new StringElement(referrer,
396 referree,
397 size,
398 length,
399 "thread"));
400 case JVMTI_HEAP_REFERENCE_OTHER:
401 return std::unique_ptr<Elem>(new StringElement(referrer,
402 referree,
403 size,
404 length,
405 "other"));
406 }
407 LOG(FATAL) << "Unknown kind";
408 UNREACHABLE();
409 }
410
Andreas Gampe216090d2016-11-15 12:33:18 -0800411 static void DumpStacks() NO_THREAD_SAFETY_ANALYSIS {
412 auto dump_function = [](art::Thread* t, void* data ATTRIBUTE_UNUSED) {
413 std::string name;
414 t->GetThreadName(name);
415 LOG(ERROR) << name;
416 art::DumpNativeStack(LOG_STREAM(ERROR), t->GetTid());
417 };
418 art::Runtime::Current()->GetThreadList()->ForEach(dump_function, nullptr);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700419 }
420
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700421 jint counter_;
422 const jint stop_after_;
423 const jint follow_set_;
Andreas Gampedce591b2016-11-17 11:23:35 -0800424
425 std::vector<std::unique_ptr<Elem>> lines_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700426 };
427
Andreas Gampe8df06922016-11-16 10:58:11 -0800428 jit::ScopedJitSuspend sjs; // Wait to avoid JIT influence (e.g., JNI globals).
429
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700430 // If jniRef isn't null, add a local and a global ref.
431 ScopedLocalRef<jobject> jni_local_ref(env, nullptr);
432 jobject jni_global_ref = nullptr;
433 if (jniRef != nullptr) {
434 jni_local_ref.reset(env->NewLocalRef(jniRef));
435 jni_global_ref = env->NewGlobalRef(jniRef);
436 }
437
438 PrintIterationConfig config(stop_after, follow_set);
439 Run(heap_filter, klass_filter, initial_object, &config);
440
Andreas Gampedce591b2016-11-17 11:23:35 -0800441 std::vector<std::string> lines = config.GetLines();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700442 jobjectArray ret = CreateObjectArray(env,
443 static_cast<jint>(lines.size()),
444 "java/lang/String",
445 [&](jint i) {
446 return env->NewStringUTF(lines[i].c_str());
447 });
448
449 if (jni_global_ref != nullptr) {
450 env->DeleteGlobalRef(jni_global_ref);
451 }
452
453 return ret;
454}
455
Andreas Gampe8da6d032016-10-31 19:31:03 -0700456// Don't do anything
457jint OnLoad(JavaVM* vm,
458 char* options ATTRIBUTE_UNUSED,
459 void* reserved ATTRIBUTE_UNUSED) {
460 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
461 printf("Unable to get jvmti env!\n");
462 return 1;
463 }
Alex Lighte6574242016-08-17 09:56:24 -0700464 SetAllCapabilities(jvmti_env);
Andreas Gampe8da6d032016-10-31 19:31:03 -0700465 return 0;
466}
467
Andreas Gampe8da6d032016-10-31 19:31:03 -0700468} // namespace Test913Heaps
469} // namespace art