blob: 6c79a602c3e657ab17faa3f0ef0931c213d3f28c [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"
David Sehr67bf42e2018-02-26 16:43:04 -080024#include "dex/primitive.h"
Andreas Gampe1c158a02017-07-13 17:26:19 -070025#include "gc/heap-visit-objects-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "gc/heap.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070027#include "gc_root-inl.h"
Andreas Gampe36a296f2017-06-13 14:11:11 -070028#include "java_frame_root_info.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010029#include "jni/jni_env_ext.h"
30#include "jni/jni_internal.h"
Andreas Gampe9e38a502017-03-06 08:19:26 -080031#include "jvmti_weak_table-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070032#include "mirror/class.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070035#include "obj_ptr-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "object_tagging.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070037#include "runtime.h"
38#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070039#include "stack.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070040#include "thread-inl.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070041#include "thread_list.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070042
43namespace openjdkjvmti {
44
Andreas Gampe3ec8e402017-02-21 15:49:53 -080045namespace {
46
Andreas Gampe9e38a502017-03-06 08:19:26 -080047struct IndexCache {
48 // The number of interface fields implemented by the class. This is a prefix to all assigned
49 // field indices.
50 size_t interface_fields;
51
52 // It would be nice to also cache the following, but it is complicated to wire up into the
53 // generic visit:
54 // The number of fields in interfaces and superclasses. This is the first index assigned to
55 // fields of the class.
56 // size_t superclass_fields;
57};
58using IndexCachingTable = JvmtiWeakTable<IndexCache>;
59
60static IndexCachingTable gIndexCachingTable;
61
Andreas Gampe3ec8e402017-02-21 15:49:53 -080062// Report the contents of a string, if a callback is set.
63jint ReportString(art::ObjPtr<art::mirror::Object> obj,
64 jvmtiEnv* env,
65 ObjectTagTable* tag_table,
66 const jvmtiHeapCallbacks* cb,
67 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
68 if (UNLIKELY(cb->string_primitive_value_callback != nullptr) && obj->IsString()) {
69 art::ObjPtr<art::mirror::String> str = obj->AsString();
70 int32_t string_length = str->GetLength();
Andreas Gampe5f942032017-02-27 19:59:40 -080071 JvmtiUniquePtr<uint16_t[]> data;
Andreas Gampe3ec8e402017-02-21 15:49:53 -080072
Andreas Gampe5f942032017-02-27 19:59:40 -080073 if (string_length > 0) {
74 jvmtiError alloc_error;
75 data = AllocJvmtiUniquePtr<uint16_t[]>(env, string_length, &alloc_error);
76 if (data == nullptr) {
77 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
78 // back? For now just warn.
79 LOG(WARNING) << "Unable to allocate buffer for string reporting! Silently dropping value."
80 << " >" << str->ToModifiedUtf8() << "<";
81 return 0;
Andreas Gampe3ec8e402017-02-21 15:49:53 -080082 }
Andreas Gampe5f942032017-02-27 19:59:40 -080083
84 if (str->IsCompressed()) {
85 uint8_t* compressed_data = str->GetValueCompressed();
86 for (int32_t i = 0; i != string_length; ++i) {
87 data[i] = compressed_data[i];
88 }
89 } else {
90 // Can copy directly.
91 memcpy(data.get(), str->GetValue(), string_length * sizeof(uint16_t));
92 }
Andreas Gampe3ec8e402017-02-21 15:49:53 -080093 }
94
95 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
96 jlong string_tag = tag_table->GetTagOrZero(obj.Ptr());
97 const jlong saved_string_tag = string_tag;
98
99 jint result = cb->string_primitive_value_callback(class_tag,
100 obj->SizeOf(),
101 &string_tag,
102 data.get(),
103 string_length,
104 const_cast<void*>(user_data));
105 if (string_tag != saved_string_tag) {
106 tag_table->Set(obj.Ptr(), string_tag);
107 }
108
109 return result;
110 }
111 return 0;
112}
113
Andreas Gampebecd6ad2017-02-22 19:20:37 -0800114// Report the contents of a primitive array, if a callback is set.
115jint ReportPrimitiveArray(art::ObjPtr<art::mirror::Object> obj,
116 jvmtiEnv* env,
117 ObjectTagTable* tag_table,
118 const jvmtiHeapCallbacks* cb,
119 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
120 if (UNLIKELY(cb->array_primitive_value_callback != nullptr) &&
121 obj->IsArrayInstance() &&
122 !obj->IsObjectArray()) {
123 art::ObjPtr<art::mirror::Array> array = obj->AsArray();
124 int32_t array_length = array->GetLength();
125 size_t component_size = array->GetClass()->GetComponentSize();
126 art::Primitive::Type art_prim_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
127 jvmtiPrimitiveType prim_type =
128 static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
129 DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
130 prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
131 prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
132 prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
133 prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
134 prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
135 prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
136 prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
137
138 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
139 jlong array_tag = tag_table->GetTagOrZero(obj.Ptr());
140 const jlong saved_array_tag = array_tag;
141
142 jint result;
143 if (array_length == 0) {
144 result = cb->array_primitive_value_callback(class_tag,
145 obj->SizeOf(),
146 &array_tag,
147 0,
148 prim_type,
149 nullptr,
150 const_cast<void*>(user_data));
151 } else {
152 jvmtiError alloc_error;
153 JvmtiUniquePtr<char[]> data = AllocJvmtiUniquePtr<char[]>(env,
154 array_length * component_size,
155 &alloc_error);
156 if (data == nullptr) {
157 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
158 // back? For now just warn.
159 LOG(WARNING) << "Unable to allocate buffer for array reporting! Silently dropping value.";
160 return 0;
161 }
162
163 memcpy(data.get(), array->GetRawData(component_size, 0), array_length * component_size);
164
165 result = cb->array_primitive_value_callback(class_tag,
166 obj->SizeOf(),
167 &array_tag,
168 array_length,
169 prim_type,
170 data.get(),
171 const_cast<void*>(user_data));
172 }
173
174 if (array_tag != saved_array_tag) {
175 tag_table->Set(obj.Ptr(), array_tag);
176 }
177
178 return result;
179 }
180 return 0;
181}
182
Andreas Gampee7316932017-02-25 09:15:05 -0800183template <typename UserData>
184bool VisitorFalse(art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
185 art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
186 art::ArtField& field ATTRIBUTE_UNUSED,
187 size_t field_index ATTRIBUTE_UNUSED,
188 UserData* user_data ATTRIBUTE_UNUSED) {
189 return false;
190}
191
Andreas Gampe95114602017-02-28 15:47:44 -0800192template <typename UserData, bool kCallVisitorOnRecursion>
Andreas Gampee7316932017-02-25 09:15:05 -0800193class FieldVisitor {
194 public:
195 // Report the contents of a primitive fields of the given object, if a callback is set.
Andreas Gampe95114602017-02-28 15:47:44 -0800196 template <typename StaticPrimitiveVisitor,
197 typename StaticReferenceVisitor,
198 typename InstancePrimitiveVisitor,
199 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800200 static bool ReportFields(art::ObjPtr<art::mirror::Object> obj,
201 UserData* user_data,
202 StaticPrimitiveVisitor& static_prim_visitor,
203 StaticReferenceVisitor& static_ref_visitor,
204 InstancePrimitiveVisitor& instance_prim_visitor,
205 InstanceReferenceVisitor& instance_ref_visitor)
206 REQUIRES_SHARED(art::Locks::mutator_lock_) {
207 FieldVisitor fv(user_data);
208
209 if (obj->IsClass()) {
210 // When visiting a class, we only visit the static fields of the given class. No field of
211 // superclasses is visited.
212 art::ObjPtr<art::mirror::Class> klass = obj->AsClass();
213 // Only report fields on resolved classes. We need valid field data.
214 if (!klass->IsResolved()) {
215 return false;
216 }
217 return fv.ReportFieldsImpl(nullptr,
218 obj->AsClass(),
219 obj->AsClass()->IsInterface(),
220 static_prim_visitor,
221 static_ref_visitor,
222 instance_prim_visitor,
223 instance_ref_visitor);
224 } else {
225 // See comment above. Just double-checking here, but an instance *should* mean the class was
226 // resolved.
227 DCHECK(obj->GetClass()->IsResolved() || obj->GetClass()->IsErroneousResolved());
228 return fv.ReportFieldsImpl(obj,
229 obj->GetClass(),
230 false,
231 static_prim_visitor,
232 static_ref_visitor,
233 instance_prim_visitor,
234 instance_ref_visitor);
235 }
236 }
237
238 private:
239 explicit FieldVisitor(UserData* user_data) : user_data_(user_data) {}
240
241 // Report the contents of fields of the given object. If obj is null, report the static fields,
242 // otherwise the instance fields.
Andreas Gampe95114602017-02-28 15:47:44 -0800243 template <typename StaticPrimitiveVisitor,
244 typename StaticReferenceVisitor,
245 typename InstancePrimitiveVisitor,
246 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800247 bool ReportFieldsImpl(art::ObjPtr<art::mirror::Object> obj,
248 art::ObjPtr<art::mirror::Class> klass,
249 bool skip_java_lang_object,
250 StaticPrimitiveVisitor& static_prim_visitor,
251 StaticReferenceVisitor& static_ref_visitor,
252 InstancePrimitiveVisitor& instance_prim_visitor,
253 InstanceReferenceVisitor& instance_ref_visitor)
254 REQUIRES_SHARED(art::Locks::mutator_lock_) {
255 // Compute the offset of field indices.
256 size_t interface_field_count = CountInterfaceFields(klass);
257
258 size_t tmp;
259 bool aborted = ReportFieldsRecursive(obj,
260 klass,
261 interface_field_count,
262 skip_java_lang_object,
263 static_prim_visitor,
264 static_ref_visitor,
265 instance_prim_visitor,
266 instance_ref_visitor,
267 &tmp);
268 return aborted;
269 }
270
271 // Visit primitive fields in an object (instance). Return true if the visit was aborted.
Andreas Gampe95114602017-02-28 15:47:44 -0800272 template <typename StaticPrimitiveVisitor,
273 typename StaticReferenceVisitor,
274 typename InstancePrimitiveVisitor,
275 typename InstanceReferenceVisitor>
Andreas Gampee7316932017-02-25 09:15:05 -0800276 bool ReportFieldsRecursive(art::ObjPtr<art::mirror::Object> obj,
277 art::ObjPtr<art::mirror::Class> klass,
278 size_t interface_fields,
279 bool skip_java_lang_object,
280 StaticPrimitiveVisitor& static_prim_visitor,
281 StaticReferenceVisitor& static_ref_visitor,
282 InstancePrimitiveVisitor& instance_prim_visitor,
283 InstanceReferenceVisitor& instance_ref_visitor,
284 size_t* field_index_out)
285 REQUIRES_SHARED(art::Locks::mutator_lock_) {
286 DCHECK(klass != nullptr);
287 size_t field_index;
288 if (klass->GetSuperClass() == nullptr) {
289 // j.l.Object. Start with the fields from interfaces.
290 field_index = interface_fields;
291 if (skip_java_lang_object) {
292 *field_index_out = field_index;
293 return false;
294 }
295 } else {
296 // Report superclass fields.
297 if (kCallVisitorOnRecursion) {
298 if (ReportFieldsRecursive(obj,
299 klass->GetSuperClass(),
300 interface_fields,
301 skip_java_lang_object,
302 static_prim_visitor,
303 static_ref_visitor,
304 instance_prim_visitor,
305 instance_ref_visitor,
306 &field_index)) {
307 return true;
308 }
309 } else {
310 // Still call, but with empty visitor. This is required for correct counting.
311 ReportFieldsRecursive(obj,
312 klass->GetSuperClass(),
313 interface_fields,
314 skip_java_lang_object,
315 VisitorFalse<UserData>,
316 VisitorFalse<UserData>,
317 VisitorFalse<UserData>,
318 VisitorFalse<UserData>,
319 &field_index);
320 }
321 }
322
323 // Now visit fields for the current klass.
324
325 for (auto& static_field : klass->GetSFields()) {
326 if (static_field.IsPrimitiveType()) {
327 if (static_prim_visitor(obj,
328 klass,
329 static_field,
330 field_index,
331 user_data_)) {
332 return true;
333 }
334 } else {
335 if (static_ref_visitor(obj,
336 klass,
337 static_field,
338 field_index,
339 user_data_)) {
340 return true;
341 }
342 }
343 field_index++;
344 }
345
346 for (auto& instance_field : klass->GetIFields()) {
347 if (instance_field.IsPrimitiveType()) {
348 if (instance_prim_visitor(obj,
349 klass,
350 instance_field,
351 field_index,
352 user_data_)) {
353 return true;
354 }
355 } else {
356 if (instance_ref_visitor(obj,
357 klass,
358 instance_field,
359 field_index,
360 user_data_)) {
361 return true;
362 }
363 }
364 field_index++;
365 }
366
367 *field_index_out = field_index;
368 return false;
369 }
370
371 // Implements a visit of the implemented interfaces of a given class.
372 template <typename T>
373 struct RecursiveInterfaceVisit {
374 static void VisitStatic(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
375 REQUIRES_SHARED(art::Locks::mutator_lock_) {
376 RecursiveInterfaceVisit rv;
377 rv.Visit(self, klass, visitor);
378 }
379
380 void Visit(art::Thread* self, art::ObjPtr<art::mirror::Class> klass, T& visitor)
381 REQUIRES_SHARED(art::Locks::mutator_lock_) {
382 // First visit the parent, to get the order right.
383 // (We do this in preparation for actual visiting of interface fields.)
384 if (klass->GetSuperClass() != nullptr) {
385 Visit(self, klass->GetSuperClass(), visitor);
386 }
387 for (uint32_t i = 0; i != klass->NumDirectInterfaces(); ++i) {
388 art::ObjPtr<art::mirror::Class> inf_klass =
389 art::mirror::Class::GetDirectInterface(self, klass, i);
390 DCHECK(inf_klass != nullptr);
391 VisitInterface(self, inf_klass, visitor);
392 }
393 }
394
395 void VisitInterface(art::Thread* self, art::ObjPtr<art::mirror::Class> inf_klass, T& visitor)
396 REQUIRES_SHARED(art::Locks::mutator_lock_) {
397 auto it = visited_interfaces.find(inf_klass.Ptr());
398 if (it != visited_interfaces.end()) {
399 return;
400 }
401 visited_interfaces.insert(inf_klass.Ptr());
402
403 // Let the visitor know about this one. Note that this order is acceptable, as the ordering
404 // of these fields never matters for known visitors.
405 visitor(inf_klass);
406
407 // Now visit the superinterfaces.
408 for (uint32_t i = 0; i != inf_klass->NumDirectInterfaces(); ++i) {
409 art::ObjPtr<art::mirror::Class> super_inf_klass =
410 art::mirror::Class::GetDirectInterface(self, inf_klass, i);
411 DCHECK(super_inf_klass != nullptr);
412 VisitInterface(self, super_inf_klass, visitor);
413 }
414 }
415
416 std::unordered_set<art::mirror::Class*> visited_interfaces;
417 };
418
419 // Counting interface fields. Note that we cannot use the interface table, as that only contains
420 // "non-marker" interfaces (= interfaces with methods).
421 static size_t CountInterfaceFields(art::ObjPtr<art::mirror::Class> klass)
422 REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe9e38a502017-03-06 08:19:26 -0800423 // Do we have a cached value?
424 IndexCache tmp;
425 if (gIndexCachingTable.GetTag(klass.Ptr(), &tmp)) {
426 return tmp.interface_fields;
427 }
428
Andreas Gampee7316932017-02-25 09:15:05 -0800429 size_t count = 0;
430 auto visitor = [&count](art::ObjPtr<art::mirror::Class> inf_klass)
431 REQUIRES_SHARED(art::Locks::mutator_lock_) {
432 DCHECK(inf_klass->IsInterface());
433 DCHECK_EQ(0u, inf_klass->NumInstanceFields());
434 count += inf_klass->NumStaticFields();
435 };
436 RecursiveInterfaceVisit<decltype(visitor)>::VisitStatic(art::Thread::Current(), klass, visitor);
Andreas Gampee7316932017-02-25 09:15:05 -0800437
Andreas Gampe9e38a502017-03-06 08:19:26 -0800438 // Store this into the cache.
439 tmp.interface_fields = count;
440 gIndexCachingTable.Set(klass.Ptr(), tmp);
441
442 return count;
Andreas Gampee7316932017-02-25 09:15:05 -0800443 }
444
445 UserData* user_data_;
446};
447
448// Debug helper. Prints the structure of an object.
449template <bool kStatic, bool kRef>
450struct DumpVisitor {
451 static bool Callback(art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
452 art::ObjPtr<art::mirror::Class> klass ATTRIBUTE_UNUSED,
453 art::ArtField& field,
454 size_t field_index,
455 void* user_data ATTRIBUTE_UNUSED)
456 REQUIRES_SHARED(art::Locks::mutator_lock_) {
457 LOG(ERROR) << (kStatic ? "static " : "instance ")
458 << (kRef ? "ref " : "primitive ")
459 << field.PrettyField()
460 << " @ "
461 << field_index;
462 return false;
463 }
464};
465ATTRIBUTE_UNUSED
466void DumpObjectFields(art::ObjPtr<art::mirror::Object> obj)
467 REQUIRES_SHARED(art::Locks::mutator_lock_) {
468 if (obj->IsClass()) {
Andreas Gampe95114602017-02-28 15:47:44 -0800469 FieldVisitor<void, false>:: ReportFields(obj,
470 nullptr,
471 DumpVisitor<true, false>::Callback,
472 DumpVisitor<true, true>::Callback,
473 DumpVisitor<false, false>::Callback,
474 DumpVisitor<false, true>::Callback);
Andreas Gampee7316932017-02-25 09:15:05 -0800475 } else {
Andreas Gampe95114602017-02-28 15:47:44 -0800476 FieldVisitor<void, true>::ReportFields(obj,
477 nullptr,
478 DumpVisitor<true, false>::Callback,
479 DumpVisitor<true, true>::Callback,
480 DumpVisitor<false, false>::Callback,
481 DumpVisitor<false, true>::Callback);
Andreas Gampee7316932017-02-25 09:15:05 -0800482 }
483}
484
485class ReportPrimitiveField {
486 public:
487 static bool Report(art::ObjPtr<art::mirror::Object> obj,
488 ObjectTagTable* tag_table,
489 const jvmtiHeapCallbacks* cb,
490 const void* user_data)
491 REQUIRES_SHARED(art::Locks::mutator_lock_) {
492 if (UNLIKELY(cb->primitive_field_callback != nullptr)) {
493 jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
494 ReportPrimitiveField rpf(tag_table, class_tag, cb, user_data);
495 if (obj->IsClass()) {
Andreas Gampe95114602017-02-28 15:47:44 -0800496 return FieldVisitor<ReportPrimitiveField, false>::ReportFields(
497 obj,
498 &rpf,
499 ReportPrimitiveFieldCallback<true>,
500 VisitorFalse<ReportPrimitiveField>,
501 VisitorFalse<ReportPrimitiveField>,
502 VisitorFalse<ReportPrimitiveField>);
Andreas Gampee7316932017-02-25 09:15:05 -0800503 } else {
Andreas Gampe95114602017-02-28 15:47:44 -0800504 return FieldVisitor<ReportPrimitiveField, true>::ReportFields(
505 obj,
506 &rpf,
507 VisitorFalse<ReportPrimitiveField>,
508 VisitorFalse<ReportPrimitiveField>,
509 ReportPrimitiveFieldCallback<false>,
510 VisitorFalse<ReportPrimitiveField>);
Andreas Gampee7316932017-02-25 09:15:05 -0800511 }
512 }
513 return false;
514 }
515
516
517 private:
518 ReportPrimitiveField(ObjectTagTable* tag_table,
519 jlong class_tag,
520 const jvmtiHeapCallbacks* cb,
521 const void* user_data)
522 : tag_table_(tag_table), class_tag_(class_tag), cb_(cb), user_data_(user_data) {}
523
524 template <bool kReportStatic>
525 static bool ReportPrimitiveFieldCallback(art::ObjPtr<art::mirror::Object> obj,
526 art::ObjPtr<art::mirror::Class> klass,
527 art::ArtField& field,
528 size_t field_index,
529 ReportPrimitiveField* user_data)
530 REQUIRES_SHARED(art::Locks::mutator_lock_) {
531 art::Primitive::Type art_prim_type = field.GetTypeAsPrimitiveType();
532 jvmtiPrimitiveType prim_type =
533 static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
534 DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
535 prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
536 prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
537 prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
538 prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
539 prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
540 prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
541 prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
542 jvmtiHeapReferenceInfo info;
543 info.field.index = field_index;
544
545 jvalue value;
546 memset(&value, 0, sizeof(jvalue));
547 art::ObjPtr<art::mirror::Object> src = kReportStatic ? klass : obj;
548 switch (art_prim_type) {
549 case art::Primitive::Type::kPrimBoolean:
550 value.z = field.GetBoolean(src) == 0 ? JNI_FALSE : JNI_TRUE;
551 break;
552 case art::Primitive::Type::kPrimByte:
553 value.b = field.GetByte(src);
554 break;
555 case art::Primitive::Type::kPrimChar:
556 value.c = field.GetChar(src);
557 break;
558 case art::Primitive::Type::kPrimShort:
559 value.s = field.GetShort(src);
560 break;
561 case art::Primitive::Type::kPrimInt:
562 value.i = field.GetInt(src);
563 break;
564 case art::Primitive::Type::kPrimLong:
565 value.j = field.GetLong(src);
566 break;
567 case art::Primitive::Type::kPrimFloat:
568 value.f = field.GetFloat(src);
569 break;
570 case art::Primitive::Type::kPrimDouble:
571 value.d = field.GetDouble(src);
572 break;
573 case art::Primitive::Type::kPrimVoid:
574 case art::Primitive::Type::kPrimNot: {
575 LOG(FATAL) << "Should not reach here";
576 UNREACHABLE();
577 }
578 }
579
580 jlong obj_tag = user_data->tag_table_->GetTagOrZero(src.Ptr());
581 const jlong saved_obj_tag = obj_tag;
582
583 jint ret = user_data->cb_->primitive_field_callback(kReportStatic
584 ? JVMTI_HEAP_REFERENCE_STATIC_FIELD
585 : JVMTI_HEAP_REFERENCE_FIELD,
586 &info,
587 user_data->class_tag_,
588 &obj_tag,
589 value,
590 prim_type,
591 const_cast<void*>(user_data->user_data_));
592
593 if (saved_obj_tag != obj_tag) {
594 user_data->tag_table_->Set(src.Ptr(), obj_tag);
595 }
596
597 if ((ret & JVMTI_VISIT_ABORT) != 0) {
598 return true;
599 }
600
601 return false;
602 }
603
604 ObjectTagTable* tag_table_;
605 jlong class_tag_;
606 const jvmtiHeapCallbacks* cb_;
607 const void* user_data_;
608};
609
Andreas Gampe6ea06072017-02-24 18:01:19 +0000610struct HeapFilter {
611 explicit HeapFilter(jint heap_filter)
612 : filter_out_tagged((heap_filter & JVMTI_HEAP_FILTER_TAGGED) != 0),
Andreas Gampee54d9922016-10-11 19:55:37 -0700613 filter_out_untagged((heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) != 0),
614 filter_out_class_tagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) != 0),
615 filter_out_class_untagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) != 0),
616 any_filter(filter_out_tagged ||
617 filter_out_untagged ||
618 filter_out_class_tagged ||
Andreas Gampe6ea06072017-02-24 18:01:19 +0000619 filter_out_class_untagged) {
Andreas Gampee54d9922016-10-11 19:55:37 -0700620 }
621
Andreas Gampe6ea06072017-02-24 18:01:19 +0000622 bool ShouldReportByHeapFilter(jlong tag, jlong class_tag) const {
Andreas Gampee54d9922016-10-11 19:55:37 -0700623 if (!any_filter) {
624 return true;
625 }
626
627 if ((tag == 0 && filter_out_untagged) || (tag != 0 && filter_out_tagged)) {
628 return false;
629 }
630
631 if ((class_tag == 0 && filter_out_class_untagged) ||
632 (class_tag != 0 && filter_out_class_tagged)) {
633 return false;
634 }
635
636 return true;
637 }
638
Andreas Gampee54d9922016-10-11 19:55:37 -0700639 const bool filter_out_tagged;
640 const bool filter_out_untagged;
641 const bool filter_out_class_tagged;
642 const bool filter_out_class_untagged;
643 const bool any_filter;
Andreas Gampe6ea06072017-02-24 18:01:19 +0000644};
645
646} // namespace
647
Andreas Gampe9e38a502017-03-06 08:19:26 -0800648void HeapUtil::Register() {
649 art::Runtime::Current()->AddSystemWeakHolder(&gIndexCachingTable);
650}
651
652void HeapUtil::Unregister() {
653 art::Runtime::Current()->RemoveSystemWeakHolder(&gIndexCachingTable);
654}
655
Alex Lightbbbcb532018-08-30 12:50:27 -0700656jvmtiError HeapUtil::IterateOverInstancesOfClass(jvmtiEnv* env,
657 jclass klass,
658 jvmtiHeapObjectFilter filter,
659 jvmtiHeapObjectCallback cb,
660 const void* user_data) {
661 if (cb == nullptr || klass == nullptr) {
662 return ERR(NULL_POINTER);
663 }
664
665 art::Thread* self = art::Thread::Current();
666 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
667 art::StackHandleScope<1> hs(self);
668
669 art::ObjPtr<art::mirror::Object> klass_ptr(soa.Decode<art::mirror::Class>(klass));
670 if (!klass_ptr->IsClass()) {
671 return ERR(INVALID_CLASS);
672 }
673 art::Handle<art::mirror::Class> filter_klass(hs.NewHandle(klass_ptr->AsClass()));
674 if (filter_klass->IsInterface()) {
675 // nothing is an 'instance' of an interface so just return without walking anything.
676 return OK;
677 }
678
679 ObjectTagTable* tag_table = ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get();
680 bool stop_reports = false;
681 auto visitor = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
682 // Early return, as we can't really stop visiting.
683 if (stop_reports) {
684 return;
685 }
686
687 art::ScopedAssertNoThreadSuspension no_suspension("IterateOverInstancesOfClass");
688
689 art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
690
691 if (filter_klass != nullptr && !filter_klass->IsAssignableFrom(klass)) {
692 return;
693 }
694
695 jlong tag = 0;
696 tag_table->GetTag(obj, &tag);
697 if ((filter != JVMTI_HEAP_OBJECT_EITHER) &&
698 ((tag == 0 && filter == JVMTI_HEAP_OBJECT_TAGGED) ||
699 (tag != 0 && filter == JVMTI_HEAP_OBJECT_UNTAGGED))) {
700 return;
701 }
702
703 jlong class_tag = 0;
704 tag_table->GetTag(klass.Ptr(), &class_tag);
705
706 jlong saved_tag = tag;
707 jint ret = cb(class_tag, obj->SizeOf(), &tag, const_cast<void*>(user_data));
708
709 stop_reports = (ret == JVMTI_ITERATION_ABORT);
710
711 if (tag != saved_tag) {
712 tag_table->Set(obj, tag);
713 }
714 };
715 art::Runtime::Current()->GetHeap()->VisitObjects(visitor);
716
717 return OK;
718}
719
Andreas Gampe1c158a02017-07-13 17:26:19 -0700720template <typename T>
721static jvmtiError DoIterateThroughHeap(T fn,
722 jvmtiEnv* env,
723 ObjectTagTable* tag_table,
724 jint heap_filter_int,
725 jclass klass,
726 const jvmtiHeapCallbacks* callbacks,
727 const void* user_data) {
728 if (callbacks == nullptr) {
729 return ERR(NULL_POINTER);
Andreas Gampe6ea06072017-02-24 18:01:19 +0000730 }
731
Andreas Gampe1c158a02017-07-13 17:26:19 -0700732 art::Thread* self = art::Thread::Current();
733 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
Andreas Gampef787fd32017-05-09 16:04:20 -0700734
Andreas Gampe1c158a02017-07-13 17:26:19 -0700735 bool stop_reports = false;
736 const HeapFilter heap_filter(heap_filter_int);
737 art::ObjPtr<art::mirror::Class> filter_klass = soa.Decode<art::mirror::Class>(klass);
738 auto visitor = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampef787fd32017-05-09 16:04:20 -0700739 // Early return, as we can't really stop visiting.
740 if (stop_reports) {
741 return;
742 }
743
744 art::ScopedAssertNoThreadSuspension no_suspension("IterateThroughHeapCallback");
745
746 jlong tag = 0;
747 tag_table->GetTag(obj, &tag);
748
749 jlong class_tag = 0;
750 art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
751 tag_table->GetTag(klass.Ptr(), &class_tag);
752 // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
753
754 if (!heap_filter.ShouldReportByHeapFilter(tag, class_tag)) {
755 return;
756 }
757
758 if (filter_klass != nullptr) {
759 if (filter_klass != klass) {
760 return;
761 }
762 }
763
764 jlong size = obj->SizeOf();
765
766 jint length = -1;
767 if (obj->IsArrayInstance()) {
768 length = obj->AsArray()->GetLength();
769 }
770
771 jlong saved_tag = tag;
Andreas Gampe1c158a02017-07-13 17:26:19 -0700772 jint ret = fn(obj, callbacks, class_tag, size, &tag, length, const_cast<void*>(user_data));
Andreas Gampef787fd32017-05-09 16:04:20 -0700773
774 if (tag != saved_tag) {
775 tag_table->Set(obj, tag);
776 }
777
778 stop_reports = (ret & JVMTI_VISIT_ABORT) != 0;
779
780 if (!stop_reports) {
781 jint string_ret = ReportString(obj, env, tag_table, callbacks, user_data);
782 stop_reports = (string_ret & JVMTI_VISIT_ABORT) != 0;
783 }
784
785 if (!stop_reports) {
786 jint array_ret = ReportPrimitiveArray(obj, env, tag_table, callbacks, user_data);
787 stop_reports = (array_ret & JVMTI_VISIT_ABORT) != 0;
788 }
789
790 if (!stop_reports) {
791 stop_reports = ReportPrimitiveField::Report(obj, tag_table, callbacks, user_data);
792 }
Andreas Gampe1c158a02017-07-13 17:26:19 -0700793 };
794 art::Runtime::Current()->GetHeap()->VisitObjects(visitor);
Andreas Gampee54d9922016-10-11 19:55:37 -0700795
796 return ERR(NONE);
797}
798
Andreas Gampef787fd32017-05-09 16:04:20 -0700799jvmtiError HeapUtil::IterateThroughHeap(jvmtiEnv* env,
800 jint heap_filter,
801 jclass klass,
802 const jvmtiHeapCallbacks* callbacks,
803 const void* user_data) {
804 auto JvmtiIterateHeap = [](art::mirror::Object* obj ATTRIBUTE_UNUSED,
805 const jvmtiHeapCallbacks* cb_callbacks,
806 jlong class_tag,
807 jlong size,
808 jlong* tag,
809 jint length,
810 void* cb_user_data)
811 REQUIRES_SHARED(art::Locks::mutator_lock_) {
812 return cb_callbacks->heap_iteration_callback(class_tag,
813 size,
814 tag,
815 length,
816 cb_user_data);
817 };
818 return DoIterateThroughHeap(JvmtiIterateHeap,
819 env,
820 ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get(),
821 heap_filter,
822 klass,
823 callbacks,
824 user_data);
825}
826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100827class FollowReferencesHelper final {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700828 public:
829 FollowReferencesHelper(HeapUtil* h,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800830 jvmtiEnv* jvmti_env,
Andreas Gampe638a6932016-12-02 19:11:17 -0800831 art::ObjPtr<art::mirror::Object> initial_object,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700832 const jvmtiHeapCallbacks* callbacks,
Andreas Gampe38da9f22017-02-20 13:35:36 -0800833 art::ObjPtr<art::mirror::Class> class_filter,
Andreas Gampe6ea06072017-02-24 18:01:19 +0000834 jint heap_filter,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700835 const void* user_data)
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800836 : env(jvmti_env),
837 tag_table_(h->GetTags()),
Andreas Gampe638a6932016-12-02 19:11:17 -0800838 initial_object_(initial_object),
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700839 callbacks_(callbacks),
Andreas Gampe38da9f22017-02-20 13:35:36 -0800840 class_filter_(class_filter),
Andreas Gampe6ea06072017-02-24 18:01:19 +0000841 heap_filter_(heap_filter),
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700842 user_data_(user_data),
843 start_(0),
844 stop_reports_(false) {
845 }
846
847 void Init()
848 REQUIRES_SHARED(art::Locks::mutator_lock_)
849 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
Andreas Gampe638a6932016-12-02 19:11:17 -0800850 if (initial_object_.IsNull()) {
851 CollectAndReportRootsVisitor carrv(this, tag_table_, &worklist_, &visited_);
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800852
853 // We need precise info (e.g., vregs).
854 constexpr art::VisitRootFlags kRootFlags = static_cast<art::VisitRootFlags>(
855 art::VisitRootFlags::kVisitRootFlagAllRoots | art::VisitRootFlags::kVisitRootFlagPrecise);
856 art::Runtime::Current()->VisitRoots(&carrv, kRootFlags);
857
Andreas Gampe638a6932016-12-02 19:11:17 -0800858 art::Runtime::Current()->VisitImageRoots(&carrv);
859 stop_reports_ = carrv.IsStopReports();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700860
Andreas Gampe638a6932016-12-02 19:11:17 -0800861 if (stop_reports_) {
862 worklist_.clear();
863 }
864 } else {
865 visited_.insert(initial_object_.Ptr());
866 worklist_.push_back(initial_object_.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700867 }
868 }
869
870 void Work()
871 REQUIRES_SHARED(art::Locks::mutator_lock_)
872 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
873 // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately
874 // from the head of the work list, instead postponing until there's a gap that's "large."
875 //
876 // Alternatively, we can implement a DFS and use the work list as a stack.
877 while (start_ < worklist_.size()) {
878 art::mirror::Object* cur_obj = worklist_[start_];
879 start_++;
880
881 if (start_ >= kMaxStart) {
882 worklist_.erase(worklist_.begin(), worklist_.begin() + start_);
883 start_ = 0;
884 }
885
886 VisitObject(cur_obj);
887
888 if (stop_reports_) {
889 break;
890 }
891 }
892 }
893
894 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100895 class CollectAndReportRootsVisitor final : public art::RootVisitor {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700896 public:
897 CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
898 ObjectTagTable* tag_table,
899 std::vector<art::mirror::Object*>* worklist,
900 std::unordered_set<art::mirror::Object*>* visited)
901 : helper_(helper),
902 tag_table_(tag_table),
903 worklist_(worklist),
904 visited_(visited),
905 stop_reports_(false) {}
906
907 void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100908 override
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700909 REQUIRES_SHARED(art::Locks::mutator_lock_)
910 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
911 for (size_t i = 0; i != count; ++i) {
912 AddRoot(*roots[i], info);
913 }
914 }
915
916 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
917 size_t count,
918 const art::RootInfo& info)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100919 override REQUIRES_SHARED(art::Locks::mutator_lock_)
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700920 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
921 for (size_t i = 0; i != count; ++i) {
922 AddRoot(roots[i]->AsMirrorPtr(), info);
923 }
924 }
925
926 bool IsStopReports() {
927 return stop_reports_;
928 }
929
930 private:
931 void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
932 REQUIRES_SHARED(art::Locks::mutator_lock_)
933 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
Andreas Gampec756f082017-03-29 17:58:28 -0700934 if (stop_reports_) {
935 return;
936 }
937 bool add_to_worklist = ReportRoot(root_obj, info);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700938 // We use visited_ to mark roots already so we do not need another set.
939 if (visited_->find(root_obj) == visited_->end()) {
Andreas Gampec756f082017-03-29 17:58:28 -0700940 if (add_to_worklist) {
Andreas Gampee0f8ed92017-04-13 16:52:23 -0700941 visited_->insert(root_obj);
Andreas Gampec756f082017-03-29 17:58:28 -0700942 worklist_->push_back(root_obj);
943 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700944 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700945 }
946
Andreas Gampe93c30902016-11-18 13:30:30 -0800947 // Remove NO_THREAD_SAFETY_ANALYSIS once ASSERT_CAPABILITY works correctly.
948 art::Thread* FindThread(const art::RootInfo& info) NO_THREAD_SAFETY_ANALYSIS {
949 art::Locks::thread_list_lock_->AssertExclusiveHeld(art::Thread::Current());
950 return art::Runtime::Current()->GetThreadList()->FindThreadByThreadId(info.GetThreadId());
951 }
952
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700953 jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
954 jvmtiHeapReferenceInfo* ref_info)
955 REQUIRES_SHARED(art::Locks::mutator_lock_) {
956 // TODO: Fill in ref_info.
957 memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
958
959 switch (info.GetType()) {
960 case art::RootType::kRootJNIGlobal:
961 return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
962
963 case art::RootType::kRootJNILocal:
Andreas Gampe93c30902016-11-18 13:30:30 -0800964 {
965 uint32_t thread_id = info.GetThreadId();
966 ref_info->jni_local.thread_id = thread_id;
967
968 art::Thread* thread = FindThread(info);
969 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800970 art::mirror::Object* thread_obj;
Andreas Gampe93c30902016-11-18 13:30:30 -0800971 if (thread->IsStillStarting()) {
972 thread_obj = nullptr;
973 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800974 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampe93c30902016-11-18 13:30:30 -0800975 }
976 if (thread_obj != nullptr) {
977 ref_info->jni_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
978 }
979 }
980
981 // TODO: We don't have this info.
982 if (thread != nullptr) {
983 ref_info->jni_local.depth = 0;
984 art::ArtMethod* method = thread->GetCurrentMethod(nullptr, false /* abort_on_error */);
985 if (method != nullptr) {
986 ref_info->jni_local.method = art::jni::EncodeArtMethod(method);
987 }
988 }
989
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700990 return JVMTI_HEAP_REFERENCE_JNI_LOCAL;
Andreas Gampe93c30902016-11-18 13:30:30 -0800991 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700992
993 case art::RootType::kRootJavaFrame:
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800994 {
995 uint32_t thread_id = info.GetThreadId();
996 ref_info->stack_local.thread_id = thread_id;
997
998 art::Thread* thread = FindThread(info);
999 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -08001000 art::mirror::Object* thread_obj;
Andreas Gampef10dfcd2016-12-02 14:42:33 -08001001 if (thread->IsStillStarting()) {
1002 thread_obj = nullptr;
1003 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -08001004 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampef10dfcd2016-12-02 14:42:33 -08001005 }
1006 if (thread_obj != nullptr) {
1007 ref_info->stack_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
1008 }
1009 }
1010
1011 auto& java_info = static_cast<const art::JavaFrameRootInfo&>(info);
1012 ref_info->stack_local.slot = static_cast<jint>(java_info.GetVReg());
1013 const art::StackVisitor* visitor = java_info.GetVisitor();
1014 ref_info->stack_local.location =
1015 static_cast<jlocation>(visitor->GetDexPc(false /* abort_on_failure */));
1016 ref_info->stack_local.depth = static_cast<jint>(visitor->GetFrameDepth());
1017 art::ArtMethod* method = visitor->GetMethod();
1018 if (method != nullptr) {
1019 ref_info->stack_local.method = art::jni::EncodeArtMethod(method);
1020 }
1021
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001022 return JVMTI_HEAP_REFERENCE_STACK_LOCAL;
Andreas Gampef10dfcd2016-12-02 14:42:33 -08001023 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001024
1025 case art::RootType::kRootNativeStack:
1026 case art::RootType::kRootThreadBlock:
1027 case art::RootType::kRootThreadObject:
1028 return JVMTI_HEAP_REFERENCE_THREAD;
1029
1030 case art::RootType::kRootStickyClass:
1031 case art::RootType::kRootInternedString:
1032 // Note: this isn't a root in the RI.
1033 return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
1034
1035 case art::RootType::kRootMonitorUsed:
1036 case art::RootType::kRootJNIMonitor:
1037 return JVMTI_HEAP_REFERENCE_MONITOR;
1038
1039 case art::RootType::kRootFinalizing:
1040 case art::RootType::kRootDebugger:
1041 case art::RootType::kRootReferenceCleanup:
1042 case art::RootType::kRootVMInternal:
1043 case art::RootType::kRootUnknown:
1044 return JVMTI_HEAP_REFERENCE_OTHER;
1045 }
1046 LOG(FATAL) << "Unreachable";
1047 UNREACHABLE();
1048 }
1049
Andreas Gampec756f082017-03-29 17:58:28 -07001050 bool ReportRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001051 REQUIRES_SHARED(art::Locks::mutator_lock_)
1052 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1053 jvmtiHeapReferenceInfo ref_info;
1054 jvmtiHeapReferenceKind kind = GetReferenceKind(info, &ref_info);
1055 jint result = helper_->ReportReference(kind, &ref_info, nullptr, root_obj);
1056 if ((result & JVMTI_VISIT_ABORT) != 0) {
1057 stop_reports_ = true;
1058 }
Andreas Gampec756f082017-03-29 17:58:28 -07001059 return (result & JVMTI_VISIT_OBJECTS) != 0;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001060 }
1061
1062 private:
1063 FollowReferencesHelper* helper_;
1064 ObjectTagTable* tag_table_;
1065 std::vector<art::mirror::Object*>* worklist_;
1066 std::unordered_set<art::mirror::Object*>* visited_;
1067 bool stop_reports_;
1068 };
1069
1070 void VisitObject(art::mirror::Object* obj)
1071 REQUIRES_SHARED(art::Locks::mutator_lock_)
1072 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1073 if (obj->IsClass()) {
1074 VisitClass(obj->AsClass());
1075 return;
1076 }
1077 if (obj->IsArrayInstance()) {
1078 VisitArray(obj);
1079 return;
1080 }
1081
Andreas Gampe95114602017-02-28 15:47:44 -08001082 // All instance fields.
1083 auto report_instance_field = [&](art::ObjPtr<art::mirror::Object> src,
1084 art::ObjPtr<art::mirror::Class> obj_klass ATTRIBUTE_UNUSED,
1085 art::ArtField& field,
1086 size_t field_index,
1087 void* user_data ATTRIBUTE_UNUSED)
1088 REQUIRES_SHARED(art::Locks::mutator_lock_)
1089 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1090 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(src);
1091 if (field_value != nullptr) {
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001092 jvmtiHeapReferenceInfo reference_info;
1093 memset(&reference_info, 0, sizeof(reference_info));
1094
Andreas Gampe95114602017-02-28 15:47:44 -08001095 reference_info.field.index = field_index;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001096
1097 jvmtiHeapReferenceKind kind =
Andreas Gampe95114602017-02-28 15:47:44 -08001098 field.GetOffset().Int32Value() == art::mirror::Object::ClassOffset().Int32Value()
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001099 ? JVMTI_HEAP_REFERENCE_CLASS
1100 : JVMTI_HEAP_REFERENCE_FIELD;
1101 const jvmtiHeapReferenceInfo* reference_info_ptr =
1102 kind == JVMTI_HEAP_REFERENCE_CLASS ? nullptr : &reference_info;
1103
Andreas Gampe95114602017-02-28 15:47:44 -08001104 return !ReportReferenceMaybeEnqueue(kind, reference_info_ptr, src.Ptr(), field_value.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001105 }
Andreas Gampe95114602017-02-28 15:47:44 -08001106 return false;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001107 };
Andreas Gampe95114602017-02-28 15:47:44 -08001108 stop_reports_ = FieldVisitor<void, true>::ReportFields(obj,
1109 nullptr,
1110 VisitorFalse<void>,
1111 VisitorFalse<void>,
1112 VisitorFalse<void>,
1113 report_instance_field);
1114 if (stop_reports_) {
1115 return;
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001116 }
Andreas Gampee7316932017-02-25 09:15:05 -08001117
Andreas Gampe95114602017-02-28 15:47:44 -08001118 jint string_ret = ReportString(obj, env, tag_table_, callbacks_, user_data_);
1119 stop_reports_ = (string_ret & JVMTI_VISIT_ABORT) != 0;
1120 if (stop_reports_) {
1121 return;
Andreas Gampee7316932017-02-25 09:15:05 -08001122 }
Andreas Gampe95114602017-02-28 15:47:44 -08001123
1124 stop_reports_ = ReportPrimitiveField::Report(obj, tag_table_, callbacks_, user_data_);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001125 }
1126
1127 void VisitArray(art::mirror::Object* array)
1128 REQUIRES_SHARED(art::Locks::mutator_lock_)
1129 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1130 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS,
1131 nullptr,
1132 array,
1133 array->GetClass());
1134 if (stop_reports_) {
1135 return;
1136 }
1137
1138 if (array->IsObjectArray()) {
1139 art::mirror::ObjectArray<art::mirror::Object>* obj_array =
1140 array->AsObjectArray<art::mirror::Object>();
1141 int32_t length = obj_array->GetLength();
1142 for (int32_t i = 0; i != length; ++i) {
1143 art::mirror::Object* elem = obj_array->GetWithoutChecks(i);
1144 if (elem != nullptr) {
1145 jvmtiHeapReferenceInfo reference_info;
1146 reference_info.array.index = i;
1147 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
1148 &reference_info,
1149 array,
1150 elem);
1151 if (stop_reports_) {
1152 break;
1153 }
1154 }
1155 }
Andreas Gampebecd6ad2017-02-22 19:20:37 -08001156 } else {
1157 if (!stop_reports_) {
1158 jint array_ret = ReportPrimitiveArray(array, env, tag_table_, callbacks_, user_data_);
1159 stop_reports_ = (array_ret & JVMTI_VISIT_ABORT) != 0;
1160 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001161 }
1162 }
1163
1164 void VisitClass(art::mirror::Class* klass)
1165 REQUIRES_SHARED(art::Locks::mutator_lock_)
1166 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1167 // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones.
1168 if (!klass->IsResolved()) {
1169 return;
1170 }
1171
1172 // Superclass.
1173 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_SUPERCLASS,
1174 nullptr,
1175 klass,
1176 klass->GetSuperClass());
1177 if (stop_reports_) {
1178 return;
1179 }
1180
1181 // Directly implemented or extended interfaces.
1182 art::Thread* self = art::Thread::Current();
1183 art::StackHandleScope<1> hs(self);
1184 art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass));
1185 for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
1186 art::ObjPtr<art::mirror::Class> inf_klass =
Vladimir Marko19a4d372016-12-08 14:41:46 +00001187 art::mirror::Class::ResolveDirectInterface(self, h_klass, i);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001188 if (inf_klass == nullptr) {
1189 // TODO: With a resolved class this should not happen...
1190 self->ClearException();
1191 break;
1192 }
1193
1194 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_INTERFACE,
1195 nullptr,
1196 klass,
1197 inf_klass.Ptr());
1198 if (stop_reports_) {
1199 return;
1200 }
1201 }
1202
1203 // Classloader.
1204 // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the
1205 // fake BootClassLoader?
1206 if (klass->GetClassLoader() != nullptr) {
1207 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
1208 nullptr,
1209 klass,
1210 klass->GetClassLoader());
1211 if (stop_reports_) {
1212 return;
1213 }
1214 }
1215 DCHECK_EQ(h_klass.Get(), klass);
1216
1217 // Declared static fields.
Andreas Gampe95114602017-02-28 15:47:44 -08001218 auto report_static_field = [&](art::ObjPtr<art::mirror::Object> obj ATTRIBUTE_UNUSED,
1219 art::ObjPtr<art::mirror::Class> obj_klass,
1220 art::ArtField& field,
1221 size_t field_index,
1222 void* user_data ATTRIBUTE_UNUSED)
1223 REQUIRES_SHARED(art::Locks::mutator_lock_)
1224 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1225 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(obj_klass);
1226 if (field_value != nullptr) {
1227 jvmtiHeapReferenceInfo reference_info;
1228 memset(&reference_info, 0, sizeof(reference_info));
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001229
Andreas Gampe95114602017-02-28 15:47:44 -08001230 reference_info.field.index = static_cast<jint>(field_index);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001231
Andreas Gampe95114602017-02-28 15:47:44 -08001232 return !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
1233 &reference_info,
1234 obj_klass.Ptr(),
1235 field_value.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001236 }
Andreas Gampe95114602017-02-28 15:47:44 -08001237 return false;
1238 };
1239 stop_reports_ = FieldVisitor<void, false>::ReportFields(klass,
1240 nullptr,
1241 VisitorFalse<void>,
1242 report_static_field,
1243 VisitorFalse<void>,
1244 VisitorFalse<void>);
1245 if (stop_reports_) {
1246 return;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001247 }
Andreas Gampee7316932017-02-25 09:15:05 -08001248
Andreas Gampe95114602017-02-28 15:47:44 -08001249 stop_reports_ = ReportPrimitiveField::Report(klass, tag_table_, callbacks_, user_data_);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001250 }
1251
1252 void MaybeEnqueue(art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
1253 if (visited_.find(obj) == visited_.end()) {
1254 worklist_.push_back(obj);
1255 visited_.insert(obj);
1256 }
1257 }
1258
1259 bool ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,
1260 const jvmtiHeapReferenceInfo* reference_info,
1261 art::mirror::Object* referree,
1262 art::mirror::Object* referrer)
1263 REQUIRES_SHARED(art::Locks::mutator_lock_)
1264 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1265 jint result = ReportReference(kind, reference_info, referree, referrer);
1266 if ((result & JVMTI_VISIT_ABORT) == 0) {
1267 if ((result & JVMTI_VISIT_OBJECTS) != 0) {
1268 MaybeEnqueue(referrer);
1269 }
1270 return true;
1271 } else {
1272 return false;
1273 }
1274 }
1275
1276 jint ReportReference(jvmtiHeapReferenceKind kind,
1277 const jvmtiHeapReferenceInfo* reference_info,
1278 art::mirror::Object* referrer,
1279 art::mirror::Object* referree)
1280 REQUIRES_SHARED(art::Locks::mutator_lock_)
1281 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
1282 if (referree == nullptr || stop_reports_) {
1283 return 0;
1284 }
1285
Andreas Gampe38da9f22017-02-20 13:35:36 -08001286 if (UNLIKELY(class_filter_ != nullptr) && class_filter_ != referree->GetClass()) {
1287 return JVMTI_VISIT_OBJECTS;
1288 }
1289
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001290 const jlong class_tag = tag_table_->GetTagOrZero(referree->GetClass());
Andreas Gampe6ea06072017-02-24 18:01:19 +00001291 jlong tag = tag_table_->GetTagOrZero(referree);
1292
1293 if (!heap_filter_.ShouldReportByHeapFilter(tag, class_tag)) {
1294 return JVMTI_VISIT_OBJECTS;
1295 }
1296
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001297 const jlong referrer_class_tag =
1298 referrer == nullptr ? 0 : tag_table_->GetTagOrZero(referrer->GetClass());
1299 const jlong size = static_cast<jlong>(referree->SizeOf());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001300 jlong saved_tag = tag;
1301 jlong referrer_tag = 0;
1302 jlong saved_referrer_tag = 0;
1303 jlong* referrer_tag_ptr;
1304 if (referrer == nullptr) {
1305 referrer_tag_ptr = nullptr;
1306 } else {
1307 if (referrer == referree) {
1308 referrer_tag_ptr = &tag;
1309 } else {
1310 referrer_tag = saved_referrer_tag = tag_table_->GetTagOrZero(referrer);
1311 referrer_tag_ptr = &referrer_tag;
1312 }
1313 }
Andreas Gampe38da9f22017-02-20 13:35:36 -08001314
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001315 jint length = -1;
1316 if (referree->IsArrayInstance()) {
1317 length = referree->AsArray()->GetLength();
1318 }
1319
1320 jint result = callbacks_->heap_reference_callback(kind,
1321 reference_info,
1322 class_tag,
1323 referrer_class_tag,
1324 size,
1325 &tag,
1326 referrer_tag_ptr,
1327 length,
1328 const_cast<void*>(user_data_));
1329
1330 if (tag != saved_tag) {
1331 tag_table_->Set(referree, tag);
1332 }
1333 if (referrer_tag != saved_referrer_tag) {
1334 tag_table_->Set(referrer, referrer_tag);
1335 }
1336
1337 return result;
1338 }
1339
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001340 jvmtiEnv* env;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001341 ObjectTagTable* tag_table_;
Andreas Gampe638a6932016-12-02 19:11:17 -08001342 art::ObjPtr<art::mirror::Object> initial_object_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001343 const jvmtiHeapCallbacks* callbacks_;
Andreas Gampe38da9f22017-02-20 13:35:36 -08001344 art::ObjPtr<art::mirror::Class> class_filter_;
Andreas Gampe6ea06072017-02-24 18:01:19 +00001345 const HeapFilter heap_filter_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001346 const void* user_data_;
1347
1348 std::vector<art::mirror::Object*> worklist_;
1349 size_t start_;
1350 static constexpr size_t kMaxStart = 1000000U;
1351
1352 std::unordered_set<art::mirror::Object*> visited_;
1353
1354 bool stop_reports_;
1355
1356 friend class CollectAndReportRootsVisitor;
1357};
1358
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001359jvmtiError HeapUtil::FollowReferences(jvmtiEnv* env,
Andreas Gampe6ea06072017-02-24 18:01:19 +00001360 jint heap_filter,
Andreas Gampe38da9f22017-02-20 13:35:36 -08001361 jclass klass,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001362 jobject initial_object,
1363 const jvmtiHeapCallbacks* callbacks,
1364 const void* user_data) {
1365 if (callbacks == nullptr) {
1366 return ERR(NULL_POINTER);
1367 }
1368
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001369 art::Thread* self = art::Thread::Current();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001370
Andreas Gampe638a6932016-12-02 19:11:17 -08001371 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
1372 if (heap->IsGcConcurrentAndMoving()) {
1373 // Need to take a heap dump while GC isn't running. See the
1374 // comment in Heap::VisitObjects().
1375 heap->IncrementDisableMovingGC(self);
1376 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001377 {
Andreas Gampe638a6932016-12-02 19:11:17 -08001378 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001379 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
1380 art::ScopedSuspendAll ssa("FollowReferences");
1381
Andreas Gampe38da9f22017-02-20 13:35:36 -08001382 art::ObjPtr<art::mirror::Class> class_filter = klass == nullptr
1383 ? nullptr
1384 : art::ObjPtr<art::mirror::Class>::DownCast(self->DecodeJObject(klass));
Andreas Gampe638a6932016-12-02 19:11:17 -08001385 FollowReferencesHelper frh(this,
Andreas Gampe3ec8e402017-02-21 15:49:53 -08001386 env,
Andreas Gampe638a6932016-12-02 19:11:17 -08001387 self->DecodeJObject(initial_object),
1388 callbacks,
Andreas Gampe38da9f22017-02-20 13:35:36 -08001389 class_filter,
Andreas Gampe6ea06072017-02-24 18:01:19 +00001390 heap_filter,
Andreas Gampe638a6932016-12-02 19:11:17 -08001391 user_data);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001392 frh.Init();
1393 frh.Work();
1394 }
Andreas Gampe638a6932016-12-02 19:11:17 -08001395 if (heap->IsGcConcurrentAndMoving()) {
1396 heap->DecrementDisableMovingGC(self);
1397 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -07001398
1399 return ERR(NONE);
1400}
1401
Andreas Gampeaa8b60c2016-10-12 12:51:25 -07001402jvmtiError HeapUtil::GetLoadedClasses(jvmtiEnv* env,
1403 jint* class_count_ptr,
1404 jclass** classes_ptr) {
1405 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
1406 return ERR(NULL_POINTER);
1407 }
1408
1409 class ReportClassVisitor : public art::ClassVisitor {
1410 public:
1411 explicit ReportClassVisitor(art::Thread* self) : self_(self) {}
1412
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001413 bool operator()(art::ObjPtr<art::mirror::Class> klass)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001414 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightf8a4a2c2017-06-22 08:43:37 -07001415 if (klass->IsLoaded() || klass->IsErroneous()) {
1416 classes_.push_back(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
1417 }
Andreas Gampeaa8b60c2016-10-12 12:51:25 -07001418 return true;
1419 }
1420
1421 art::Thread* self_;
1422 std::vector<jclass> classes_;
1423 };
1424
1425 art::Thread* self = art::Thread::Current();
1426 ReportClassVisitor rcv(self);
1427 {
1428 art::ScopedObjectAccess soa(self);
1429 art::Runtime::Current()->GetClassLinker()->VisitClasses(&rcv);
1430 }
1431
1432 size_t size = rcv.classes_.size();
1433 jclass* classes = nullptr;
1434 jvmtiError alloc_ret = env->Allocate(static_cast<jlong>(size * sizeof(jclass)),
1435 reinterpret_cast<unsigned char**>(&classes));
1436 if (alloc_ret != ERR(NONE)) {
1437 return alloc_ret;
1438 }
1439
1440 for (size_t i = 0; i < size; ++i) {
1441 classes[i] = rcv.classes_[i];
1442 }
1443 *classes_ptr = classes;
1444 *class_count_ptr = static_cast<jint>(size);
1445
1446 return ERR(NONE);
1447}
1448
Andreas Gampe8da6d032016-10-31 19:31:03 -07001449jvmtiError HeapUtil::ForceGarbageCollection(jvmtiEnv* env ATTRIBUTE_UNUSED) {
Roland Levillainaf290312018-02-27 20:02:17 +00001450 art::Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ false);
Andreas Gampe8da6d032016-10-31 19:31:03 -07001451
1452 return ERR(NONE);
1453}
Andreas Gamped73aba42017-05-03 21:40:26 -07001454
1455static constexpr jint kHeapIdDefault = 0;
1456static constexpr jint kHeapIdImage = 1;
1457static constexpr jint kHeapIdZygote = 2;
1458static constexpr jint kHeapIdApp = 3;
1459
Andreas Gampe2eb25e42017-05-09 17:14:58 -07001460static jint GetHeapId(art::ObjPtr<art::mirror::Object> obj)
1461 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1462 if (obj == nullptr) {
1463 return -1;
1464 }
1465
1466 art::gc::Heap* const heap = art::Runtime::Current()->GetHeap();
1467 const art::gc::space::ContinuousSpace* const space =
1468 heap->FindContinuousSpaceFromObject(obj, true);
1469 jint heap_type = kHeapIdApp;
1470 if (space != nullptr) {
1471 if (space->IsZygoteSpace()) {
1472 heap_type = kHeapIdZygote;
1473 } else if (space->IsImageSpace() && heap->ObjectIsInBootImageSpace(obj)) {
1474 // Only count objects in the boot image as HPROF_HEAP_IMAGE, this leaves app image objects
1475 // as HPROF_HEAP_APP. b/35762934
1476 heap_type = kHeapIdImage;
1477 }
1478 } else {
1479 const auto* los = heap->GetLargeObjectsSpace();
1480 if (los->Contains(obj.Ptr()) && los->IsZygoteLargeObject(art::Thread::Current(), obj.Ptr())) {
1481 heap_type = kHeapIdZygote;
1482 }
1483 }
1484 return heap_type;
1485};
1486
Andreas Gamped73aba42017-05-03 21:40:26 -07001487jvmtiError HeapExtensions::GetObjectHeapId(jvmtiEnv* env, jlong tag, jint* heap_id, ...) {
1488 if (heap_id == nullptr) {
1489 return ERR(NULL_POINTER);
1490 }
1491
1492 art::Thread* self = art::Thread::Current();
1493
1494 auto work = [&]() REQUIRES_SHARED(art::Locks::mutator_lock_) {
1495 ObjectTagTable* tag_table = ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get();
1496 art::ObjPtr<art::mirror::Object> obj = tag_table->Find(tag);
Andreas Gampe2eb25e42017-05-09 17:14:58 -07001497 jint heap_type = GetHeapId(obj);
1498 if (heap_type == -1) {
Andreas Gamped73aba42017-05-03 21:40:26 -07001499 return ERR(NOT_FOUND);
1500 }
Andreas Gamped73aba42017-05-03 21:40:26 -07001501 *heap_id = heap_type;
1502 return ERR(NONE);
1503 };
1504
1505 if (!art::Locks::mutator_lock_->IsSharedHeld(self)) {
1506 if (!self->IsThreadSuspensionAllowable()) {
1507 return ERR(INTERNAL);
1508 }
1509 art::ScopedObjectAccess soa(self);
1510 return work();
1511 } else {
1512 // We cannot use SOA in this case. We might be holding the lock, but may not be in the
1513 // runnable state (e.g., during GC).
1514 art::Locks::mutator_lock_->AssertSharedHeld(self);
1515 // TODO: Investigate why ASSERT_SHARED_CAPABILITY doesn't work.
1516 auto annotalysis_workaround = [&]() NO_THREAD_SAFETY_ANALYSIS {
1517 return work();
1518 };
1519 return annotalysis_workaround();
1520 }
1521}
1522
1523static jvmtiError CopyStringAndReturn(jvmtiEnv* env, const char* in, char** out) {
1524 jvmtiError error;
1525 JvmtiUniquePtr<char[]> param_name = CopyString(env, in, &error);
1526 if (param_name == nullptr) {
1527 return error;
1528 }
1529 *out = param_name.release();
1530 return ERR(NONE);
1531}
1532
1533static constexpr const char* kHeapIdDefaultName = "default";
1534static constexpr const char* kHeapIdImageName = "image";
1535static constexpr const char* kHeapIdZygoteName = "zygote";
1536static constexpr const char* kHeapIdAppName = "app";
1537
1538jvmtiError HeapExtensions::GetHeapName(jvmtiEnv* env, jint heap_id, char** heap_name, ...) {
1539 switch (heap_id) {
1540 case kHeapIdDefault:
1541 return CopyStringAndReturn(env, kHeapIdDefaultName, heap_name);
1542 case kHeapIdImage:
1543 return CopyStringAndReturn(env, kHeapIdImageName, heap_name);
1544 case kHeapIdZygote:
1545 return CopyStringAndReturn(env, kHeapIdZygoteName, heap_name);
1546 case kHeapIdApp:
1547 return CopyStringAndReturn(env, kHeapIdAppName, heap_name);
1548
1549 default:
1550 return ERR(ILLEGAL_ARGUMENT);
1551 }
1552}
1553
Andreas Gampe2eb25e42017-05-09 17:14:58 -07001554jvmtiError HeapExtensions::IterateThroughHeapExt(jvmtiEnv* env,
1555 jint heap_filter,
1556 jclass klass,
1557 const jvmtiHeapCallbacks* callbacks,
1558 const void* user_data) {
1559 if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.can_tag_objects != 1) { \
1560 return ERR(MUST_POSSESS_CAPABILITY); \
1561 }
1562
1563 // ART extension API: Also pass the heap id.
1564 auto ArtIterateHeap = [](art::mirror::Object* obj,
1565 const jvmtiHeapCallbacks* cb_callbacks,
1566 jlong class_tag,
1567 jlong size,
1568 jlong* tag,
1569 jint length,
1570 void* cb_user_data)
1571 REQUIRES_SHARED(art::Locks::mutator_lock_) {
1572 jint heap_id = GetHeapId(obj);
1573 using ArtExtensionAPI = jint (*)(jlong, jlong, jlong*, jint length, void*, jint);
1574 return reinterpret_cast<ArtExtensionAPI>(cb_callbacks->heap_iteration_callback)(
1575 class_tag, size, tag, length, cb_user_data, heap_id);
1576 };
1577 return DoIterateThroughHeap(ArtIterateHeap,
1578 env,
1579 ArtJvmTiEnv::AsArtJvmTiEnv(env)->object_tag_table.get(),
1580 heap_filter,
1581 klass,
1582 callbacks,
1583 user_data);
1584}
1585
Andreas Gampee54d9922016-10-11 19:55:37 -07001586} // namespace openjdkjvmti