blob: 2fbc12bb38650a89dc6833ca5bd8f9f4b9438994 [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"
Andreas Gampebecd6ad2017-02-22 19:20:37 -080034#include "primitive.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070035#include "runtime.h"
36#include "scoped_thread_state_change-inl.h"
37#include "thread-inl.h"
Andreas Gampe70bfc8a2016-11-03 11:04:15 -070038#include "thread_list.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070039
40namespace openjdkjvmti {
41
Andreas Gampe3ec8e402017-02-21 15:49:53 -080042namespace {
43
44// Report the contents of a string, if a callback is set.
45jint ReportString(art::ObjPtr<art::mirror::Object> obj,
46 jvmtiEnv* env,
47 ObjectTagTable* tag_table,
48 const jvmtiHeapCallbacks* cb,
49 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
50 if (UNLIKELY(cb->string_primitive_value_callback != nullptr) && obj->IsString()) {
51 art::ObjPtr<art::mirror::String> str = obj->AsString();
52 int32_t string_length = str->GetLength();
53 jvmtiError alloc_error;
54 JvmtiUniquePtr<uint16_t[]> data = AllocJvmtiUniquePtr<uint16_t[]>(env,
55 string_length,
56 &alloc_error);
57 if (data == nullptr) {
58 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
59 // back? For now just warn.
60 LOG(WARNING) << "Unable to allocate buffer for string reporting! Silently dropping value.";
61 return 0;
62 }
63
64 if (str->IsCompressed()) {
65 uint8_t* compressed_data = str->GetValueCompressed();
66 for (int32_t i = 0; i != string_length; ++i) {
67 data[i] = compressed_data[i];
68 }
69 } else {
70 // Can copy directly.
71 memcpy(data.get(), str->GetValue(), string_length * sizeof(uint16_t));
72 }
73
74 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
75 jlong string_tag = tag_table->GetTagOrZero(obj.Ptr());
76 const jlong saved_string_tag = string_tag;
77
78 jint result = cb->string_primitive_value_callback(class_tag,
79 obj->SizeOf(),
80 &string_tag,
81 data.get(),
82 string_length,
83 const_cast<void*>(user_data));
84 if (string_tag != saved_string_tag) {
85 tag_table->Set(obj.Ptr(), string_tag);
86 }
87
88 return result;
89 }
90 return 0;
91}
92
Andreas Gampebecd6ad2017-02-22 19:20:37 -080093// Report the contents of a primitive array, if a callback is set.
94jint ReportPrimitiveArray(art::ObjPtr<art::mirror::Object> obj,
95 jvmtiEnv* env,
96 ObjectTagTable* tag_table,
97 const jvmtiHeapCallbacks* cb,
98 const void* user_data) REQUIRES_SHARED(art::Locks::mutator_lock_) {
99 if (UNLIKELY(cb->array_primitive_value_callback != nullptr) &&
100 obj->IsArrayInstance() &&
101 !obj->IsObjectArray()) {
102 art::ObjPtr<art::mirror::Array> array = obj->AsArray();
103 int32_t array_length = array->GetLength();
104 size_t component_size = array->GetClass()->GetComponentSize();
105 art::Primitive::Type art_prim_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
106 jvmtiPrimitiveType prim_type =
107 static_cast<jvmtiPrimitiveType>(art::Primitive::Descriptor(art_prim_type)[0]);
108 DCHECK(prim_type == JVMTI_PRIMITIVE_TYPE_BOOLEAN ||
109 prim_type == JVMTI_PRIMITIVE_TYPE_BYTE ||
110 prim_type == JVMTI_PRIMITIVE_TYPE_CHAR ||
111 prim_type == JVMTI_PRIMITIVE_TYPE_SHORT ||
112 prim_type == JVMTI_PRIMITIVE_TYPE_INT ||
113 prim_type == JVMTI_PRIMITIVE_TYPE_LONG ||
114 prim_type == JVMTI_PRIMITIVE_TYPE_FLOAT ||
115 prim_type == JVMTI_PRIMITIVE_TYPE_DOUBLE);
116
117 const jlong class_tag = tag_table->GetTagOrZero(obj->GetClass());
118 jlong array_tag = tag_table->GetTagOrZero(obj.Ptr());
119 const jlong saved_array_tag = array_tag;
120
121 jint result;
122 if (array_length == 0) {
123 result = cb->array_primitive_value_callback(class_tag,
124 obj->SizeOf(),
125 &array_tag,
126 0,
127 prim_type,
128 nullptr,
129 const_cast<void*>(user_data));
130 } else {
131 jvmtiError alloc_error;
132 JvmtiUniquePtr<char[]> data = AllocJvmtiUniquePtr<char[]>(env,
133 array_length * component_size,
134 &alloc_error);
135 if (data == nullptr) {
136 // TODO: Not really sure what to do here. Should we abort the iteration and go all the way
137 // back? For now just warn.
138 LOG(WARNING) << "Unable to allocate buffer for array reporting! Silently dropping value.";
139 return 0;
140 }
141
142 memcpy(data.get(), array->GetRawData(component_size, 0), array_length * component_size);
143
144 result = cb->array_primitive_value_callback(class_tag,
145 obj->SizeOf(),
146 &array_tag,
147 array_length,
148 prim_type,
149 data.get(),
150 const_cast<void*>(user_data));
151 }
152
153 if (array_tag != saved_array_tag) {
154 tag_table->Set(obj.Ptr(), array_tag);
155 }
156
157 return result;
158 }
159 return 0;
160}
161
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800162} // namespace
163
Andreas Gampee54d9922016-10-11 19:55:37 -0700164struct IterateThroughHeapData {
165 IterateThroughHeapData(HeapUtil* _heap_util,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800166 jvmtiEnv* _env,
Andreas Gampee54d9922016-10-11 19:55:37 -0700167 jint heap_filter,
168 art::ObjPtr<art::mirror::Class> klass,
169 const jvmtiHeapCallbacks* _callbacks,
170 const void* _user_data)
171 : heap_util(_heap_util),
172 filter_klass(klass),
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800173 env(_env),
Andreas Gampee54d9922016-10-11 19:55:37 -0700174 callbacks(_callbacks),
175 user_data(_user_data),
176 filter_out_tagged((heap_filter & JVMTI_HEAP_FILTER_TAGGED) != 0),
177 filter_out_untagged((heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) != 0),
178 filter_out_class_tagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) != 0),
179 filter_out_class_untagged((heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) != 0),
180 any_filter(filter_out_tagged ||
181 filter_out_untagged ||
182 filter_out_class_tagged ||
183 filter_out_class_untagged),
184 stop_reports(false) {
185 }
186
187 bool ShouldReportByHeapFilter(jlong tag, jlong class_tag) {
188 if (!any_filter) {
189 return true;
190 }
191
192 if ((tag == 0 && filter_out_untagged) || (tag != 0 && filter_out_tagged)) {
193 return false;
194 }
195
196 if ((class_tag == 0 && filter_out_class_untagged) ||
197 (class_tag != 0 && filter_out_class_tagged)) {
198 return false;
199 }
200
201 return true;
202 }
203
204 HeapUtil* heap_util;
205 art::ObjPtr<art::mirror::Class> filter_klass;
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800206 jvmtiEnv* env;
Andreas Gampee54d9922016-10-11 19:55:37 -0700207 const jvmtiHeapCallbacks* callbacks;
208 const void* user_data;
209 const bool filter_out_tagged;
210 const bool filter_out_untagged;
211 const bool filter_out_class_tagged;
212 const bool filter_out_class_untagged;
213 const bool any_filter;
214
215 bool stop_reports;
216};
217
218static void IterateThroughHeapObjectCallback(art::mirror::Object* obj, void* arg)
219 REQUIRES_SHARED(art::Locks::mutator_lock_) {
220 IterateThroughHeapData* ithd = reinterpret_cast<IterateThroughHeapData*>(arg);
221 // Early return, as we can't really stop visiting.
222 if (ithd->stop_reports) {
223 return;
224 }
225
226 art::ScopedAssertNoThreadSuspension no_suspension("IterateThroughHeapCallback");
227
228 jlong tag = 0;
229 ithd->heap_util->GetTags()->GetTag(obj, &tag);
230
231 jlong class_tag = 0;
232 art::ObjPtr<art::mirror::Class> klass = obj->GetClass();
233 ithd->heap_util->GetTags()->GetTag(klass.Ptr(), &class_tag);
234 // For simplicity, even if we find a tag = 0, assume 0 = not tagged.
235
236 if (!ithd->ShouldReportByHeapFilter(tag, class_tag)) {
237 return;
238 }
239
Andreas Gampee54d9922016-10-11 19:55:37 -0700240 if (ithd->filter_klass != nullptr) {
241 if (ithd->filter_klass != klass) {
242 return;
243 }
244 }
245
246 jlong size = obj->SizeOf();
247
248 jint length = -1;
249 if (obj->IsArrayInstance()) {
250 length = obj->AsArray()->GetLength();
251 }
252
253 jlong saved_tag = tag;
254 jint ret = ithd->callbacks->heap_iteration_callback(class_tag,
255 size,
256 &tag,
257 length,
258 const_cast<void*>(ithd->user_data));
259
260 if (tag != saved_tag) {
261 ithd->heap_util->GetTags()->Set(obj, tag);
262 }
263
264 ithd->stop_reports = (ret & JVMTI_VISIT_ABORT) != 0;
265
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800266 if (!ithd->stop_reports) {
267 jint string_ret = ReportString(obj,
268 ithd->env,
269 ithd->heap_util->GetTags(),
270 ithd->callbacks,
271 ithd->user_data);
272 ithd->stop_reports = (string_ret & JVMTI_VISIT_ABORT) != 0;
273 }
274
Andreas Gampebecd6ad2017-02-22 19:20:37 -0800275 if (!ithd->stop_reports) {
276 jint array_ret = ReportPrimitiveArray(obj,
277 ithd->env,
278 ithd->heap_util->GetTags(),
279 ithd->callbacks,
280 ithd->user_data);
281 ithd->stop_reports = (array_ret & JVMTI_VISIT_ABORT) != 0;
282 }
283
Andreas Gampee54d9922016-10-11 19:55:37 -0700284 // TODO Implement primitive field callback.
285}
286
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800287jvmtiError HeapUtil::IterateThroughHeap(jvmtiEnv* env,
Andreas Gampee54d9922016-10-11 19:55:37 -0700288 jint heap_filter,
289 jclass klass,
290 const jvmtiHeapCallbacks* callbacks,
291 const void* user_data) {
292 if (callbacks == nullptr) {
293 return ERR(NULL_POINTER);
294 }
295
Andreas Gampee54d9922016-10-11 19:55:37 -0700296 art::Thread* self = art::Thread::Current();
297 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
298
299 IterateThroughHeapData ithd(this,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800300 env,
Andreas Gampee54d9922016-10-11 19:55:37 -0700301 heap_filter,
302 soa.Decode<art::mirror::Class>(klass),
303 callbacks,
304 user_data);
305
306 art::Runtime::Current()->GetHeap()->VisitObjects(IterateThroughHeapObjectCallback, &ithd);
307
308 return ERR(NONE);
309}
310
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700311class FollowReferencesHelper FINAL {
312 public:
313 FollowReferencesHelper(HeapUtil* h,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800314 jvmtiEnv* jvmti_env,
Andreas Gampe638a6932016-12-02 19:11:17 -0800315 art::ObjPtr<art::mirror::Object> initial_object,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700316 const jvmtiHeapCallbacks* callbacks,
317 const void* user_data)
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800318 : env(jvmti_env),
319 tag_table_(h->GetTags()),
Andreas Gampe638a6932016-12-02 19:11:17 -0800320 initial_object_(initial_object),
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700321 callbacks_(callbacks),
322 user_data_(user_data),
323 start_(0),
324 stop_reports_(false) {
325 }
326
327 void Init()
328 REQUIRES_SHARED(art::Locks::mutator_lock_)
329 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
Andreas Gampe638a6932016-12-02 19:11:17 -0800330 if (initial_object_.IsNull()) {
331 CollectAndReportRootsVisitor carrv(this, tag_table_, &worklist_, &visited_);
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800332
333 // We need precise info (e.g., vregs).
334 constexpr art::VisitRootFlags kRootFlags = static_cast<art::VisitRootFlags>(
335 art::VisitRootFlags::kVisitRootFlagAllRoots | art::VisitRootFlags::kVisitRootFlagPrecise);
336 art::Runtime::Current()->VisitRoots(&carrv, kRootFlags);
337
Andreas Gampe638a6932016-12-02 19:11:17 -0800338 art::Runtime::Current()->VisitImageRoots(&carrv);
339 stop_reports_ = carrv.IsStopReports();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700340
Andreas Gampe638a6932016-12-02 19:11:17 -0800341 if (stop_reports_) {
342 worklist_.clear();
343 }
344 } else {
345 visited_.insert(initial_object_.Ptr());
346 worklist_.push_back(initial_object_.Ptr());
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700347 }
348 }
349
350 void Work()
351 REQUIRES_SHARED(art::Locks::mutator_lock_)
352 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
353 // Currently implemented as a BFS. To lower overhead, we don't erase elements immediately
354 // from the head of the work list, instead postponing until there's a gap that's "large."
355 //
356 // Alternatively, we can implement a DFS and use the work list as a stack.
357 while (start_ < worklist_.size()) {
358 art::mirror::Object* cur_obj = worklist_[start_];
359 start_++;
360
361 if (start_ >= kMaxStart) {
362 worklist_.erase(worklist_.begin(), worklist_.begin() + start_);
363 start_ = 0;
364 }
365
366 VisitObject(cur_obj);
367
368 if (stop_reports_) {
369 break;
370 }
371 }
372 }
373
374 private:
375 class CollectAndReportRootsVisitor FINAL : public art::RootVisitor {
376 public:
377 CollectAndReportRootsVisitor(FollowReferencesHelper* helper,
378 ObjectTagTable* tag_table,
379 std::vector<art::mirror::Object*>* worklist,
380 std::unordered_set<art::mirror::Object*>* visited)
381 : helper_(helper),
382 tag_table_(tag_table),
383 worklist_(worklist),
384 visited_(visited),
385 stop_reports_(false) {}
386
387 void VisitRoots(art::mirror::Object*** roots, size_t count, const art::RootInfo& info)
388 OVERRIDE
389 REQUIRES_SHARED(art::Locks::mutator_lock_)
390 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
391 for (size_t i = 0; i != count; ++i) {
392 AddRoot(*roots[i], info);
393 }
394 }
395
396 void VisitRoots(art::mirror::CompressedReference<art::mirror::Object>** roots,
397 size_t count,
398 const art::RootInfo& info)
399 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_)
400 REQUIRES(!*helper_->tag_table_->GetAllowDisallowLock()) {
401 for (size_t i = 0; i != count; ++i) {
402 AddRoot(roots[i]->AsMirrorPtr(), info);
403 }
404 }
405
406 bool IsStopReports() {
407 return stop_reports_;
408 }
409
410 private:
411 void AddRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
412 REQUIRES_SHARED(art::Locks::mutator_lock_)
413 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
414 // We use visited_ to mark roots already so we do not need another set.
415 if (visited_->find(root_obj) == visited_->end()) {
416 visited_->insert(root_obj);
417 worklist_->push_back(root_obj);
418 }
419 ReportRoot(root_obj, info);
420 }
421
Andreas Gampe93c30902016-11-18 13:30:30 -0800422 // Remove NO_THREAD_SAFETY_ANALYSIS once ASSERT_CAPABILITY works correctly.
423 art::Thread* FindThread(const art::RootInfo& info) NO_THREAD_SAFETY_ANALYSIS {
424 art::Locks::thread_list_lock_->AssertExclusiveHeld(art::Thread::Current());
425 return art::Runtime::Current()->GetThreadList()->FindThreadByThreadId(info.GetThreadId());
426 }
427
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700428 jvmtiHeapReferenceKind GetReferenceKind(const art::RootInfo& info,
429 jvmtiHeapReferenceInfo* ref_info)
430 REQUIRES_SHARED(art::Locks::mutator_lock_) {
431 // TODO: Fill in ref_info.
432 memset(ref_info, 0, sizeof(jvmtiHeapReferenceInfo));
433
434 switch (info.GetType()) {
435 case art::RootType::kRootJNIGlobal:
436 return JVMTI_HEAP_REFERENCE_JNI_GLOBAL;
437
438 case art::RootType::kRootJNILocal:
Andreas Gampe93c30902016-11-18 13:30:30 -0800439 {
440 uint32_t thread_id = info.GetThreadId();
441 ref_info->jni_local.thread_id = thread_id;
442
443 art::Thread* thread = FindThread(info);
444 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800445 art::mirror::Object* thread_obj;
Andreas Gampe93c30902016-11-18 13:30:30 -0800446 if (thread->IsStillStarting()) {
447 thread_obj = nullptr;
448 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800449 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampe93c30902016-11-18 13:30:30 -0800450 }
451 if (thread_obj != nullptr) {
452 ref_info->jni_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
453 }
454 }
455
456 // TODO: We don't have this info.
457 if (thread != nullptr) {
458 ref_info->jni_local.depth = 0;
459 art::ArtMethod* method = thread->GetCurrentMethod(nullptr, false /* abort_on_error */);
460 if (method != nullptr) {
461 ref_info->jni_local.method = art::jni::EncodeArtMethod(method);
462 }
463 }
464
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700465 return JVMTI_HEAP_REFERENCE_JNI_LOCAL;
Andreas Gampe93c30902016-11-18 13:30:30 -0800466 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700467
468 case art::RootType::kRootJavaFrame:
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800469 {
470 uint32_t thread_id = info.GetThreadId();
471 ref_info->stack_local.thread_id = thread_id;
472
473 art::Thread* thread = FindThread(info);
474 if (thread != nullptr) {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800475 art::mirror::Object* thread_obj;
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800476 if (thread->IsStillStarting()) {
477 thread_obj = nullptr;
478 } else {
Andreas Gampe202f85a2017-02-06 10:23:26 -0800479 thread_obj = thread->GetPeerFromOtherThread();
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800480 }
481 if (thread_obj != nullptr) {
482 ref_info->stack_local.thread_tag = tag_table_->GetTagOrZero(thread_obj);
483 }
484 }
485
486 auto& java_info = static_cast<const art::JavaFrameRootInfo&>(info);
487 ref_info->stack_local.slot = static_cast<jint>(java_info.GetVReg());
488 const art::StackVisitor* visitor = java_info.GetVisitor();
489 ref_info->stack_local.location =
490 static_cast<jlocation>(visitor->GetDexPc(false /* abort_on_failure */));
491 ref_info->stack_local.depth = static_cast<jint>(visitor->GetFrameDepth());
492 art::ArtMethod* method = visitor->GetMethod();
493 if (method != nullptr) {
494 ref_info->stack_local.method = art::jni::EncodeArtMethod(method);
495 }
496
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700497 return JVMTI_HEAP_REFERENCE_STACK_LOCAL;
Andreas Gampef10dfcd2016-12-02 14:42:33 -0800498 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700499
500 case art::RootType::kRootNativeStack:
501 case art::RootType::kRootThreadBlock:
502 case art::RootType::kRootThreadObject:
503 return JVMTI_HEAP_REFERENCE_THREAD;
504
505 case art::RootType::kRootStickyClass:
506 case art::RootType::kRootInternedString:
507 // Note: this isn't a root in the RI.
508 return JVMTI_HEAP_REFERENCE_SYSTEM_CLASS;
509
510 case art::RootType::kRootMonitorUsed:
511 case art::RootType::kRootJNIMonitor:
512 return JVMTI_HEAP_REFERENCE_MONITOR;
513
514 case art::RootType::kRootFinalizing:
515 case art::RootType::kRootDebugger:
516 case art::RootType::kRootReferenceCleanup:
517 case art::RootType::kRootVMInternal:
518 case art::RootType::kRootUnknown:
519 return JVMTI_HEAP_REFERENCE_OTHER;
520 }
521 LOG(FATAL) << "Unreachable";
522 UNREACHABLE();
523 }
524
525 void ReportRoot(art::mirror::Object* root_obj, const art::RootInfo& info)
526 REQUIRES_SHARED(art::Locks::mutator_lock_)
527 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
528 jvmtiHeapReferenceInfo ref_info;
529 jvmtiHeapReferenceKind kind = GetReferenceKind(info, &ref_info);
530 jint result = helper_->ReportReference(kind, &ref_info, nullptr, root_obj);
531 if ((result & JVMTI_VISIT_ABORT) != 0) {
532 stop_reports_ = true;
533 }
534 }
535
536 private:
537 FollowReferencesHelper* helper_;
538 ObjectTagTable* tag_table_;
539 std::vector<art::mirror::Object*>* worklist_;
540 std::unordered_set<art::mirror::Object*>* visited_;
541 bool stop_reports_;
542 };
543
544 void VisitObject(art::mirror::Object* obj)
545 REQUIRES_SHARED(art::Locks::mutator_lock_)
546 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
547 if (obj->IsClass()) {
548 VisitClass(obj->AsClass());
549 return;
550 }
551 if (obj->IsArrayInstance()) {
552 VisitArray(obj);
553 return;
554 }
555
556 // TODO: We'll probably have to rewrite this completely with our own visiting logic, if we
557 // want to have a chance of getting the field indices computed halfway efficiently. For
558 // now, ignore them altogether.
559
560 struct InstanceReferenceVisitor {
561 explicit InstanceReferenceVisitor(FollowReferencesHelper* helper_)
562 : helper(helper_), stop_reports(false) {}
563
564 void operator()(art::mirror::Object* src,
565 art::MemberOffset field_offset,
566 bool is_static ATTRIBUTE_UNUSED) const
567 REQUIRES_SHARED(art::Locks::mutator_lock_)
568 REQUIRES(!*helper->tag_table_->GetAllowDisallowLock()) {
569 if (stop_reports) {
570 return;
571 }
572
573 art::mirror::Object* trg = src->GetFieldObjectReferenceAddr(field_offset)->AsMirrorPtr();
574 jvmtiHeapReferenceInfo reference_info;
575 memset(&reference_info, 0, sizeof(reference_info));
576
577 // TODO: Implement spec-compliant numbering.
578 reference_info.field.index = field_offset.Int32Value();
579
580 jvmtiHeapReferenceKind kind =
581 field_offset.Int32Value() == art::mirror::Object::ClassOffset().Int32Value()
582 ? JVMTI_HEAP_REFERENCE_CLASS
583 : JVMTI_HEAP_REFERENCE_FIELD;
584 const jvmtiHeapReferenceInfo* reference_info_ptr =
585 kind == JVMTI_HEAP_REFERENCE_CLASS ? nullptr : &reference_info;
586
587 stop_reports = !helper->ReportReferenceMaybeEnqueue(kind, reference_info_ptr, src, trg);
588 }
589
590 void VisitRoot(art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED)
591 const {
592 LOG(FATAL) << "Unreachable";
593 }
594 void VisitRootIfNonNull(
595 art::mirror::CompressedReference<art::mirror::Object>* root ATTRIBUTE_UNUSED) const {
596 LOG(FATAL) << "Unreachable";
597 }
598
599 // "mutable" required by the visitor API.
600 mutable FollowReferencesHelper* helper;
601 mutable bool stop_reports;
602 };
603
604 InstanceReferenceVisitor visitor(this);
605 // Visit references, not native roots.
606 obj->VisitReferences<false>(visitor, art::VoidFunctor());
607
608 stop_reports_ = visitor.stop_reports;
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800609
610 if (!stop_reports_) {
611 jint string_ret = ReportString(obj, env, tag_table_, callbacks_, user_data_);
612 stop_reports_ = (string_ret & JVMTI_VISIT_ABORT) != 0;
613 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700614 }
615
616 void VisitArray(art::mirror::Object* array)
617 REQUIRES_SHARED(art::Locks::mutator_lock_)
618 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
619 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS,
620 nullptr,
621 array,
622 array->GetClass());
623 if (stop_reports_) {
624 return;
625 }
626
627 if (array->IsObjectArray()) {
628 art::mirror::ObjectArray<art::mirror::Object>* obj_array =
629 array->AsObjectArray<art::mirror::Object>();
630 int32_t length = obj_array->GetLength();
631 for (int32_t i = 0; i != length; ++i) {
632 art::mirror::Object* elem = obj_array->GetWithoutChecks(i);
633 if (elem != nullptr) {
634 jvmtiHeapReferenceInfo reference_info;
635 reference_info.array.index = i;
636 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
637 &reference_info,
638 array,
639 elem);
640 if (stop_reports_) {
641 break;
642 }
643 }
644 }
Andreas Gampebecd6ad2017-02-22 19:20:37 -0800645 } else {
646 if (!stop_reports_) {
647 jint array_ret = ReportPrimitiveArray(array, env, tag_table_, callbacks_, user_data_);
648 stop_reports_ = (array_ret & JVMTI_VISIT_ABORT) != 0;
649 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700650 }
651 }
652
653 void VisitClass(art::mirror::Class* klass)
654 REQUIRES_SHARED(art::Locks::mutator_lock_)
655 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
656 // TODO: Are erroneous classes reported? Are non-prepared ones? For now, just use resolved ones.
657 if (!klass->IsResolved()) {
658 return;
659 }
660
661 // Superclass.
662 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_SUPERCLASS,
663 nullptr,
664 klass,
665 klass->GetSuperClass());
666 if (stop_reports_) {
667 return;
668 }
669
670 // Directly implemented or extended interfaces.
671 art::Thread* self = art::Thread::Current();
672 art::StackHandleScope<1> hs(self);
673 art::Handle<art::mirror::Class> h_klass(hs.NewHandle<art::mirror::Class>(klass));
674 for (size_t i = 0; i < h_klass->NumDirectInterfaces(); ++i) {
675 art::ObjPtr<art::mirror::Class> inf_klass =
Vladimir Marko19a4d372016-12-08 14:41:46 +0000676 art::mirror::Class::ResolveDirectInterface(self, h_klass, i);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700677 if (inf_klass == nullptr) {
678 // TODO: With a resolved class this should not happen...
679 self->ClearException();
680 break;
681 }
682
683 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_INTERFACE,
684 nullptr,
685 klass,
686 inf_klass.Ptr());
687 if (stop_reports_) {
688 return;
689 }
690 }
691
692 // Classloader.
693 // TODO: What about the boot classpath loader? We'll skip for now, but do we have to find the
694 // fake BootClassLoader?
695 if (klass->GetClassLoader() != nullptr) {
696 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_CLASS_LOADER,
697 nullptr,
698 klass,
699 klass->GetClassLoader());
700 if (stop_reports_) {
701 return;
702 }
703 }
704 DCHECK_EQ(h_klass.Get(), klass);
705
706 // Declared static fields.
707 for (auto& field : klass->GetSFields()) {
708 if (!field.IsPrimitiveType()) {
709 art::ObjPtr<art::mirror::Object> field_value = field.GetObject(klass);
710 if (field_value != nullptr) {
711 jvmtiHeapReferenceInfo reference_info;
712 memset(&reference_info, 0, sizeof(reference_info));
713
714 // TODO: Implement spec-compliant numbering.
715 reference_info.field.index = field.GetOffset().Int32Value();
716
717 stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
718 &reference_info,
719 klass,
720 field_value.Ptr());
721 if (stop_reports_) {
722 return;
723 }
724 }
725 }
726 }
727 }
728
729 void MaybeEnqueue(art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) {
730 if (visited_.find(obj) == visited_.end()) {
731 worklist_.push_back(obj);
732 visited_.insert(obj);
733 }
734 }
735
736 bool ReportReferenceMaybeEnqueue(jvmtiHeapReferenceKind kind,
737 const jvmtiHeapReferenceInfo* reference_info,
738 art::mirror::Object* referree,
739 art::mirror::Object* referrer)
740 REQUIRES_SHARED(art::Locks::mutator_lock_)
741 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
742 jint result = ReportReference(kind, reference_info, referree, referrer);
743 if ((result & JVMTI_VISIT_ABORT) == 0) {
744 if ((result & JVMTI_VISIT_OBJECTS) != 0) {
745 MaybeEnqueue(referrer);
746 }
747 return true;
748 } else {
749 return false;
750 }
751 }
752
753 jint ReportReference(jvmtiHeapReferenceKind kind,
754 const jvmtiHeapReferenceInfo* reference_info,
755 art::mirror::Object* referrer,
756 art::mirror::Object* referree)
757 REQUIRES_SHARED(art::Locks::mutator_lock_)
758 REQUIRES(!*tag_table_->GetAllowDisallowLock()) {
759 if (referree == nullptr || stop_reports_) {
760 return 0;
761 }
762
763 const jlong class_tag = tag_table_->GetTagOrZero(referree->GetClass());
764 const jlong referrer_class_tag =
765 referrer == nullptr ? 0 : tag_table_->GetTagOrZero(referrer->GetClass());
766 const jlong size = static_cast<jlong>(referree->SizeOf());
767 jlong tag = tag_table_->GetTagOrZero(referree);
768 jlong saved_tag = tag;
769 jlong referrer_tag = 0;
770 jlong saved_referrer_tag = 0;
771 jlong* referrer_tag_ptr;
772 if (referrer == nullptr) {
773 referrer_tag_ptr = nullptr;
774 } else {
775 if (referrer == referree) {
776 referrer_tag_ptr = &tag;
777 } else {
778 referrer_tag = saved_referrer_tag = tag_table_->GetTagOrZero(referrer);
779 referrer_tag_ptr = &referrer_tag;
780 }
781 }
782 jint length = -1;
783 if (referree->IsArrayInstance()) {
784 length = referree->AsArray()->GetLength();
785 }
786
787 jint result = callbacks_->heap_reference_callback(kind,
788 reference_info,
789 class_tag,
790 referrer_class_tag,
791 size,
792 &tag,
793 referrer_tag_ptr,
794 length,
795 const_cast<void*>(user_data_));
796
797 if (tag != saved_tag) {
798 tag_table_->Set(referree, tag);
799 }
800 if (referrer_tag != saved_referrer_tag) {
801 tag_table_->Set(referrer, referrer_tag);
802 }
803
804 return result;
805 }
806
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800807 jvmtiEnv* env;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700808 ObjectTagTable* tag_table_;
Andreas Gampe638a6932016-12-02 19:11:17 -0800809 art::ObjPtr<art::mirror::Object> initial_object_;
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700810 const jvmtiHeapCallbacks* callbacks_;
811 const void* user_data_;
812
813 std::vector<art::mirror::Object*> worklist_;
814 size_t start_;
815 static constexpr size_t kMaxStart = 1000000U;
816
817 std::unordered_set<art::mirror::Object*> visited_;
818
819 bool stop_reports_;
820
821 friend class CollectAndReportRootsVisitor;
822};
823
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800824jvmtiError HeapUtil::FollowReferences(jvmtiEnv* env,
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700825 jint heap_filter ATTRIBUTE_UNUSED,
826 jclass klass ATTRIBUTE_UNUSED,
827 jobject initial_object,
828 const jvmtiHeapCallbacks* callbacks,
829 const void* user_data) {
830 if (callbacks == nullptr) {
831 return ERR(NULL_POINTER);
832 }
833
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700834 art::Thread* self = art::Thread::Current();
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700835
Andreas Gampe638a6932016-12-02 19:11:17 -0800836 art::gc::Heap* heap = art::Runtime::Current()->GetHeap();
837 if (heap->IsGcConcurrentAndMoving()) {
838 // Need to take a heap dump while GC isn't running. See the
839 // comment in Heap::VisitObjects().
840 heap->IncrementDisableMovingGC(self);
841 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700842 {
Andreas Gampe638a6932016-12-02 19:11:17 -0800843 art::ScopedObjectAccess soa(self); // Now we know we have the shared lock.
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700844 art::ScopedThreadSuspension sts(self, art::kWaitingForVisitObjects);
845 art::ScopedSuspendAll ssa("FollowReferences");
846
Andreas Gampe638a6932016-12-02 19:11:17 -0800847 FollowReferencesHelper frh(this,
Andreas Gampe3ec8e402017-02-21 15:49:53 -0800848 env,
Andreas Gampe638a6932016-12-02 19:11:17 -0800849 self->DecodeJObject(initial_object),
850 callbacks,
851 user_data);
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700852 frh.Init();
853 frh.Work();
854 }
Andreas Gampe638a6932016-12-02 19:11:17 -0800855 if (heap->IsGcConcurrentAndMoving()) {
856 heap->DecrementDisableMovingGC(self);
857 }
Andreas Gampe70bfc8a2016-11-03 11:04:15 -0700858
859 return ERR(NONE);
860}
861
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700862jvmtiError HeapUtil::GetLoadedClasses(jvmtiEnv* env,
863 jint* class_count_ptr,
864 jclass** classes_ptr) {
865 if (class_count_ptr == nullptr || classes_ptr == nullptr) {
866 return ERR(NULL_POINTER);
867 }
868
869 class ReportClassVisitor : public art::ClassVisitor {
870 public:
871 explicit ReportClassVisitor(art::Thread* self) : self_(self) {}
872
Mathieu Chartier28357fa2016-10-18 16:27:40 -0700873 bool operator()(art::ObjPtr<art::mirror::Class> klass)
874 OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampeef54d8d2016-10-25 09:55:53 -0700875 classes_.push_back(self_->GetJniEnv()->AddLocalReference<jclass>(klass));
Andreas Gampeaa8b60c2016-10-12 12:51:25 -0700876 return true;
877 }
878
879 art::Thread* self_;
880 std::vector<jclass> classes_;
881 };
882
883 art::Thread* self = art::Thread::Current();
884 ReportClassVisitor rcv(self);
885 {
886 art::ScopedObjectAccess soa(self);
887 art::Runtime::Current()->GetClassLinker()->VisitClasses(&rcv);
888 }
889
890 size_t size = rcv.classes_.size();
891 jclass* classes = nullptr;
892 jvmtiError alloc_ret = env->Allocate(static_cast<jlong>(size * sizeof(jclass)),
893 reinterpret_cast<unsigned char**>(&classes));
894 if (alloc_ret != ERR(NONE)) {
895 return alloc_ret;
896 }
897
898 for (size_t i = 0; i < size; ++i) {
899 classes[i] = rcv.classes_[i];
900 }
901 *classes_ptr = classes;
902 *class_count_ptr = static_cast<jint>(size);
903
904 return ERR(NONE);
905}
906
Andreas Gampe8da6d032016-10-31 19:31:03 -0700907jvmtiError HeapUtil::ForceGarbageCollection(jvmtiEnv* env ATTRIBUTE_UNUSED) {
908 art::Runtime::Current()->GetHeap()->CollectGarbage(false);
909
910 return ERR(NONE);
911}
Andreas Gampee54d9922016-10-11 19:55:37 -0700912} // namespace openjdkjvmti