blob: 49d9aca46e1f4d969a6f455405752fdf6293334f [file] [log] [blame]
Andreas Gampee54d9922016-10-11 19:55:37 -07001/*
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
Andreas Gampeba8df692016-11-01 10:30:44 -070017#include "ti_heap.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070018
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070019#include "art_field-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070020#include "art_jvmti.h"
21#include "base/macros.h"
22#include "base/mutex.h"
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070023#include "class_linker.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070024#include "gc/heap.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070025#include "gc_root-inl.h"
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070026#include "jni_env_ext.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070027#include "jni_internal.h"
Andreas Gampe9e38a502017-03-06 08:19:26 -080028#include "jvmti_weak_table-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070029#include "mirror/class.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070032#include "object_callbacks.h"
33#include "object_tagging.h"
34#include "obj_ptr-inl.h"
Andreas Gampebecd6ad2017-02-22 19:20:37 -080035#include "primitive.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070036#include "runtime.h"
37#include "scoped_thread_state_change-inl.h"
38#include "thread-inl.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070039#include "thread_list.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070040
41namespace openjdkjvmti {
42
Andreas Gampe3ec8e402017-02-21 15:49:53 -080043namespace {
44
Andreas Gampe9e38a502017-03-06 08:19:26 -080045struct IndexCache {
46 // The number of interface fields implemented by the class. This is a prefix to all assigned
47 // field indices.
48 size_t interface_fields;
49
50 // It would be nice to also cache the following, but it is complicated to wire up into the
51 // generic visit:
52 // The number of fields in interfaces and superclasses. This is the first index assigned to
53 // fields of the class.
54 // size_t superclass_fields;
55};
56using IndexCachingTable = JvmtiWeakTable<IndexCache>;
57
58static IndexCachingTable gIndexCachingTable;
59
Andreas Gampe3ec8e402017-02-21 15:49:53 -080060// Report the contents of a string, if a callback is set.
61jint ReportString(art::ObjPtr<art::mirror::Object> obj,
62 jvmtiEnv* env,
63 ObjectTagTable* tag_table,
64 const jvmtiHeapCallbacks* cb,
65 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
66 if (UNLIKELY(cb->string_primitive_value_callback != nullptr) && obj->IsString()) {
67 art::ObjPtr<art::mirror::String> str = obj->AsString();
68 int32_t string_length = str->GetLength();
Andreas Gampe5f942032017-02-27 19:59:40 -080069 JvmtiUniquePtr<uint16_t[]> data;
Andreas Gampe3ec8e402017-02-21 15:49:53 -080070
Andreas Gampe5f942032017-02-27 19:59:40 -080071 if (string_length > 0) {
72 jvmtiError alloc_error;
73 data = AllocJvmtiUniquePtr<uint16_t[]>(env, string_length, &alloc_error);
74 if (data == nullptr) {
75 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
76 // back? For now just warn.
77 LOG(WARNING) << "Unable to allocate buffer for string reporting! Silently dropping value."
78 << " >" << str->ToModifiedUtf8() << "<";
79 return 0;
Andreas Gampe3ec8e402017-02-21 15:49:53 -080080 }
Andreas Gampe5f942032017-02-27 19:59:40 -080081
82 if (str->IsCompressed()) {
83 uint8_t* compressed_data = str->GetValueCompressed();
84 for (int32_t i = 0; i != string_length; ++i) {
85 data[i] = compressed_data[i];
86 }
87 } else {
88 // Can copy directly.
89 memcpy(data.get(), str->GetValue(), string_length * sizeof(uint16_t));
90 }
Andreas Gampe3ec8e402017-02-21 15:49:53 -080091 }
92
93 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
94 jlong string_tag = tag_table->GetTagOrZero(obj.Ptr());
95 const jlong saved_string_tag = string_tag;
96
97 jint result = cb->string_primitive_value_callback(class_tag,
98 obj->SizeOf(),
99 &string_tag,
100 data.get(),
101 string_length,
102 const_cast<void*>(user_data));
103 if (string_tag != saved_string_tag) {
104 tag_table->Set(obj.Ptr(), string_tag);
105 }
106
107 return result;
108 }
109 return 0;
110}
111
Andreas Gampebecd6ad2017-02-22 19:20:37 -0800112// Report the contents of a primitive array, if a callback is set.
113jint ReportPrimitiveArray(art::ObjPtr<art::mirror::Object> obj,
114 jvmtiEnv* env,
115 ObjectTagTable* tag_table,
116 const jvmtiHeapCallbacks* cb,
117 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
118 if (UNLIKELY(cb->array_primitive_value_callback != nullptr) &&
119 obj->IsArrayInstance() &&
120 !obj->IsObjectArray()) {
121 art::ObjPtr<art::mirror::Array> array = obj->AsArray();
122 int32_t array_length = array->GetLength();
123 size_t component_size = array->GetClass()->GetComponentSize();
124 art::Primitive::Type art_prim_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
125 jvmtiPrimitiveType prim_type =
126 static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
127 DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
128 prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
129 prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
130 prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
131 prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
132 prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
133 prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
134 prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
135
136 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
137 jlong array_tag = tag_table->GetTagOrZero(obj.Ptr());
138 const jlong saved_array_tag = array_tag;
139
140 jint result;
141 if (array_length == 0) {
142 result = cb->array_primitive_value_callback(class_tag,
143 obj->SizeOf(),
144 &array_tag,
145 0,
146 prim_type,
147 nullptr,
148 const_cast<void*>(user_data));
149 } else {
150 jvmtiError alloc_error;
151 JvmtiUniquePtr<char[]> data = AllocJvmtiUniquePtr<char[]>(env,
152 array_length * component_size,
153 &alloc_error);
154 if (data == nullptr) {
155 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
156 // back? For now just warn.
157 LOG(WARNING) << "Unable to allocate buffer for array reporting! Silently dropping value.";
158 return 0;
159 }
160
161 memcpy(data.get(), array->GetRawData(component_size, 0), array_length * component_size);
162
163 result = cb->array_primitive_value_callback(class_tag,
164 obj->SizeOf(),
165 &array_tag,
166 array_length,
167 prim_type,
168 data.get(),
169 const_cast<void*>(user_data));
170 }
171
172 if (array_tag != saved_array_tag) {
173 tag_table->Set(obj.Ptr(), array_tag);
174 }
175
176 return result;
177 }
178 return 0;
179}
180
Andreas Gampee7316932017-02-25 09:15:05 -0800181template <typename UserData>
182bool VisitorFalse(art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
183 art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
184 art::ArtField& field ATTRIBUTE_UNUSED,
185 size_t field_index ATTRIBUTE_UNUSED,
186 UserData* user_data ATTRIBUTE_UNUSED) {
187 return false;
188}
189
Andreas Gampe95114602017-02-28 15:47:44 -0800190template <typename UserData, bool kCallVisitorOnRecursion>
Andreas Gampee7316932017-02-25 09:15:05 -0800191class FieldVisitor {
192 public:
193 // Report the contents of a primitive fields of the given object, if a callback is set.
Andreas Gampe95114602017-02-28 15:47:44 -0800194 template <typename StaticPrimitiveVisitor,
195 typename StaticReferenceVisitor,
196 typename InstancePrimitiveVisitor,
197 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800198 static bool ReportFields(art::ObjPtr<art::mirror::Object> obj,
199 UserData* user_data,
200 StaticPrimitiveVisitor& static_prim_visitor,
201 StaticReferenceVisitor& static_ref_visitor,
202 InstancePrimitiveVisitor& instance_prim_visitor,
203 InstanceReferenceVisitor& instance_ref_visitor)
204 REQUIRES_SHARED(art::Locks::mutator_lock_) {
205 FieldVisitor fv(user_data);
206
207 if (obj->IsClass()) {
208 // When visiting a class, we only visit the static fields of the given class. No field of
209 // superclasses is visited.
210 art::ObjPtr<art::mirror::Class> klass = obj->AsClass();
211 // Only report fields on resolved classes. We need valid field data.
212 if (!klass->IsResolved()) {
213 return false;
214 }
215 return fv.ReportFieldsImpl(nullptr,
216 obj->AsClass(),
217 obj->AsClass()->IsInterface(),
218 static_prim_visitor,
219 static_ref_visitor,
220 instance_prim_visitor,
221 instance_ref_visitor);
222 } else {
223 // See comment above. Just double-checking here, but an instance *should* mean the class was
224 // resolved.
225 DCHECK(obj->GetClass()->IsResolved() || obj->GetClass()->IsErroneousResolved());
226 return fv.ReportFieldsImpl(obj,
227 obj->GetClass(),
228 false,
229 static_prim_visitor,
230 static_ref_visitor,
231 instance_prim_visitor,
232 instance_ref_visitor);
233 }
234 }
235
236 private:
237 explicit FieldVisitor(UserData* user_data) : user_data_(user_data) {}
238
239 // Report the contents of fields of the given object. If obj is null, report the static fields,
240 // otherwise the instance fields.
Andreas Gampe95114602017-02-28 15:47:44 -0800241 template <typename StaticPrimitiveVisitor,
242 typename StaticReferenceVisitor,
243 typename InstancePrimitiveVisitor,
244 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800245 bool ReportFieldsImpl(art::ObjPtr<art::mirror::Object> obj,
246 art::ObjPtr<art::mirror::Class> klass,
247 bool skip_java_lang_object,
248 StaticPrimitiveVisitor& static_prim_visitor,
249 StaticReferenceVisitor& static_ref_visitor,
250 InstancePrimitiveVisitor& instance_prim_visitor,
251 InstanceReferenceVisitor& instance_ref_visitor)
252 REQUIRES_SHARED(art::Locks::mutator_lock_) {
253 // Compute the offset of field indices.
254 size_t interface_field_count = CountInterfaceFields(klass);
255
256 size_t tmp;
257 bool aborted = ReportFieldsRecursive(obj,
258 klass,
259 interface_field_count,
260 skip_java_lang_object,
261 static_prim_visitor,
262 static_ref_visitor,
263 instance_prim_visitor,
264 instance_ref_visitor,
265 &tmp);
266 return aborted;
267 }
268
269 // Visit primitive fields in an object (instance). Return true if the visit was aborted.
Andreas Gampe95114602017-02-28 15:47:44 -0800270 template <typename StaticPrimitiveVisitor,
271 typename StaticReferenceVisitor,
272 typename InstancePrimitiveVisitor,
273 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800274 bool ReportFieldsRecursive(art::ObjPtr<art::mirror::Object> obj,
275 art::ObjPtr<art::mirror::Class> klass,
276 size_t interface_fields,
277 bool skip_java_lang_object,
278 StaticPrimitiveVisitor& static_prim_visitor,
279 StaticReferenceVisitor& static_ref_visitor,
280 InstancePrimitiveVisitor& instance_prim_visitor,
281 InstanceReferenceVisitor& instance_ref_visitor,
282 size_t* field_index_out)
283 REQUIRES_SHARED(art::Locks::mutator_lock_) {
284 DCHECK(klass != nullptr);
285 size_t field_index;
286 if (klass->GetSuperClass() == nullptr) {
287 // j.l.Object. Start with the fields from interfaces.
288 field_index = interface_fields;
289 if (skip_java_lang_object) {
290 *field_index_out = field_index;
291 return false;
292 }
293 } else {
294 // Report superclass fields.
295 if (kCallVisitorOnRecursion) {
296 if (ReportFieldsRecursive(obj,
297 klass->GetSuperClass(),
298 interface_fields,
299 skip_java_lang_object,
300 static_prim_visitor,
301 static_ref_visitor,
302 instance_prim_visitor,
303 instance_ref_visitor,
304 &field_index)) {
305 return true;
306 }
307 } else {
308 // Still call, but with empty visitor. This is required for correct counting.
309 ReportFieldsRecursive(obj,
310 klass->GetSuperClass(),
311 interface_fields,
312 skip_java_lang_object,
313 VisitorFalse<UserData>,
314 VisitorFalse<UserData>,
315 VisitorFalse<UserData>,
316 VisitorFalse<UserData>,
317 &field_index);
318 }
319 }
320
321 // Now visit fields for the current klass.
322
323 for (auto& static_field : klass->GetSFields()) {
324 if (static_field.IsPrimitiveType()) {
325 if (static_prim_visitor(obj,
326 klass,
327 static_field,
328 field_index,
329 user_data_)) {
330 return true;
331 }
332 } else {
333 if (static_ref_visitor(obj,
334 klass,
335 static_field,
336 field_index,
337 user_data_)) {
338 return true;
339 }
340 }
341 field_index++;
342 }
343
344 for (auto& instance_field : klass->GetIFields()) {
345 if (instance_field.IsPrimitiveType()) {
346 if (instance_prim_visitor(obj,
347 klass,
348 instance_field,
349 field_index,
350 user_data_)) {
351 return true;
352 }
353 } else {
354 if (instance_ref_visitor(obj,
355 klass,
356 instance_field,
357 field_index,
358 user_data_)) {
359 return true;
360 }
361 }
362 field_index++;
363 }
364
365 *field_index_out = field_index;
366 return false;
367 }
368
369 // Implements a visit of the implemented interfaces of a given class.
370 template <typename T>
371 struct RecursiveInterfaceVisit {
372 static void VisitStatic(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
373 REQUIRES_SHARED(art::Locks::mutator_lock_) {
374 RecursiveInterfaceVisit rv;
375 rv.Visit(self, klass, visitor);
376 }
377
378 void Visit(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
379 REQUIRES_SHARED(art::Locks::mutator_lock_) {
380 // First visit the parent, to get the order right.
381 // (We do this in preparation for actual visiting of interface fields.)
382 if (klass->GetSuperClass() != nullptr) {
383 Visit(self, klass->GetSuperClass(), visitor);
384 }
385 for (uint32_t i = 0; i != klass->NumDirectInterfaces(); ++i) {
386 art::ObjPtr<art::mirror::Class> inf_klass =
387 art::mirror::Class::GetDirectInterface(self, klass, i);
388 DCHECK(inf_klass != nullptr);
389 VisitInterface(self, inf_klass, visitor);
390 }
391 }
392
393 void VisitInterface(art::Thread* self, art::ObjPtr<art::mirror::Class> inf_klass, T& visitor)
394 REQUIRES_SHARED(art::Locks::mutator_lock_) {
395 auto it = visited_interfaces.find(inf_klass.Ptr());
396 if (it != visited_interfaces.end()) {
397 return;
398 }
399 visited_interfaces.insert(inf_klass.Ptr());
400
401 // Let the visitor know about this one. Note that this order is acceptable, as the ordering
402 // of these fields never matters for known visitors.
403 visitor(inf_klass);
404
405 // Now visit the superinterfaces.
406 for (uint32_t i = 0; i != inf_klass->NumDirectInterfaces(); ++i) {
407 art::ObjPtr<art::mirror::Class> super_inf_klass =
408 art::mirror::Class::GetDirectInterface(self, inf_klass, i);
409 DCHECK(super_inf_klass != nullptr);
410 VisitInterface(self, super_inf_klass, visitor);
411 }
412 }
413
414 std::unordered_set<art::mirror::Class*> visited_interfaces;
415 };
416
417 // Counting interface fields. Note that we cannot use the interface table, as that only contains
418 // "non-marker" interfaces (= interfaces with methods).
419 static size_t CountInterfaceFields(art::ObjPtr<art::mirror::Class> klass)
420 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe9e38a502017-03-06 08:19:26 -0800421 // Do we have a cached value?
422 IndexCache tmp;
423 if (gIndexCachingTable.GetTag(klass.Ptr(), &tmp)) {
424 return tmp.interface_fields;
425 }
426
Andreas Gampee7316932017-02-25 09:15:05 -0800427 size_t count = 0;
428 auto visitor = [&count](art::ObjPtr<art::mirror::Class> inf_klass)
429 REQUIRES_SHARED(art::Locks::mutator_lock_) {
430 DCHECK(inf_klass->IsInterface());
431 DCHECK_EQ(0u, inf_klass->NumInstanceFields());
432 count += inf_klass->NumStaticFields();
433 };
434 RecursiveInterfaceVisit<decltype(visitor)>::VisitStatic(art::Thread::Current(), klass, visitor);
Andreas Gampee7316932017-02-25 09:15:05 -0800435
Andreas Gampe9e38a502017-03-06 08:19:26 -0800436 // Store this into the cache.
437 tmp.interface_fields = count;
438 gIndexCachingTable.Set(klass.Ptr(), tmp);
439
440 return count;
Andreas Gampee7316932017-02-25 09:15:05 -0800441 }
442
443 UserData* user_data_;
444};
445
446// Debug helper. Prints the structure of an object.
447template <bool kStatic, bool kRef>
448struct DumpVisitor {
449 static bool Callback(art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
450 art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
451 art::ArtField& field,
452 size_t field_index,
453 void* user_data ATTRIBUTE_UNUSED)
454 REQUIRES_SHARED(art::Locks::mutator_lock_) {
455 LOG(ERROR) << (kStatic ? "static " : "instance ")
456 << (kRef ? "ref " : "primitive ")
457 << field.PrettyField()
458 << " @ "
459 << field_index;
460 return false;
461 }
462};
463ATTRIBUTE_UNUSED
464void DumpObjectFields(art::ObjPtr<art::mirror::Object> obj)
465 REQUIRES_SHARED(art::Locks::mutator_lock_) {
466 if (obj->IsClass()) {
Andreas Gampe95114602017-02-28 15:47:44 -0800467 FieldVisitor<void, false>:: ReportFields(obj,
468 nullptr,
469 DumpVisitor<true, false>::Callback,
470 DumpVisitor<true, true>::Callback,
471 DumpVisitor<false, false>::Callback,
472 DumpVisitor<false, true>::Callback);
Andreas Gampee7316932017-02-25 09:15:05 -0800473 } else {
Andreas Gampe95114602017-02-28 15:47:44 -0800474 FieldVisitor<void, true>::ReportFields(obj,
475 nullptr,
476 DumpVisitor<true, false>::Callback,
477 DumpVisitor<true, true>::Callback,
478 DumpVisitor<false, false>::Callback,
479 DumpVisitor<false, true>::Callback);
Andreas Gampee7316932017-02-25 09:15:05 -0800480 }
481}
482
483class ReportPrimitiveField {
484 public:
485 static bool Report(art::ObjPtr<art::mirror::Object> obj,
486 ObjectTagTable* tag_table,
487 const jvmtiHeapCallbacks* cb,
488 const void* user_data)
489 REQUIRES_SHARED(art::Locks::mutator_lock_) {
490 if (UNLIKELY(cb->primitive_field_callback != nullptr)) {
491 jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
492 ReportPrimitiveField rpf(tag_table, class_tag, cb, user_data);
493 if (obj->IsClass()) {
Andreas Gampe95114602017-02-28 15:47:44 -0800494 return FieldVisitor<ReportPrimitiveField, false>::ReportFields(
495 obj,
496 &rpf,
497 ReportPrimitiveFieldCallback<true>,
498 VisitorFalse<ReportPrimitiveField>,
499 VisitorFalse<ReportPrimitiveField>,
500 VisitorFalse<ReportPrimitiveField>);
Andreas Gampee7316932017-02-25 09:15:05 -0800501 } else {
Andreas Gampe95114602017-02-28 15:47:44 -0800502 return FieldVisitor<ReportPrimitiveField, true>::ReportFields(
503 obj,
504 &rpf,
505 VisitorFalse<ReportPrimitiveField>,
506 VisitorFalse<ReportPrimitiveField>,
507 ReportPrimitiveFieldCallback<false>,
508 VisitorFalse<ReportPrimitiveField>);
Andreas Gampee7316932017-02-25 09:15:05 -0800509 }
510 }
511 return false;
512 }
513
514
515 private:
516 ReportPrimitiveField(ObjectTagTable* tag_table,
517 jlong class_tag,
518 const jvmtiHeapCallbacks* cb,
519 const void* user_data)
520 : tag_table_(tag_table), class_tag_(class_tag), cb_(cb), user_data_(user_data) {}
521
522 template <bool kReportStatic>
523 static bool ReportPrimitiveFieldCallback(art::ObjPtr<art::mirror::Object> obj,
524 art::ObjPtr<art::mirror::Class> klass,
525 art::ArtField& field,
526 size_t field_index,
527 ReportPrimitiveField* user_data)
528 REQUIRES_SHARED(art::Locks::mutator_lock_) {
529 art::Primitive::Type art_prim_type = field.GetTypeAsPrimitiveType();
530 jvmtiPrimitiveType prim_type =
531 static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
532 DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
533 prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
534 prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
535 prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
536 prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
537 prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
538 prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
539 prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
540 jvmtiHeapReferenceInfo info;
541 info.field.index = field_index;
542
543 jvalue value;
544 memset(&value, 0, sizeof(jvalue));
545 art::ObjPtr<art::mirror::Object> src = kReportStatic ? klass : obj;
546 switch (art_prim_type) {
547 case art::Primitive::Type::kPrimBoolean:
548 value.z = field.GetBoolean(src) == 0 ? JNI_FALSE : JNI_TRUE;
549 break;
550 case art::Primitive::Type::kPrimByte:
551 value.b = field.GetByte(src);
552 break;
553 case art::Primitive::Type::kPrimChar:
554 value.c = field.GetChar(src);
555 break;
556 case art::Primitive::Type::kPrimShort:
557 value.s = field.GetShort(src);
558 break;
559 case art::Primitive::Type::kPrimInt:
560 value.i = field.GetInt(src);
561 break;
562 case art::Primitive::Type::kPrimLong:
563 value.j = field.GetLong(src);
564 break;
565 case art::Primitive::Type::kPrimFloat:
566 value.f = field.GetFloat(src);
567 break;
568 case art::Primitive::Type::kPrimDouble:
569 value.d = field.GetDouble(src);
570 break;
571 case art::Primitive::Type::kPrimVoid:
572 case art::Primitive::Type::kPrimNot: {
573 LOG(FATAL) << "Should not reach here";
574 UNREACHABLE();
575 }
576 }
577
578 jlong obj_tag = user_data->tag_table_->GetTagOrZero(src.Ptr());
579 const jlong saved_obj_tag = obj_tag;
580
581 jint ret = user_data->cb_->primitive_field_callback(kReportStatic
582 ? JVMTI_HEAP_REFERENCE_STATIC_FIELD
583 : JVMTI_HEAP_REFERENCE_FIELD,
584 &info,
585 user_data->class_tag_,
586 &obj_tag,
587 value,
588 prim_type,
589 const_cast<void*>(user_data->user_data_));
590
591 if (saved_obj_tag != obj_tag) {
592 user_data->tag_table_->Set(src.Ptr(), obj_tag);
593 }
594
595 if ((ret & JVMTI_VISIT_ABORT) != 0) {
596 return true;
597 }
598
599 return false;
600 }
601
602 ObjectTagTable* tag_table_;
603 jlong class_tag_;
604 const jvmtiHeapCallbacks* cb_;
605 const void* user_data_;
606};
607
Andreas Gampe6ea06072017-02-24 18:01:19 +0000608struct HeapFilter {
609 explicit HeapFilter(jint heap_filter)
610 : filter_out_tagged((heap_filter & JVMTI_HEAP_FILTER_TAGGED) != 0),
Andreas Gampee54d9922016-10-11 19:55:37 -0700611 filter_out_untagged((heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) != 0),
612 filter_out_class_tagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) != 0),
613 filter_out_class_untagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) != 0),
614 any_filter(filter_out_tagged ||
615 filter_out_untagged ||
616 filter_out_class_tagged ||
Andreas Gampe6ea06072017-02-24 18:01:19 +0000617 filter_out_class_untagged) {
Andreas Gampee54d9922016-10-11 19:55:37 -0700618 }
619
Andreas Gampe6ea06072017-02-24 18:01:19 +0000620 bool ShouldReportByHeapFilter(jlong tag, jlong class_tag) const {
Andreas Gampee54d9922016-10-11 19:55:37 -0700621 if (!any_filter) {
622 return true;
623 }
624
625 if ((tag == 0 && filter_out_untagged) || (tag != 0 && filter_out_tagged)) {
626 return false;
627 }
628
629 if ((class_tag == 0 && filter_out_class_untagged) ||
630 (class_tag != 0 && filter_out_class_tagged)) {
631 return false;
632 }
633
634 return true;
635 }
636
Andreas Gampee54d9922016-10-11 19:55:37 -0700637 const bool filter_out_tagged;
638 const bool filter_out_untagged;
639 const bool filter_out_class_tagged;
640 const bool filter_out_class_untagged;
641 const bool any_filter;
Andreas Gampe6ea06072017-02-24 18:01:19 +0000642};
643
644} // namespace
645
Andreas Gampe9e38a502017-03-06 08:19:26 -0800646void HeapUtil::Register() {
647 art::Runtime::Current()->AddSystemWeakHolder(&gIndexCachingTable);
648}
649
650void HeapUtil::Unregister() {
651 art::Runtime::Current()->RemoveSystemWeakHolder(&gIndexCachingTable);
652}
653
Andreas Gampe6ea06072017-02-24 18:01:19 +0000654struct IterateThroughHeapData {
655 IterateThroughHeapData(HeapUtil* _heap_util,
656 jvmtiEnv* _env,
657 art::ObjPtr<art::mirror::Class> klass,
658 jint _heap_filter,
659 const jvmtiHeapCallbacks* _callbacks,
660 const void* _user_data)
661 : heap_util(_heap_util),
662 heap_filter(_heap_filter),
663 filter_klass(klass),
664 env(_env),
665 callbacks(_callbacks),
666 user_data(_user_data),
667 stop_reports(false) {
668 }
669
670 HeapUtil* heap_util;
671 const HeapFilter heap_filter;
672 art::ObjPtr<art::mirror::Class> filter_klass;
673 jvmtiEnv* env;
674 const jvmtiHeapCallbacks* callbacks;
675 const void* user_data;
Andreas Gampee54d9922016-10-11 19:55:37 -0700676
677 bool stop_reports;
678};
679
680static void IterateThroughHeapObjectCallback(art::mirror::Object* obj, void* arg)
681 REQUIRES_SHARED(art::Locks::mutator_lock_) {
682 IterateThroughHeapData* ithd = reinterpret_cast<IterateThroughHeapData*>(arg);
683 // Early return, as we can't really stop visiting.
684 if (ithd->stop_reports) {
685 return;
686 }
687
688 art::ScopedAssertNoThreadSuspension no_suspension("IterateThroughHeapCallback");
689
690 jlong tag = 0;
691 ithd->heap_util->GetTags()->GetTag(obj, &tag);
692
693 jlong class_tag = 0;
694 art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
695 ithd->heap_util->GetTags()->GetTag(klass.Ptr(), &class_tag);
696 // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
697
Andreas Gampe6ea06072017-02-24 18:01:19 +0000698 if (!ithd->heap_filter.ShouldReportByHeapFilter(tag, class_tag)) {
Andreas Gampee54d9922016-10-11 19:55:37 -0700699 return;
700 }
701
Andreas Gampee54d9922016-10-11 19:55:37 -0700702 if (ithd->filter_klass != nullptr) {
703 if (ithd->filter_klass != klass) {
704 return;
705 }
706 }
707
708 jlong size = obj->SizeOf();
709
710 jint length = -1;
711 if (obj->IsArrayInstance()) {
712 length = obj->AsArray()->GetLength();
713 }
714
715 jlong saved_tag = tag;
716 jint ret = ithd->callbacks->heap_iteration_callback(class_tag,
717 size,
718 &tag,
719 length,
720 const_cast<void*>(ithd->user_data));
721
722 if (tag != saved_tag) {
723 ithd->heap_util->GetTags()->Set(obj, tag);
724 }
725
726 ithd->stop_reports = (ret & JVMTI_VISIT_ABORT) != 0;
727
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800728 if (!ithd->stop_reports) {
729 jint string_ret = ReportString(obj,
730 ithd->env,
731 ithd->heap_util->GetTags(),
732 ithd->callbacks,
733 ithd->user_data);
734 ithd->stop_reports = (string_ret & JVMTI_VISIT_ABORT) != 0;
735 }
736
Andreas Gampebecd6ad2017-02-22 19:20:37 -0800737 if (!ithd->stop_reports) {
738 jint array_ret = ReportPrimitiveArray(obj,
739 ithd->env,
740 ithd->heap_util->GetTags(),
741 ithd->callbacks,
742 ithd->user_data);
743 ithd->stop_reports = (array_ret & JVMTI_VISIT_ABORT) != 0;
744 }
745
Andreas Gampee7316932017-02-25 09:15:05 -0800746 if (!ithd->stop_reports) {
747 ithd->stop_reports = ReportPrimitiveField::Report(obj,
748 ithd->heap_util->GetTags(),
749 ithd->callbacks,
750 ithd->user_data);
751 }
Andreas Gampee54d9922016-10-11 19:55:37 -0700752}
753
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800754jvmtiError HeapUtil::IterateThroughHeap(jvmtiEnv* env,
Andreas Gampee54d9922016-10-11 19:55:37 -0700755 jint heap_filter,
756 jclass klass,
757 const jvmtiHeapCallbacks* callbacks,
758 const void* user_data) {
759 if (callbacks == nullptr) {
760 return ERR(NULL_POINTER);
761 }
762
Andreas Gampee54d9922016-10-11 19:55:37 -0700763 art::Thread* self = art::Thread::Current();
764 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
765
766 IterateThroughHeapData ithd(this,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800767 env,
Nicolas Geoffray2cb576c2017-02-24 09:40:37 +0000768 soa.Decode<art::mirror::Class>(klass),
Andreas Gampe6ea06072017-02-24 18:01:19 +0000769 heap_filter,
Andreas Gampee54d9922016-10-11 19:55:37 -0700770 callbacks,
771 user_data);
772
773 art::Runtime::Current()->GetHeap()->VisitObjects(IterateThroughHeapObjectCallback, &ithd);
774
775 return ERR(NONE);
776}
777
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700778class FollowReferencesHelper FINAL {
779 public:
780 FollowReferencesHelper(HeapUtil* h,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800781 jvmtiEnv* jvmti_env,
Andreas Gampe638a6932016-12-02 19:11:17 -0800782 art::ObjPtr<art::mirror::Object> initial_object,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700783 const jvmtiHeapCallbacks* callbacks,
Andreas Gampe38da9f22017-02-20 13:35:36 -0800784 art::ObjPtr<art::mirror::Class> class_filter,
Andreas Gampe6ea06072017-02-24 18:01:19 +0000785 jint heap_filter,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700786 const void* user_data)
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800787 : env(jvmti_env),
788 tag_table_(h->GetTags()),
Andreas Gampe638a6932016-12-02 19:11:17 -0800789 initial_object_(initial_object),
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700790 callbacks_(callbacks),
Andreas Gampe38da9f22017-02-20 13:35:36 -0800791 class_filter_(class_filter),
Andreas Gampe6ea06072017-02-24 18:01:19 +0000792 heap_filter_(heap_filter),
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700793 user_data_(user_data),
794 start_(0),
795 stop_reports_(false) {
796 }
797
798 void Init()
799 REQUIRES_SHARED(art::Locks::mutator_lock_)
800 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
Andreas Gampe638a6932016-12-02 19:11:17 -0800801 if (initial_object_.IsNull()) {
802 CollectAndReportRootsVisitor carrv(this, tag_table_, &worklist_, &visited_);
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800803
804 // We need precise info (e.g., vregs).
805 constexpr art::VisitRootFlags kRootFlags = static_cast<art::VisitRootFlags>(
806 art::VisitRootFlags::kVisitRootFlagAllRoots | art::VisitRootFlags::kVisitRootFlagPrecise);
807 art::Runtime::Current()->VisitRoots(&carrv, kRootFlags);
808
Andreas Gampe638a6932016-12-02 19:11:17 -0800809 art::Runtime::Current()->VisitImageRoots(&carrv);
810 stop_reports_ = carrv.IsStopReports();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700811
Andreas Gampe638a6932016-12-02 19:11:17 -0800812 if (stop_reports_) {
813 worklist_.clear();
814 }
815 } else {
816 visited_.insert(initial_object_.Ptr());
817 worklist_.push_back(initial_object_.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700818 }
819 }
820
821 void Work()
822 REQUIRES_SHARED(art::Locks::mutator_lock_)
823 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
824 // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately
825 // from the head of the work list, instead postponing until there's a gap that's "large."
826 //
827 // Alternatively, we can implement a DFS and use the work list as a stack.
828 while (start_ < worklist_.size()) {
829 art::mirror::Object* cur_obj = worklist_[start_];
830 start_++;
831
832 if (start_ >= kMaxStart) {
833 worklist_.erase(worklist_.begin(), worklist_.begin() + start_);
834 start_ = 0;
835 }
836
837 VisitObject(cur_obj);
838
839 if (stop_reports_) {
840 break;
841 }
842 }
843 }
844
845 private:
846 class CollectAndReportRootsVisitor FINAL : public art::RootVisitor {
847 public:
848 CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
849 ObjectTagTable* tag_table,
850 std::vector<art::mirror::Object*>* worklist,
851 std::unordered_set<art::mirror::Object*>* visited)
852 : helper_(helper),
853 tag_table_(tag_table),
854 worklist_(worklist),
855 visited_(visited),
856 stop_reports_(false) {}
857
858 void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
859 OVERRIDE
860 REQUIRES_SHARED(art::Locks::mutator_lock_)
861 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
862 for (size_t i = 0; i != count; ++i) {
863 AddRoot(*roots[i], info);
864 }
865 }
866
867 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
868 size_t count,
869 const art::RootInfo& info)
870 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_)
871 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
872 for (size_t i = 0; i != count; ++i) {
873 AddRoot(roots[i]->AsMirrorPtr(), info);
874 }
875 }
876
877 bool IsStopReports() {
878 return stop_reports_;
879 }
880
881 private:
882 void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
883 REQUIRES_SHARED(art::Locks::mutator_lock_)
884 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
Andreas Gampec756f082017-03-29 17:58:28 -0700885 if (stop_reports_) {
886 return;
887 }
888 bool add_to_worklist = ReportRoot(root_obj, info);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700889 // We use visited_ to mark roots already so we do not need another set.
890 if (visited_->find(root_obj) == visited_->end()) {
891 visited_->insert(root_obj);
Andreas Gampec756f082017-03-29 17:58:28 -0700892 if (add_to_worklist) {
893 worklist_->push_back(root_obj);
894 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700895 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700896 }
897
Andreas Gampe93c30902016-11-18 13:30:30 -0800898 // Remove NO_THREAD_SAFETY_ANALYSIS once ASSERT_CAPABILITY works correctly.
899 art::Thread* FindThread(const art::RootInfo& info) NO_THREAD_SAFETY_ANALYSIS {
900 art::Locks::thread_list_lock_->AssertExclusiveHeld(art::Thread::Current());
901 return art::Runtime::Current()->GetThreadList()->FindThreadByThreadId(info.GetThreadId());
902 }
903
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700904 jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
905 jvmtiHeapReferenceInfo* ref_info)
906 REQUIRES_SHARED(art::Locks::mutator_lock_) {
907 // TODO: Fill in ref_info.
908 memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
909
910 switch (info.GetType()) {
911 case art::RootType::kRootJNIGlobal:
912 return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
913
914 case art::RootType::kRootJNILocal:
Andreas Gampe93c30902016-11-18 13:30:30 -0800915 {
916 uint32_t thread_id = info.GetThreadId();
917 ref_info->jni_local.thread_id = thread_id;
918
919 art::Thread* thread = FindThread(info);
920 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800921 art::mirror::Object* thread_obj;
Andreas Gampe93c30902016-11-18 13:30:30 -0800922 if (thread->IsStillStarting()) {
923 thread_obj = nullptr;
924 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800925 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampe93c30902016-11-18 13:30:30 -0800926 }
927 if (thread_obj != nullptr) {
928 ref_info->jni_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
929 }
930 }
931
932 // TODO: We don't have this info.
933 if (thread != nullptr) {
934 ref_info->jni_local.depth = 0;
935 art::ArtMethod* method = thread->GetCurrentMethod(nullptr, false /* abort_on_error */);
936 if (method != nullptr) {
937 ref_info->jni_local.method = art::jni::EncodeArtMethod(method);
938 }
939 }
940
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700941 return JVMTI_HEAP_REFERENCE_JNI_LOCAL;
Andreas Gampe93c30902016-11-18 13:30:30 -0800942 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700943
944 case art::RootType::kRootJavaFrame:
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800945 {
946 uint32_t thread_id = info.GetThreadId();
947 ref_info->stack_local.thread_id = thread_id;
948
949 art::Thread* thread = FindThread(info);
950 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800951 art::mirror::Object* thread_obj;
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800952 if (thread->IsStillStarting()) {
953 thread_obj = nullptr;
954 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800955 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800956 }
957 if (thread_obj != nullptr) {
958 ref_info->stack_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
959 }
960 }
961
962 auto& java_info = static_cast<const art::JavaFrameRootInfo&>(info);
963 ref_info->stack_local.slot = static_cast<jint>(java_info.GetVReg());
964 const art::StackVisitor* visitor = java_info.GetVisitor();
965 ref_info->stack_local.location =
966 static_cast<jlocation>(visitor->GetDexPc(false /* abort_on_failure */));
967 ref_info->stack_local.depth = static_cast<jint>(visitor->GetFrameDepth());
968 art::ArtMethod* method = visitor->GetMethod();
969 if (method != nullptr) {
970 ref_info->stack_local.method = art::jni::EncodeArtMethod(method);
971 }
972
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700973 return JVMTI_HEAP_REFERENCE_STACK_LOCAL;
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800974 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700975
976 case art::RootType::kRootNativeStack:
977 case art::RootType::kRootThreadBlock:
978 case art::RootType::kRootThreadObject:
979 return JVMTI_HEAP_REFERENCE_THREAD;
980
981 case art::RootType::kRootStickyClass:
982 case art::RootType::kRootInternedString:
983 // Note: this isn't a root in the RI.
984 return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
985
986 case art::RootType::kRootMonitorUsed:
987 case art::RootType::kRootJNIMonitor:
988 return JVMTI_HEAP_REFERENCE_MONITOR;
989
990 case art::RootType::kRootFinalizing:
991 case art::RootType::kRootDebugger:
992 case art::RootType::kRootReferenceCleanup:
993 case art::RootType::kRootVMInternal:
994 case art::RootType::kRootUnknown:
995 return JVMTI_HEAP_REFERENCE_OTHER;
996 }
997 LOG(FATAL) << "Unreachable";
998 UNREACHABLE();
999 }
1000
Andreas Gampec756f082017-03-29 17:58:28 -07001001 bool ReportRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001002 REQUIRES_SHARED(art::Locks::mutator_lock_)
1003 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1004 jvmtiHeapReferenceInfo ref_info;
1005 jvmtiHeapReferenceKind kind = GetReferenceKind(info, &ref_info);
1006 jint result = helper_->ReportReference(kind, &ref_info, nullptr, root_obj);
1007 if ((result & JVMTI_VISIT_ABORT) != 0) {
1008 stop_reports_ = true;
1009 }
Andreas Gampec756f082017-03-29 17:58:28 -07001010 return (result & JVMTI_VISIT_OBJECTS) != 0;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001011 }
1012
1013 private:
1014 FollowReferencesHelper* helper_;
1015 ObjectTagTable* tag_table_;
1016 std::vector<art::mirror::Object*>* worklist_;
1017 std::unordered_set<art::mirror::Object*>* visited_;
1018 bool stop_reports_;
1019 };
1020
1021 void VisitObject(art::mirror::Object* obj)
1022 REQUIRES_SHARED(art::Locks::mutator_lock_)
1023 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1024 if (obj->IsClass()) {
1025 VisitClass(obj->AsClass());
1026 return;
1027 }
1028 if (obj->IsArrayInstance()) {
1029 VisitArray(obj);
1030 return;
1031 }
1032
Andreas Gampe95114602017-02-28 15:47:44 -08001033 // All instance fields.
1034 auto report_instance_field = [&](art::ObjPtr<art::mirror::Object> src,
1035 art::ObjPtr<art::mirror::Class> obj_klass ATTRIBUTE_UNUSED,
1036 art::ArtField& field,
1037 size_t field_index,
1038 void* user_data ATTRIBUTE_UNUSED)
1039 REQUIRES_SHARED(art::Locks::mutator_lock_)
1040 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1041 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(src);
1042 if (field_value != nullptr) {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001043 jvmtiHeapReferenceInfo reference_info;
1044 memset(&reference_info, 0, sizeof(reference_info));
1045
Andreas Gampe95114602017-02-28 15:47:44 -08001046 reference_info.field.index = field_index;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001047
1048 jvmtiHeapReferenceKind kind =
Andreas Gampe95114602017-02-28 15:47:44 -08001049 field.GetOffset().Int32Value() == art::mirror::Object::ClassOffset().Int32Value()
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001050 ? JVMTI_HEAP_REFERENCE_CLASS
1051 : JVMTI_HEAP_REFERENCE_FIELD;
1052 const jvmtiHeapReferenceInfo* reference_info_ptr =
1053 kind == JVMTI_HEAP_REFERENCE_CLASS ? nullptr : &reference_info;
1054
Andreas Gampe95114602017-02-28 15:47:44 -08001055 return !ReportReferenceMaybeEnqueue(kind, reference_info_ptr, src.Ptr(), field_value.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001056 }
Andreas Gampe95114602017-02-28 15:47:44 -08001057 return false;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001058 };
Andreas Gampe95114602017-02-28 15:47:44 -08001059 stop_reports_ = FieldVisitor<void, true>::ReportFields(obj,
1060 nullptr,
1061 VisitorFalse<void>,
1062 VisitorFalse<void>,
1063 VisitorFalse<void>,
1064 report_instance_field);
1065 if (stop_reports_) {
1066 return;
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001067 }
Andreas Gampee7316932017-02-25 09:15:05 -08001068
Andreas Gampe95114602017-02-28 15:47:44 -08001069 jint string_ret = ReportString(obj, env, tag_table_, callbacks_, user_data_);
1070 stop_reports_ = (string_ret & JVMTI_VISIT_ABORT) != 0;
1071 if (stop_reports_) {
1072 return;
Andreas Gampee7316932017-02-25 09:15:05 -08001073 }
Andreas Gampe95114602017-02-28 15:47:44 -08001074
1075 stop_reports_ = ReportPrimitiveField::Report(obj, tag_table_, callbacks_, user_data_);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001076 }
1077
1078 void VisitArray(art::mirror::Object* array)
1079 REQUIRES_SHARED(art::Locks::mutator_lock_)
1080 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1081 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS,
1082 nullptr,
1083 array,
1084 array->GetClass());
1085 if (stop_reports_) {
1086 return;
1087 }
1088
1089 if (array->IsObjectArray()) {
1090 art::mirror::ObjectArray<art::mirror::Object>* obj_array =
1091 array->AsObjectArray<art::mirror::Object>();
1092 int32_t length = obj_array->GetLength();
1093 for (int32_t i = 0; i != length; ++i) {
1094 art::mirror::Object* elem = obj_array->GetWithoutChecks(i);
1095 if (elem != nullptr) {
1096 jvmtiHeapReferenceInfo reference_info;
1097 reference_info.array.index = i;
1098 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
1099 &reference_info,
1100 array,
1101 elem);
1102 if (stop_reports_) {
1103 break;
1104 }
1105 }
1106 }
Andreas Gampebecd6ad2017-02-22 19:20:37 -08001107 } else {
1108 if (!stop_reports_) {
1109 jint array_ret = ReportPrimitiveArray(array, env, tag_table_, callbacks_, user_data_);
1110 stop_reports_ = (array_ret & JVMTI_VISIT_ABORT) != 0;
1111 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001112 }
1113 }
1114
1115 void VisitClass(art::mirror::Class* klass)
1116 REQUIRES_SHARED(art::Locks::mutator_lock_)
1117 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1118 // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones.
1119 if (!klass->IsResolved()) {
1120 return;
1121 }
1122
1123 // Superclass.
1124 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_SUPERCLASS,
1125 nullptr,
1126 klass,
1127 klass->GetSuperClass());
1128 if (stop_reports_) {
1129 return;
1130 }
1131
1132 // Directly implemented or extended interfaces.
1133 art::Thread* self = art::Thread::Current();
1134 art::StackHandleScope<1> hs(self);
1135 art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass));
1136 for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
1137 art::ObjPtr<art::mirror::Class> inf_klass =
Vladimir Marko19a4d372016-12-08 14:41:46 +00001138 art::mirror::Class::ResolveDirectInterface(self, h_klass, i);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001139 if (inf_klass == nullptr) {
1140 // TODO: With a resolved class this should not happen...
1141 self->ClearException();
1142 break;
1143 }
1144
1145 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_INTERFACE,
1146 nullptr,
1147 klass,
1148 inf_klass.Ptr());
1149 if (stop_reports_) {
1150 return;
1151 }
1152 }
1153
1154 // Classloader.
1155 // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the
1156 // fake BootClassLoader?
1157 if (klass->GetClassLoader() != nullptr) {
1158 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
1159 nullptr,
1160 klass,
1161 klass->GetClassLoader());
1162 if (stop_reports_) {
1163 return;
1164 }
1165 }
1166 DCHECK_EQ(h_klass.Get(), klass);
1167
1168 // Declared static fields.
Andreas Gampe95114602017-02-28 15:47:44 -08001169 auto report_static_field = [&](art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
1170 art::ObjPtr<art::mirror::Class> obj_klass,
1171 art::ArtField& field,
1172 size_t field_index,
1173 void* user_data ATTRIBUTE_UNUSED)
1174 REQUIRES_SHARED(art::Locks::mutator_lock_)
1175 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1176 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(obj_klass);
1177 if (field_value != nullptr) {
1178 jvmtiHeapReferenceInfo reference_info;
1179 memset(&reference_info, 0, sizeof(reference_info));
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001180
Andreas Gampe95114602017-02-28 15:47:44 -08001181 reference_info.field.index = static_cast<jint>(field_index);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001182
Andreas Gampe95114602017-02-28 15:47:44 -08001183 return !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
1184 &reference_info,
1185 obj_klass.Ptr(),
1186 field_value.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001187 }
Andreas Gampe95114602017-02-28 15:47:44 -08001188 return false;
1189 };
1190 stop_reports_ = FieldVisitor<void, false>::ReportFields(klass,
1191 nullptr,
1192 VisitorFalse<void>,
1193 report_static_field,
1194 VisitorFalse<void>,
1195 VisitorFalse<void>);
1196 if (stop_reports_) {
1197 return;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001198 }
Andreas Gampee7316932017-02-25 09:15:05 -08001199
Andreas Gampe95114602017-02-28 15:47:44 -08001200 stop_reports_ = ReportPrimitiveField::Report(klass, tag_table_, callbacks_, user_data_);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001201 }
1202
1203 void MaybeEnqueue(art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1204 if (visited_.find(obj) == visited_.end()) {
1205 worklist_.push_back(obj);
1206 visited_.insert(obj);
1207 }
1208 }
1209
1210 bool ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,
1211 const jvmtiHeapReferenceInfo* reference_info,
1212 art::mirror::Object* referree,
1213 art::mirror::Object* referrer)
1214 REQUIRES_SHARED(art::Locks::mutator_lock_)
1215 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1216 jint result = ReportReference(kind, reference_info, referree, referrer);
1217 if ((result & JVMTI_VISIT_ABORT) == 0) {
1218 if ((result & JVMTI_VISIT_OBJECTS) != 0) {
1219 MaybeEnqueue(referrer);
1220 }
1221 return true;
1222 } else {
1223 return false;
1224 }
1225 }
1226
1227 jint ReportReference(jvmtiHeapReferenceKind kind,
1228 const jvmtiHeapReferenceInfo* reference_info,
1229 art::mirror::Object* referrer,
1230 art::mirror::Object* referree)
1231 REQUIRES_SHARED(art::Locks::mutator_lock_)
1232 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1233 if (referree == nullptr || stop_reports_) {
1234 return 0;
1235 }
1236
Andreas Gampe38da9f22017-02-20 13:35:36 -08001237 if (UNLIKELY(class_filter_ != nullptr) && class_filter_ != referree->GetClass()) {
1238 return JVMTI_VISIT_OBJECTS;
1239 }
1240
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001241 const jlong class_tag = tag_table_->GetTagOrZero(referree->GetClass());
Andreas Gampe6ea06072017-02-24 18:01:19 +00001242 jlong tag = tag_table_->GetTagOrZero(referree);
1243
1244 if (!heap_filter_.ShouldReportByHeapFilter(tag, class_tag)) {
1245 return JVMTI_VISIT_OBJECTS;
1246 }
1247
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001248 const jlong referrer_class_tag =
1249 referrer == nullptr ? 0 : tag_table_->GetTagOrZero(referrer->GetClass());
1250 const jlong size = static_cast<jlong>(referree->SizeOf());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001251 jlong saved_tag = tag;
1252 jlong referrer_tag = 0;
1253 jlong saved_referrer_tag = 0;
1254 jlong* referrer_tag_ptr;
1255 if (referrer == nullptr) {
1256 referrer_tag_ptr = nullptr;
1257 } else {
1258 if (referrer == referree) {
1259 referrer_tag_ptr = &tag;
1260 } else {
1261 referrer_tag = saved_referrer_tag = tag_table_->GetTagOrZero(referrer);
1262 referrer_tag_ptr = &referrer_tag;
1263 }
1264 }
Andreas Gampe38da9f22017-02-20 13:35:36 -08001265
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001266 jint length = -1;
1267 if (referree->IsArrayInstance()) {
1268 length = referree->AsArray()->GetLength();
1269 }
1270
1271 jint result = callbacks_->heap_reference_callback(kind,
1272 reference_info,
1273 class_tag,
1274 referrer_class_tag,
1275 size,
1276 &tag,
1277 referrer_tag_ptr,
1278 length,
1279 const_cast<void*>(user_data_));
1280
1281 if (tag != saved_tag) {
1282 tag_table_->Set(referree, tag);
1283 }
1284 if (referrer_tag != saved_referrer_tag) {
1285 tag_table_->Set(referrer, referrer_tag);
1286 }
1287
1288 return result;
1289 }
1290
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001291 jvmtiEnv* env;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001292 ObjectTagTable* tag_table_;
Andreas Gampe638a6932016-12-02 19:11:17 -08001293 art::ObjPtr<art::mirror::Object> initial_object_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001294 const jvmtiHeapCallbacks* callbacks_;
Andreas Gampe38da9f22017-02-20 13:35:36 -08001295 art::ObjPtr<art::mirror::Class> class_filter_;
Andreas Gampe6ea06072017-02-24 18:01:19 +00001296 const HeapFilter heap_filter_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001297 const void* user_data_;
1298
1299 std::vector<art::mirror::Object*> worklist_;
1300 size_t start_;
1301 static constexpr size_t kMaxStart = 1000000U;
1302
1303 std::unordered_set<art::mirror::Object*> visited_;
1304
1305 bool stop_reports_;
1306
1307 friend class CollectAndReportRootsVisitor;
1308};
1309
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001310jvmtiError HeapUtil::FollowReferences(jvmtiEnv* env,
Andreas Gampe6ea06072017-02-24 18:01:19 +00001311 jint heap_filter,
Andreas Gampe38da9f22017-02-20 13:35:36 -08001312 jclass klass,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001313 jobject initial_object,
1314 const jvmtiHeapCallbacks* callbacks,
1315 const void* user_data) {
1316 if (callbacks == nullptr) {
1317 return ERR(NULL_POINTER);
1318 }
1319
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001320 art::Thread* self = art::Thread::Current();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001321
Andreas Gampe638a6932016-12-02 19:11:17 -08001322 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
1323 if (heap->IsGcConcurrentAndMoving()) {
1324 // Need to take a heap dump while GC isn't running. See the
1325 // comment in Heap::VisitObjects().
1326 heap->IncrementDisableMovingGC(self);
1327 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001328 {
Andreas Gampe638a6932016-12-02 19:11:17 -08001329 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001330 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
1331 art::ScopedSuspendAll ssa("FollowReferences");
1332
Andreas Gampe38da9f22017-02-20 13:35:36 -08001333 art::ObjPtr<art::mirror::Class> class_filter = klass == nullptr
1334 ? nullptr
1335 : art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(klass));
Andreas Gampe638a6932016-12-02 19:11:17 -08001336 FollowReferencesHelper frh(this,
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001337 env,
Andreas Gampe638a6932016-12-02 19:11:17 -08001338 self->DecodeJObject(initial_object),
1339 callbacks,
Andreas Gampe38da9f22017-02-20 13:35:36 -08001340 class_filter,
Andreas Gampe6ea06072017-02-24 18:01:19 +00001341 heap_filter,
Andreas Gampe638a6932016-12-02 19:11:17 -08001342 user_data);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001343 frh.Init();
1344 frh.Work();
1345 }
Andreas Gampe638a6932016-12-02 19:11:17 -08001346 if (heap->IsGcConcurrentAndMoving()) {
1347 heap->DecrementDisableMovingGC(self);
1348 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001349
1350 return ERR(NONE);
1351}
1352
Andreas Gampeaa8b60c2016-10-12 12:51:25 -07001353jvmtiError HeapUtil::GetLoadedClasses(jvmtiEnv* env,
1354 jint* class_count_ptr,
1355 jclass** classes_ptr) {
1356 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
1357 return ERR(NULL_POINTER);
1358 }
1359
1360 class ReportClassVisitor : public art::ClassVisitor {
1361 public:
1362 explicit ReportClassVisitor(art::Thread* self) : self_(self) {}
1363
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001364 bool operator()(art::ObjPtr<art::mirror::Class> klass)
1365 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampeef54d8d2016-10-25 09:55:53 -07001366 classes_.push_back(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
Andreas Gampeaa8b60c2016-10-12 12:51:25 -07001367 return true;
1368 }
1369
1370 art::Thread* self_;
1371 std::vector<jclass> classes_;
1372 };
1373
1374 art::Thread* self = art::Thread::Current();
1375 ReportClassVisitor rcv(self);
1376 {
1377 art::ScopedObjectAccess soa(self);
1378 art::Runtime::Current()->GetClassLinker()->VisitClasses(&rcv);
1379 }
1380
1381 size_t size = rcv.classes_.size();
1382 jclass* classes = nullptr;
1383 jvmtiError alloc_ret = env->Allocate(static_cast<jlong>(size * sizeof(jclass)),
1384 reinterpret_cast<unsigned char**>(&classes));
1385 if (alloc_ret != ERR(NONE)) {
1386 return alloc_ret;
1387 }
1388
1389 for (size_t i = 0; i < size; ++i) {
1390 classes[i] = rcv.classes_[i];
1391 }
1392 *classes_ptr = classes;
1393 *class_count_ptr = static_cast<jint>(size);
1394
1395 return ERR(NONE);
1396}
1397
Andreas Gampe8da6d032016-10-31 19:31:03 -07001398jvmtiError HeapUtil::ForceGarbageCollection(jvmtiEnv* env ATTRIBUTE_UNUSED) {
1399 art::Runtime::Current()->GetHeap()->CollectGarbage(false);
1400
1401 return ERR(NONE);
1402}
Andreas Gampee54d9922016-10-11 19:55:37 -07001403} // namespace openjdkjvmti