blob: 0eff4698846a025313e918827ab6fe968f5a380f [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 Gampee54d9922016-10-11 19:55:37 -070028#include "mirror/class.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070031#include "object_callbacks.h"
32#include "object_tagging.h"
33#include "obj_ptr-inl.h"
34#include "runtime.h"
35#include "scoped_thread_state_change-inl.h"
36#include "thread-inl.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070037#include "thread_list.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070038
39namespace openjdkjvmti {
40
41struct IterateThroughHeapData {
42 IterateThroughHeapData(HeapUtil* _heap_util,
43 jint heap_filter,
44 art::ObjPtr<art::mirror::Class> klass,
45 const jvmtiHeapCallbacks* _callbacks,
46 const void* _user_data)
47 : heap_util(_heap_util),
48 filter_klass(klass),
49 callbacks(_callbacks),
50 user_data(_user_data),
51 filter_out_tagged((heap_filter & JVMTI_HEAP_FILTER_TAGGED) != 0),
52 filter_out_untagged((heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) != 0),
53 filter_out_class_tagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) != 0),
54 filter_out_class_untagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) != 0),
55 any_filter(filter_out_tagged ||
56 filter_out_untagged ||
57 filter_out_class_tagged ||
58 filter_out_class_untagged),
59 stop_reports(false) {
60 }
61
62 bool ShouldReportByHeapFilter(jlong tag, jlong class_tag) {
63 if (!any_filter) {
64 return true;
65 }
66
67 if ((tag == 0 && filter_out_untagged) || (tag != 0 && filter_out_tagged)) {
68 return false;
69 }
70
71 if ((class_tag == 0 && filter_out_class_untagged) ||
72 (class_tag != 0 && filter_out_class_tagged)) {
73 return false;
74 }
75
76 return true;
77 }
78
79 HeapUtil* heap_util;
80 art::ObjPtr<art::mirror::Class> filter_klass;
81 const jvmtiHeapCallbacks* callbacks;
82 const void* user_data;
83 const bool filter_out_tagged;
84 const bool filter_out_untagged;
85 const bool filter_out_class_tagged;
86 const bool filter_out_class_untagged;
87 const bool any_filter;
88
89 bool stop_reports;
90};
91
92static void IterateThroughHeapObjectCallback(art::mirror::Object* obj, void* arg)
93 REQUIRES_SHARED(art::Locks::mutator_lock_) {
94 IterateThroughHeapData* ithd = reinterpret_cast<IterateThroughHeapData*>(arg);
95 // Early return, as we can't really stop visiting.
96 if (ithd->stop_reports) {
97 return;
98 }
99
100 art::ScopedAssertNoThreadSuspension no_suspension("IterateThroughHeapCallback");
101
102 jlong tag = 0;
103 ithd->heap_util->GetTags()->GetTag(obj, &tag);
104
105 jlong class_tag = 0;
106 art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
107 ithd->heap_util->GetTags()->GetTag(klass.Ptr(), &class_tag);
108 // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
109
110 if (!ithd->ShouldReportByHeapFilter(tag, class_tag)) {
111 return;
112 }
113
114 // TODO: Handle array_primitive_value_callback.
115
116 if (ithd->filter_klass != nullptr) {
117 if (ithd->filter_klass != klass) {
118 return;
119 }
120 }
121
122 jlong size = obj->SizeOf();
123
124 jint length = -1;
125 if (obj->IsArrayInstance()) {
126 length = obj->AsArray()->GetLength();
127 }
128
129 jlong saved_tag = tag;
130 jint ret = ithd->callbacks->heap_iteration_callback(class_tag,
131 size,
132 &tag,
133 length,
134 const_cast<void*>(ithd->user_data));
135
136 if (tag != saved_tag) {
137 ithd->heap_util->GetTags()->Set(obj, tag);
138 }
139
140 ithd->stop_reports = (ret & JVMTI_VISIT_ABORT) != 0;
141
142 // TODO Implement array primitive and string primitive callback.
143 // TODO Implement primitive field callback.
144}
145
146jvmtiError HeapUtil::IterateThroughHeap(jvmtiEnv* env ATTRIBUTE_UNUSED,
147 jint heap_filter,
148 jclass klass,
149 const jvmtiHeapCallbacks* callbacks,
150 const void* user_data) {
151 if (callbacks == nullptr) {
152 return ERR(NULL_POINTER);
153 }
154
155 if (callbacks->array_primitive_value_callback != nullptr) {
156 // TODO: Implement.
157 return ERR(NOT_IMPLEMENTED);
158 }
159
160 art::Thread* self = art::Thread::Current();
161 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
162
163 IterateThroughHeapData ithd(this,
164 heap_filter,
165 soa.Decode<art::mirror::Class>(klass),
166 callbacks,
167 user_data);
168
169 art::Runtime::Current()->GetHeap()->VisitObjects(IterateThroughHeapObjectCallback, &ithd);
170
171 return ERR(NONE);
172}
173
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700174class FollowReferencesHelper FINAL {
175 public:
176 FollowReferencesHelper(HeapUtil* h,
177 art::ObjPtr<art::mirror::Object> initial_object ATTRIBUTE_UNUSED,
178 const jvmtiHeapCallbacks* callbacks,
179 const void* user_data)
180 : tag_table_(h->GetTags()),
181 callbacks_(callbacks),
182 user_data_(user_data),
183 start_(0),
184 stop_reports_(false) {
185 }
186
187 void Init()
188 REQUIRES_SHARED(art::Locks::mutator_lock_)
189 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
190 CollectAndReportRootsVisitor carrv(this, tag_table_, &worklist_, &visited_);
191 art::Runtime::Current()->VisitRoots(&carrv);
192 art::Runtime::Current()->VisitImageRoots(&carrv);
193 stop_reports_ = carrv.IsStopReports();
194
195 if (stop_reports_) {
196 worklist_.clear();
197 }
198 }
199
200 void Work()
201 REQUIRES_SHARED(art::Locks::mutator_lock_)
202 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
203 // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately
204 // from the head of the work list, instead postponing until there's a gap that's "large."
205 //
206 // Alternatively, we can implement a DFS and use the work list as a stack.
207 while (start_ < worklist_.size()) {
208 art::mirror::Object* cur_obj = worklist_[start_];
209 start_++;
210
211 if (start_ >= kMaxStart) {
212 worklist_.erase(worklist_.begin(), worklist_.begin() + start_);
213 start_ = 0;
214 }
215
216 VisitObject(cur_obj);
217
218 if (stop_reports_) {
219 break;
220 }
221 }
222 }
223
224 private:
225 class CollectAndReportRootsVisitor FINAL : public art::RootVisitor {
226 public:
227 CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
228 ObjectTagTable* tag_table,
229 std::vector<art::mirror::Object*>* worklist,
230 std::unordered_set<art::mirror::Object*>* visited)
231 : helper_(helper),
232 tag_table_(tag_table),
233 worklist_(worklist),
234 visited_(visited),
235 stop_reports_(false) {}
236
237 void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
238 OVERRIDE
239 REQUIRES_SHARED(art::Locks::mutator_lock_)
240 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
241 for (size_t i = 0; i != count; ++i) {
242 AddRoot(*roots[i], info);
243 }
244 }
245
246 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
247 size_t count,
248 const art::RootInfo& info)
249 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_)
250 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
251 for (size_t i = 0; i != count; ++i) {
252 AddRoot(roots[i]->AsMirrorPtr(), info);
253 }
254 }
255
256 bool IsStopReports() {
257 return stop_reports_;
258 }
259
260 private:
261 void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
262 REQUIRES_SHARED(art::Locks::mutator_lock_)
263 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
264 // We use visited_ to mark roots already so we do not need another set.
265 if (visited_->find(root_obj) == visited_->end()) {
266 visited_->insert(root_obj);
267 worklist_->push_back(root_obj);
268 }
269 ReportRoot(root_obj, info);
270 }
271
272 jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
273 jvmtiHeapReferenceInfo* ref_info)
274 REQUIRES_SHARED(art::Locks::mutator_lock_) {
275 // TODO: Fill in ref_info.
276 memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
277
278 switch (info.GetType()) {
279 case art::RootType::kRootJNIGlobal:
280 return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
281
282 case art::RootType::kRootJNILocal:
283 return JVMTI_HEAP_REFERENCE_JNI_LOCAL;
284
285 case art::RootType::kRootJavaFrame:
286 return JVMTI_HEAP_REFERENCE_STACK_LOCAL;
287
288 case art::RootType::kRootNativeStack:
289 case art::RootType::kRootThreadBlock:
290 case art::RootType::kRootThreadObject:
291 return JVMTI_HEAP_REFERENCE_THREAD;
292
293 case art::RootType::kRootStickyClass:
294 case art::RootType::kRootInternedString:
295 // Note: this isn't a root in the RI.
296 return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
297
298 case art::RootType::kRootMonitorUsed:
299 case art::RootType::kRootJNIMonitor:
300 return JVMTI_HEAP_REFERENCE_MONITOR;
301
302 case art::RootType::kRootFinalizing:
303 case art::RootType::kRootDebugger:
304 case art::RootType::kRootReferenceCleanup:
305 case art::RootType::kRootVMInternal:
306 case art::RootType::kRootUnknown:
307 return JVMTI_HEAP_REFERENCE_OTHER;
308 }
309 LOG(FATAL) << "Unreachable";
310 UNREACHABLE();
311 }
312
313 void ReportRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
314 REQUIRES_SHARED(art::Locks::mutator_lock_)
315 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
316 jvmtiHeapReferenceInfo ref_info;
317 jvmtiHeapReferenceKind kind = GetReferenceKind(info, &ref_info);
318 jint result = helper_->ReportReference(kind, &ref_info, nullptr, root_obj);
319 if ((result & JVMTI_VISIT_ABORT) != 0) {
320 stop_reports_ = true;
321 }
322 }
323
324 private:
325 FollowReferencesHelper* helper_;
326 ObjectTagTable* tag_table_;
327 std::vector<art::mirror::Object*>* worklist_;
328 std::unordered_set<art::mirror::Object*>* visited_;
329 bool stop_reports_;
330 };
331
332 void VisitObject(art::mirror::Object* obj)
333 REQUIRES_SHARED(art::Locks::mutator_lock_)
334 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
335 if (obj->IsClass()) {
336 VisitClass(obj->AsClass());
337 return;
338 }
339 if (obj->IsArrayInstance()) {
340 VisitArray(obj);
341 return;
342 }
343
344 // TODO: We'll probably have to rewrite this completely with our own visiting logic, if we
345 // want to have a chance of getting the field indices computed halfway efficiently. For
346 // now, ignore them altogether.
347
348 struct InstanceReferenceVisitor {
349 explicit InstanceReferenceVisitor(FollowReferencesHelper* helper_)
350 : helper(helper_), stop_reports(false) {}
351
352 void operator()(art::mirror::Object* src,
353 art::MemberOffset field_offset,
354 bool is_static ATTRIBUTE_UNUSED) const
355 REQUIRES_SHARED(art::Locks::mutator_lock_)
356 REQUIRES(!*helper->tag_table_->GetAllowDisallowLock()) {
357 if (stop_reports) {
358 return;
359 }
360
361 art::mirror::Object* trg = src->GetFieldObjectReferenceAddr(field_offset)->AsMirrorPtr();
362 jvmtiHeapReferenceInfo reference_info;
363 memset(&reference_info, 0, sizeof(reference_info));
364
365 // TODO: Implement spec-compliant numbering.
366 reference_info.field.index = field_offset.Int32Value();
367
368 jvmtiHeapReferenceKind kind =
369 field_offset.Int32Value() == art::mirror::Object::ClassOffset().Int32Value()
370 ? JVMTI_HEAP_REFERENCE_CLASS
371 : JVMTI_HEAP_REFERENCE_FIELD;
372 const jvmtiHeapReferenceInfo* reference_info_ptr =
373 kind == JVMTI_HEAP_REFERENCE_CLASS ? nullptr : &reference_info;
374
375 stop_reports = !helper->ReportReferenceMaybeEnqueue(kind, reference_info_ptr, src, trg);
376 }
377
378 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
379 const {
380 LOG(FATAL) << "Unreachable";
381 }
382 void VisitRootIfNonNull(
383 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
384 LOG(FATAL) << "Unreachable";
385 }
386
387 // "mutable" required by the visitor API.
388 mutable FollowReferencesHelper* helper;
389 mutable bool stop_reports;
390 };
391
392 InstanceReferenceVisitor visitor(this);
393 // Visit references, not native roots.
394 obj->VisitReferences<false>(visitor, art::VoidFunctor());
395
396 stop_reports_ = visitor.stop_reports;
397 }
398
399 void VisitArray(art::mirror::Object* array)
400 REQUIRES_SHARED(art::Locks::mutator_lock_)
401 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
402 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS,
403 nullptr,
404 array,
405 array->GetClass());
406 if (stop_reports_) {
407 return;
408 }
409
410 if (array->IsObjectArray()) {
411 art::mirror::ObjectArray<art::mirror::Object>* obj_array =
412 array->AsObjectArray<art::mirror::Object>();
413 int32_t length = obj_array->GetLength();
414 for (int32_t i = 0; i != length; ++i) {
415 art::mirror::Object* elem = obj_array->GetWithoutChecks(i);
416 if (elem != nullptr) {
417 jvmtiHeapReferenceInfo reference_info;
418 reference_info.array.index = i;
419 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
420 &reference_info,
421 array,
422 elem);
423 if (stop_reports_) {
424 break;
425 }
426 }
427 }
428 }
429 }
430
431 void VisitClass(art::mirror::Class* klass)
432 REQUIRES_SHARED(art::Locks::mutator_lock_)
433 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
434 // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones.
435 if (!klass->IsResolved()) {
436 return;
437 }
438
439 // Superclass.
440 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_SUPERCLASS,
441 nullptr,
442 klass,
443 klass->GetSuperClass());
444 if (stop_reports_) {
445 return;
446 }
447
448 // Directly implemented or extended interfaces.
449 art::Thread* self = art::Thread::Current();
450 art::StackHandleScope<1> hs(self);
451 art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass));
452 for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
453 art::ObjPtr<art::mirror::Class> inf_klass =
454 art::mirror::Class::GetDirectInterface(self, h_klass, i);
455 if (inf_klass == nullptr) {
456 // TODO: With a resolved class this should not happen...
457 self->ClearException();
458 break;
459 }
460
461 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_INTERFACE,
462 nullptr,
463 klass,
464 inf_klass.Ptr());
465 if (stop_reports_) {
466 return;
467 }
468 }
469
470 // Classloader.
471 // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the
472 // fake BootClassLoader?
473 if (klass->GetClassLoader() != nullptr) {
474 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
475 nullptr,
476 klass,
477 klass->GetClassLoader());
478 if (stop_reports_) {
479 return;
480 }
481 }
482 DCHECK_EQ(h_klass.Get(), klass);
483
484 // Declared static fields.
485 for (auto& field : klass->GetSFields()) {
486 if (!field.IsPrimitiveType()) {
487 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(klass);
488 if (field_value != nullptr) {
489 jvmtiHeapReferenceInfo reference_info;
490 memset(&reference_info, 0, sizeof(reference_info));
491
492 // TODO: Implement spec-compliant numbering.
493 reference_info.field.index = field.GetOffset().Int32Value();
494
495 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
496 &reference_info,
497 klass,
498 field_value.Ptr());
499 if (stop_reports_) {
500 return;
501 }
502 }
503 }
504 }
505 }
506
507 void MaybeEnqueue(art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
508 if (visited_.find(obj) == visited_.end()) {
509 worklist_.push_back(obj);
510 visited_.insert(obj);
511 }
512 }
513
514 bool ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,
515 const jvmtiHeapReferenceInfo* reference_info,
516 art::mirror::Object* referree,
517 art::mirror::Object* referrer)
518 REQUIRES_SHARED(art::Locks::mutator_lock_)
519 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
520 jint result = ReportReference(kind, reference_info, referree, referrer);
521 if ((result & JVMTI_VISIT_ABORT) == 0) {
522 if ((result & JVMTI_VISIT_OBJECTS) != 0) {
523 MaybeEnqueue(referrer);
524 }
525 return true;
526 } else {
527 return false;
528 }
529 }
530
531 jint ReportReference(jvmtiHeapReferenceKind kind,
532 const jvmtiHeapReferenceInfo* reference_info,
533 art::mirror::Object* referrer,
534 art::mirror::Object* referree)
535 REQUIRES_SHARED(art::Locks::mutator_lock_)
536 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
537 if (referree == nullptr || stop_reports_) {
538 return 0;
539 }
540
541 const jlong class_tag = tag_table_->GetTagOrZero(referree->GetClass());
542 const jlong referrer_class_tag =
543 referrer == nullptr ? 0 : tag_table_->GetTagOrZero(referrer->GetClass());
544 const jlong size = static_cast<jlong>(referree->SizeOf());
545 jlong tag = tag_table_->GetTagOrZero(referree);
546 jlong saved_tag = tag;
547 jlong referrer_tag = 0;
548 jlong saved_referrer_tag = 0;
549 jlong* referrer_tag_ptr;
550 if (referrer == nullptr) {
551 referrer_tag_ptr = nullptr;
552 } else {
553 if (referrer == referree) {
554 referrer_tag_ptr = &tag;
555 } else {
556 referrer_tag = saved_referrer_tag = tag_table_->GetTagOrZero(referrer);
557 referrer_tag_ptr = &referrer_tag;
558 }
559 }
560 jint length = -1;
561 if (referree->IsArrayInstance()) {
562 length = referree->AsArray()->GetLength();
563 }
564
565 jint result = callbacks_->heap_reference_callback(kind,
566 reference_info,
567 class_tag,
568 referrer_class_tag,
569 size,
570 &tag,
571 referrer_tag_ptr,
572 length,
573 const_cast<void*>(user_data_));
574
575 if (tag != saved_tag) {
576 tag_table_->Set(referree, tag);
577 }
578 if (referrer_tag != saved_referrer_tag) {
579 tag_table_->Set(referrer, referrer_tag);
580 }
581
582 return result;
583 }
584
585 ObjectTagTable* tag_table_;
586 const jvmtiHeapCallbacks* callbacks_;
587 const void* user_data_;
588
589 std::vector<art::mirror::Object*> worklist_;
590 size_t start_;
591 static constexpr size_t kMaxStart = 1000000U;
592
593 std::unordered_set<art::mirror::Object*> visited_;
594
595 bool stop_reports_;
596
597 friend class CollectAndReportRootsVisitor;
598};
599
600jvmtiError HeapUtil::FollowReferences(jvmtiEnv* env ATTRIBUTE_UNUSED,
601 jint heap_filter ATTRIBUTE_UNUSED,
602 jclass klass ATTRIBUTE_UNUSED,
603 jobject initial_object,
604 const jvmtiHeapCallbacks* callbacks,
605 const void* user_data) {
606 if (callbacks == nullptr) {
607 return ERR(NULL_POINTER);
608 }
609
610 if (callbacks->array_primitive_value_callback != nullptr) {
611 // TODO: Implement.
612 return ERR(NOT_IMPLEMENTED);
613 }
614
615 art::Thread* self = art::Thread::Current();
616 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
617
618 art::Runtime::Current()->GetHeap()->IncrementDisableMovingGC(self);
619 {
620 art::ObjPtr<art::mirror::Object> o_initial = soa.Decode<art::mirror::Object>(initial_object);
621
622 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
623 art::ScopedSuspendAll ssa("FollowReferences");
624
625 FollowReferencesHelper frh(this, o_initial, callbacks, user_data);
626 frh.Init();
627 frh.Work();
628 }
629 art::Runtime::Current()->GetHeap()->DecrementDisableMovingGC(self);
630
631 return ERR(NONE);
632}
633
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700634jvmtiError HeapUtil::GetLoadedClasses(jvmtiEnv* env,
635 jint* class_count_ptr,
636 jclass** classes_ptr) {
637 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
638 return ERR(NULL_POINTER);
639 }
640
641 class ReportClassVisitor : public art::ClassVisitor {
642 public:
643 explicit ReportClassVisitor(art::Thread* self) : self_(self) {}
644
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700645 bool operator()(art::ObjPtr<art::mirror::Class> klass)
646 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampeef54d8d2016-10-25 09:55:53 -0700647 classes_.push_back(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700648 return true;
649 }
650
651 art::Thread* self_;
652 std::vector<jclass> classes_;
653 };
654
655 art::Thread* self = art::Thread::Current();
656 ReportClassVisitor rcv(self);
657 {
658 art::ScopedObjectAccess soa(self);
659 art::Runtime::Current()->GetClassLinker()->VisitClasses(&rcv);
660 }
661
662 size_t size = rcv.classes_.size();
663 jclass* classes = nullptr;
664 jvmtiError alloc_ret = env->Allocate(static_cast<jlong>(size * sizeof(jclass)),
665 reinterpret_cast<unsigned char**>(&classes));
666 if (alloc_ret != ERR(NONE)) {
667 return alloc_ret;
668 }
669
670 for (size_t i = 0; i < size; ++i) {
671 classes[i] = rcv.classes_[i];
672 }
673 *classes_ptr = classes;
674 *class_count_ptr = static_cast<jint>(size);
675
676 return ERR(NONE);
677}
678
Andreas Gampe8da6d032016-10-31 19:31:03 -0700679jvmtiError HeapUtil::ForceGarbageCollection(jvmtiEnv* env ATTRIBUTE_UNUSED) {
680 art::Runtime::Current()->GetHeap()->CollectGarbage(false);
681
682 return ERR(NONE);
683}
Andreas Gampee54d9922016-10-11 19:55:37 -0700684} // namespace openjdkjvmti