1 /* 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classLoaderDataGraph.hpp" 27 #include "classfile/javaClasses.inline.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionary.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "jvmtifiles/jvmtiEnv.hpp" 32 #include "logging/log.hpp" 33 #include "memory/allocation.inline.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "memory/universe.hpp" 36 #include "oops/access.inline.hpp" 37 #include "oops/arrayOop.inline.hpp" 38 #include "oops/constantPool.inline.hpp" 39 #include "oops/instanceMirrorKlass.hpp" 40 #include "oops/objArrayKlass.hpp" 41 #include "oops/objArrayOop.inline.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "oops/typeArrayOop.inline.hpp" 44 #include "prims/jvmtiEventController.hpp" 45 #include "prims/jvmtiEventController.inline.hpp" 46 #include "prims/jvmtiExport.hpp" 47 #include "prims/jvmtiImpl.hpp" 48 #include "prims/jvmtiTagMap.hpp" 49 #include "runtime/biasedLocking.hpp" 50 #include "runtime/deoptimization.hpp" 51 #include "runtime/frame.inline.hpp" 52 #include "runtime/handles.inline.hpp" 53 #include "runtime/javaCalls.hpp" 54 #include "runtime/jniHandles.inline.hpp" 55 #include "runtime/mutex.hpp" 56 #include "runtime/mutexLocker.hpp" 57 #include "runtime/reflectionUtils.hpp" 58 #include "runtime/thread.inline.hpp" 59 #include "runtime/threadSMR.hpp" 60 #include "runtime/vframe.hpp" 61 #include "runtime/vmThread.hpp" 62 #include "runtime/vmOperations.hpp" 63 #include "utilities/macros.hpp" 64 #if INCLUDE_ZGC 65 #include "gc/z/zGlobals.hpp" 66 #endif 67 68 // JvmtiTagHashmapEntry 69 // 70 // Each entry encapsulates a reference to the tagged object 71 // and the tag value. In addition an entry includes a next pointer which 72 // is used to chain entries together. 73 74 class JvmtiTagHashmapEntry : public CHeapObj<mtInternal> { 75 private: 76 friend class JvmtiTagMap; 77 78 oop _object; // tagged object 79 jlong _tag; // the tag 80 JvmtiTagHashmapEntry* _next; // next on the list 81 82 inline void init(oop object, jlong tag) { 83 _object = object; 84 _tag = tag; 85 _next = NULL; 86 } 87 88 // constructor 89 JvmtiTagHashmapEntry(oop object, jlong tag) { init(object, tag); } 90 91 public: 92 93 // accessor methods 94 inline oop* object_addr() { return &_object; } 95 inline oop object() { return NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(object_addr()); } 96 // Peek at the object without keeping it alive. The returned object must be 97 // kept alive using a normal access if it leaks out of a thread transition from VM. 98 inline oop object_peek() { 99 return NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(object_addr()); 100 } 101 inline jlong tag() const { return _tag; } 102 103 inline void set_tag(jlong tag) { 104 assert(tag != 0, "can't be zero"); 105 _tag = tag; 106 } 107 108 inline bool equals(oop object) { 109 return oopDesc::equals(object, object_peek()); 110 } 111 112 inline JvmtiTagHashmapEntry* next() const { return _next; } 113 inline void set_next(JvmtiTagHashmapEntry* next) { _next = next; } 114 }; 115 116 117 // JvmtiTagHashmap 118 // 119 // A hashmap is essentially a table of pointers to entries. Entries 120 // are hashed to a location, or position in the table, and then 121 // chained from that location. The "key" for hashing is address of 122 // the object, or oop. The "value" is the tag value. 123 // 124 // A hashmap maintains a count of the number entries in the hashmap 125 // and resizes if the number of entries exceeds a given threshold. 126 // The threshold is specified as a percentage of the size - for 127 // example a threshold of 0.75 will trigger the hashmap to resize 128 // if the number of entries is >75% of table size. 129 // 130 // A hashmap provides functions for adding, removing, and finding 131 // entries. It also provides a function to iterate over all entries 132 // in the hashmap. 133 134 class JvmtiTagHashmap : public CHeapObj<mtInternal> { 135 private: 136 friend class JvmtiTagMap; 137 138 enum { 139 small_trace_threshold = 10000, // threshold for tracing 140 medium_trace_threshold = 100000, 141 large_trace_threshold = 1000000, 142 initial_trace_threshold = small_trace_threshold 143 }; 144 145 static int _sizes[]; // array of possible hashmap sizes 146 int _size; // actual size of the table 147 int _size_index; // index into size table 148 149 int _entry_count; // number of entries in the hashmap 150 151 float _load_factor; // load factor as a % of the size 152 int _resize_threshold; // computed threshold to trigger resizing. 153 bool _resizing_enabled; // indicates if hashmap can resize 154 155 int _trace_threshold; // threshold for trace messages 156 157 JvmtiTagHashmapEntry** _table; // the table of entries. 158 159 // private accessors 160 int resize_threshold() const { return _resize_threshold; } 161 int trace_threshold() const { return _trace_threshold; } 162 163 // initialize the hashmap 164 void init(int size_index=0, float load_factor=4.0f) { 165 int initial_size = _sizes[size_index]; 166 _size_index = size_index; 167 _size = initial_size; 168 _entry_count = 0; 169 _trace_threshold = initial_trace_threshold; 170 _load_factor = load_factor; 171 _resize_threshold = (int)(_load_factor * _size); 172 _resizing_enabled = true; 173 size_t s = initial_size * sizeof(JvmtiTagHashmapEntry*); 174 _table = (JvmtiTagHashmapEntry**)os::malloc(s, mtInternal); 175 if (_table == NULL) { 176 vm_exit_out_of_memory(s, OOM_MALLOC_ERROR, 177 "unable to allocate initial hashtable for jvmti object tags"); 178 } 179 for (int i=0; i<initial_size; i++) { 180 _table[i] = NULL; 181 } 182 } 183 184 // hash a given key (oop) with the specified size 185 static unsigned int hash(oop key, int size) { 186 const oop obj = Access<>::resolve(key); 187 const unsigned int hash = Universe::heap()->hash_oop(obj); 188 return hash % size; 189 } 190 191 // hash a given key (oop) 192 unsigned int hash(oop key) { 193 return hash(key, _size); 194 } 195 196 // resize the hashmap - allocates a large table and re-hashes 197 // all entries into the new table. 198 void resize() { 199 int new_size_index = _size_index+1; 200 int new_size = _sizes[new_size_index]; 201 if (new_size < 0) { 202 // hashmap already at maximum capacity 203 return; 204 } 205 206 // allocate new table 207 size_t s = new_size * sizeof(JvmtiTagHashmapEntry*); 208 JvmtiTagHashmapEntry** new_table = (JvmtiTagHashmapEntry**)os::malloc(s, mtInternal); 209 if (new_table == NULL) { 210 warning("unable to allocate larger hashtable for jvmti object tags"); 211 set_resizing_enabled(false); 212 return; 213 } 214 215 // initialize new table 216 int i; 217 for (i=0; i<new_size; i++) { 218 new_table[i] = NULL; 219 } 220 221 // rehash all entries into the new table 222 for (i=0; i<_size; i++) { 223 JvmtiTagHashmapEntry* entry = _table[i]; 224 while (entry != NULL) { 225 JvmtiTagHashmapEntry* next = entry->next(); 226 oop key = entry->object_peek(); 227 assert(key != NULL, "jni weak reference cleared!!"); 228 unsigned int h = hash(key, new_size); 229 JvmtiTagHashmapEntry* anchor = new_table[h]; 230 if (anchor == NULL) { 231 new_table[h] = entry; 232 entry->set_next(NULL); 233 } else { 234 entry->set_next(anchor); 235 new_table[h] = entry; 236 } 237 entry = next; 238 } 239 } 240 241 // free old table and update settings. 242 os::free((void*)_table); 243 _table = new_table; 244 _size_index = new_size_index; 245 _size = new_size; 246 247 // compute new resize threshold 248 _resize_threshold = (int)(_load_factor * _size); 249 } 250 251 252 // internal remove function - remove an entry at a given position in the 253 // table. 254 inline void remove(JvmtiTagHashmapEntry* prev, int pos, JvmtiTagHashmapEntry* entry) { 255 assert(pos >= 0 && pos < _size, "out of range"); 256 if (prev == NULL) { 257 _table[pos] = entry->next(); 258 } else { 259 prev->set_next(entry->next()); 260 } 261 assert(_entry_count > 0, "checking"); 262 _entry_count--; 263 } 264 265 // resizing switch 266 bool is_resizing_enabled() const { return _resizing_enabled; } 267 void set_resizing_enabled(bool enable) { _resizing_enabled = enable; } 268 269 // debugging 270 void print_memory_usage(); 271 void compute_next_trace_threshold(); 272 273 public: 274 275 // create a JvmtiTagHashmap of a preferred size and optionally a load factor. 276 // The preferred size is rounded down to an actual size. 277 JvmtiTagHashmap(int size, float load_factor=0.0f) { 278 int i=0; 279 while (_sizes[i] < size) { 280 if (_sizes[i] < 0) { 281 assert(i > 0, "sanity check"); 282 i--; 283 break; 284 } 285 i++; 286 } 287 288 // if a load factor is specified then use it, otherwise use default 289 if (load_factor > 0.01f) { 290 init(i, load_factor); 291 } else { 292 init(i); 293 } 294 } 295 296 // create a JvmtiTagHashmap with default settings 297 JvmtiTagHashmap() { 298 init(); 299 } 300 301 // release table when JvmtiTagHashmap destroyed 302 ~JvmtiTagHashmap() { 303 if (_table != NULL) { 304 os::free((void*)_table); 305 _table = NULL; 306 } 307 } 308 309 // accessors 310 int size() const { return _size; } 311 JvmtiTagHashmapEntry** table() const { return _table; } 312 int entry_count() const { return _entry_count; } 313 314 // find an entry in the hashmap, returns NULL if not found. 315 inline JvmtiTagHashmapEntry* find(oop key) { 316 unsigned int h = hash(key); 317 JvmtiTagHashmapEntry* entry = _table[h]; 318 while (entry != NULL) { 319 if (entry->equals(key)) { 320 return entry; 321 } 322 entry = entry->next(); 323 } 324 return NULL; 325 } 326 327 328 // add a new entry to hashmap 329 inline void add(oop key, JvmtiTagHashmapEntry* entry) { 330 assert(key != NULL, "checking"); 331 assert(find(key) == NULL, "duplicate detected"); 332 unsigned int h = hash(key); 333 JvmtiTagHashmapEntry* anchor = _table[h]; 334 if (anchor == NULL) { 335 _table[h] = entry; 336 entry->set_next(NULL); 337 } else { 338 entry->set_next(anchor); 339 _table[h] = entry; 340 } 341 342 _entry_count++; 343 if (log_is_enabled(Debug, jvmti, objecttagging) && entry_count() >= trace_threshold()) { 344 print_memory_usage(); 345 compute_next_trace_threshold(); 346 } 347 348 // if the number of entries exceed the threshold then resize 349 if (entry_count() > resize_threshold() && is_resizing_enabled()) { 350 resize(); 351 } 352 } 353 354 // remove an entry with the given key. 355 inline JvmtiTagHashmapEntry* remove(oop key) { 356 unsigned int h = hash(key); 357 JvmtiTagHashmapEntry* entry = _table[h]; 358 JvmtiTagHashmapEntry* prev = NULL; 359 while (entry != NULL) { 360 if (entry->equals(key)) { 361 break; 362 } 363 prev = entry; 364 entry = entry->next(); 365 } 366 if (entry != NULL) { 367 remove(prev, h, entry); 368 } 369 return entry; 370 } 371 372 // iterate over all entries in the hashmap 373 void entry_iterate(JvmtiTagHashmapEntryClosure* closure); 374 }; 375 376 // possible hashmap sizes - odd primes that roughly double in size. 377 // To avoid excessive resizing the odd primes from 4801-76831 and 378 // 76831-307261 have been removed. The list must be terminated by -1. 379 int JvmtiTagHashmap::_sizes[] = { 4801, 76831, 307261, 614563, 1228891, 380 2457733, 4915219, 9830479, 19660831, 39321619, 78643219, -1 }; 381 382 383 // A supporting class for iterating over all entries in Hashmap 384 class JvmtiTagHashmapEntryClosure { 385 public: 386 virtual void do_entry(JvmtiTagHashmapEntry* entry) = 0; 387 }; 388 389 390 // iterate over all entries in the hashmap 391 void JvmtiTagHashmap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) { 392 for (int i=0; i<_size; i++) { 393 JvmtiTagHashmapEntry* entry = _table[i]; 394 JvmtiTagHashmapEntry* prev = NULL; 395 while (entry != NULL) { 396 // obtain the next entry before invoking do_entry - this is 397 // necessary because do_entry may remove the entry from the 398 // hashmap. 399 JvmtiTagHashmapEntry* next = entry->next(); 400 closure->do_entry(entry); 401 entry = next; 402 } 403 } 404 } 405 406 // debugging 407 void JvmtiTagHashmap::print_memory_usage() { 408 intptr_t p = (intptr_t)this; 409 tty->print("[JvmtiTagHashmap @ " INTPTR_FORMAT, p); 410 411 // table + entries in KB 412 int hashmap_usage = (size()*sizeof(JvmtiTagHashmapEntry*) + 413 entry_count()*sizeof(JvmtiTagHashmapEntry))/K; 414 415 int weak_globals_usage = (int)(JNIHandles::weak_global_handle_memory_usage()/K); 416 tty->print_cr(", %d entries (%d KB) <JNI weak globals: %d KB>]", 417 entry_count(), hashmap_usage, weak_globals_usage); 418 } 419 420 // compute threshold for the next trace message 421 void JvmtiTagHashmap::compute_next_trace_threshold() { 422 _trace_threshold = entry_count(); 423 if (trace_threshold() < medium_trace_threshold) { 424 _trace_threshold += small_trace_threshold; 425 } else { 426 if (trace_threshold() < large_trace_threshold) { 427 _trace_threshold += medium_trace_threshold; 428 } else { 429 _trace_threshold += large_trace_threshold; 430 } 431 } 432 } 433 434 // create a JvmtiTagMap 435 JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) : 436 _env(env), 437 _lock(Mutex::nonleaf+2, "JvmtiTagMap._lock", false), 438 _free_entries(NULL), 439 _free_entries_count(0) 440 { 441 assert(JvmtiThreadState_lock->is_locked(), "sanity check"); 442 assert(((JvmtiEnvBase *)env)->tag_map() == NULL, "tag map already exists for environment"); 443 444 _hashmap = new JvmtiTagHashmap(); 445 446 // finally add us to the environment 447 ((JvmtiEnvBase *)env)->release_set_tag_map(this); 448 } 449 450 451 // destroy a JvmtiTagMap 452 JvmtiTagMap::~JvmtiTagMap() { 453 454 // no lock acquired as we assume the enclosing environment is 455 // also being destroryed. 456 ((JvmtiEnvBase *)_env)->set_tag_map(NULL); 457 458 JvmtiTagHashmapEntry** table = _hashmap->table(); 459 for (int j = 0; j < _hashmap->size(); j++) { 460 JvmtiTagHashmapEntry* entry = table[j]; 461 while (entry != NULL) { 462 JvmtiTagHashmapEntry* next = entry->next(); 463 delete entry; 464 entry = next; 465 } 466 } 467 468 // finally destroy the hashmap 469 delete _hashmap; 470 _hashmap = NULL; 471 472 // remove any entries on the free list 473 JvmtiTagHashmapEntry* entry = _free_entries; 474 while (entry != NULL) { 475 JvmtiTagHashmapEntry* next = entry->next(); 476 delete entry; 477 entry = next; 478 } 479 _free_entries = NULL; 480 } 481 482 // create a hashmap entry 483 // - if there's an entry on the (per-environment) free list then this 484 // is returned. Otherwise an new entry is allocated. 485 JvmtiTagHashmapEntry* JvmtiTagMap::create_entry(oop ref, jlong tag) { 486 assert(Thread::current()->is_VM_thread() || is_locked(), "checking"); 487 JvmtiTagHashmapEntry* entry; 488 if (_free_entries == NULL) { 489 entry = new JvmtiTagHashmapEntry(ref, tag); 490 } else { 491 assert(_free_entries_count > 0, "mismatched _free_entries_count"); 492 _free_entries_count--; 493 entry = _free_entries; 494 _free_entries = entry->next(); 495 entry->init(ref, tag); 496 } 497 return entry; 498 } 499 500 // destroy an entry by returning it to the free list 501 void JvmtiTagMap::destroy_entry(JvmtiTagHashmapEntry* entry) { 502 assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking"); 503 // limit the size of the free list 504 if (_free_entries_count >= max_free_entries) { 505 delete entry; 506 } else { 507 entry->set_next(_free_entries); 508 _free_entries = entry; 509 _free_entries_count++; 510 } 511 } 512 513 // returns the tag map for the given environments. If the tag map 514 // doesn't exist then it is created. 515 JvmtiTagMap* JvmtiTagMap::tag_map_for(JvmtiEnv* env) { 516 JvmtiTagMap* tag_map = ((JvmtiEnvBase*)env)->tag_map_acquire(); 517 if (tag_map == NULL) { 518 MutexLocker mu(JvmtiThreadState_lock); 519 tag_map = ((JvmtiEnvBase*)env)->tag_map(); 520 if (tag_map == NULL) { 521 tag_map = new JvmtiTagMap(env); 522 } 523 } else { 524 DEBUG_ONLY(Thread::current()->check_possible_safepoint()); 525 } 526 return tag_map; 527 } 528 529 // iterate over all entries in the tag map. 530 void JvmtiTagMap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) { 531 hashmap()->entry_iterate(closure); 532 } 533 534 // returns true if the hashmaps are empty 535 bool JvmtiTagMap::is_empty() { 536 assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking"); 537 return hashmap()->entry_count() == 0; 538 } 539 540 541 // Return the tag value for an object, or 0 if the object is 542 // not tagged 543 // 544 static inline jlong tag_for(JvmtiTagMap* tag_map, oop o) { 545 JvmtiTagHashmapEntry* entry = tag_map->hashmap()->find(o); 546 if (entry == NULL) { 547 return 0; 548 } else { 549 return entry->tag(); 550 } 551 } 552 553 554 // A CallbackWrapper is a support class for querying and tagging an object 555 // around a callback to a profiler. The constructor does pre-callback 556 // work to get the tag value, klass tag value, ... and the destructor 557 // does the post-callback work of tagging or untagging the object. 558 // 559 // { 560 // CallbackWrapper wrapper(tag_map, o); 561 // 562 // (*callback)(wrapper.klass_tag(), wrapper.obj_size(), wrapper.obj_tag_p(), ...) 563 // 564 // } // wrapper goes out of scope here which results in the destructor 565 // checking to see if the object has been tagged, untagged, or the 566 // tag value has changed. 567 // 568 class CallbackWrapper : public StackObj { 569 private: 570 JvmtiTagMap* _tag_map; 571 JvmtiTagHashmap* _hashmap; 572 JvmtiTagHashmapEntry* _entry; 573 oop _o; 574 jlong _obj_size; 575 jlong _obj_tag; 576 jlong _klass_tag; 577 578 protected: 579 JvmtiTagMap* tag_map() const { return _tag_map; } 580 581 // invoked post-callback to tag, untag, or update the tag of an object 582 void inline post_callback_tag_update(oop o, JvmtiTagHashmap* hashmap, 583 JvmtiTagHashmapEntry* entry, jlong obj_tag); 584 public: 585 CallbackWrapper(JvmtiTagMap* tag_map, oop o) { 586 assert(Thread::current()->is_VM_thread() || tag_map->is_locked(), 587 "MT unsafe or must be VM thread"); 588 589 // object to tag 590 _o = o; 591 592 // object size 593 _obj_size = (jlong)_o->size() * wordSize; 594 595 // record the context 596 _tag_map = tag_map; 597 _hashmap = tag_map->hashmap(); 598 _entry = _hashmap->find(_o); 599 600 // get object tag 601 _obj_tag = (_entry == NULL) ? 0 : _entry->tag(); 602 603 // get the class and the class's tag value 604 assert(SystemDictionary::Class_klass()->is_mirror_instance_klass(), "Is not?"); 605 606 _klass_tag = tag_for(tag_map, _o->klass()->java_mirror()); 607 } 608 609 ~CallbackWrapper() { 610 post_callback_tag_update(_o, _hashmap, _entry, _obj_tag); 611 } 612 613 inline jlong* obj_tag_p() { return &_obj_tag; } 614 inline jlong obj_size() const { return _obj_size; } 615 inline jlong obj_tag() const { return _obj_tag; } 616 inline jlong klass_tag() const { return _klass_tag; } 617 }; 618 619 620 621 // callback post-callback to tag, untag, or update the tag of an object 622 void inline CallbackWrapper::post_callback_tag_update(oop o, 623 JvmtiTagHashmap* hashmap, 624 JvmtiTagHashmapEntry* entry, 625 jlong obj_tag) { 626 if (entry == NULL) { 627 if (obj_tag != 0) { 628 // callback has tagged the object 629 assert(Thread::current()->is_VM_thread(), "must be VMThread"); 630 entry = tag_map()->create_entry(o, obj_tag); 631 hashmap->add(o, entry); 632 } 633 } else { 634 // object was previously tagged - the callback may have untagged 635 // the object or changed the tag value 636 if (obj_tag == 0) { 637 638 JvmtiTagHashmapEntry* entry_removed = hashmap->remove(o); 639 assert(entry_removed == entry, "checking"); 640 tag_map()->destroy_entry(entry); 641 642 } else { 643 if (obj_tag != entry->tag()) { 644 entry->set_tag(obj_tag); 645 } 646 } 647 } 648 } 649 650 // An extended CallbackWrapper used when reporting an object reference 651 // to the agent. 652 // 653 // { 654 // TwoOopCallbackWrapper wrapper(tag_map, referrer, o); 655 // 656 // (*callback)(wrapper.klass_tag(), 657 // wrapper.obj_size(), 658 // wrapper.obj_tag_p() 659 // wrapper.referrer_tag_p(), ...) 660 // 661 // } // wrapper goes out of scope here which results in the destructor 662 // checking to see if the referrer object has been tagged, untagged, 663 // or the tag value has changed. 664 // 665 class TwoOopCallbackWrapper : public CallbackWrapper { 666 private: 667 bool _is_reference_to_self; 668 JvmtiTagHashmap* _referrer_hashmap; 669 JvmtiTagHashmapEntry* _referrer_entry; 670 oop _referrer; 671 jlong _referrer_obj_tag; 672 jlong _referrer_klass_tag; 673 jlong* _referrer_tag_p; 674 675 bool is_reference_to_self() const { return _is_reference_to_self; } 676 677 public: 678 TwoOopCallbackWrapper(JvmtiTagMap* tag_map, oop referrer, oop o) : 679 CallbackWrapper(tag_map, o) 680 { 681 // self reference needs to be handled in a special way 682 _is_reference_to_self = (referrer == o); 683 684 if (_is_reference_to_self) { 685 _referrer_klass_tag = klass_tag(); 686 _referrer_tag_p = obj_tag_p(); 687 } else { 688 _referrer = referrer; 689 // record the context 690 _referrer_hashmap = tag_map->hashmap(); 691 _referrer_entry = _referrer_hashmap->find(_referrer); 692 693 // get object tag 694 _referrer_obj_tag = (_referrer_entry == NULL) ? 0 : _referrer_entry->tag(); 695 _referrer_tag_p = &_referrer_obj_tag; 696 697 // get referrer class tag. 698 _referrer_klass_tag = tag_for(tag_map, _referrer->klass()->java_mirror()); 699 } 700 } 701 702 ~TwoOopCallbackWrapper() { 703 if (!is_reference_to_self()){ 704 post_callback_tag_update(_referrer, 705 _referrer_hashmap, 706 _referrer_entry, 707 _referrer_obj_tag); 708 } 709 } 710 711 // address of referrer tag 712 // (for a self reference this will return the same thing as obj_tag_p()) 713 inline jlong* referrer_tag_p() { return _referrer_tag_p; } 714 715 // referrer's class tag 716 inline jlong referrer_klass_tag() { return _referrer_klass_tag; } 717 }; 718 719 // tag an object 720 // 721 // This function is performance critical. If many threads attempt to tag objects 722 // around the same time then it's possible that the Mutex associated with the 723 // tag map will be a hot lock. 724 void JvmtiTagMap::set_tag(jobject object, jlong tag) { 725 MutexLocker ml(lock()); 726 727 // resolve the object 728 oop o = JNIHandles::resolve_non_null(object); 729 730 // see if the object is already tagged 731 JvmtiTagHashmap* hashmap = _hashmap; 732 JvmtiTagHashmapEntry* entry = hashmap->find(o); 733 734 // if the object is not already tagged then we tag it 735 if (entry == NULL) { 736 if (tag != 0) { 737 entry = create_entry(o, tag); 738 hashmap->add(o, entry); 739 } else { 740 // no-op 741 } 742 } else { 743 // if the object is already tagged then we either update 744 // the tag (if a new tag value has been provided) 745 // or remove the object if the new tag value is 0. 746 if (tag == 0) { 747 hashmap->remove(o); 748 destroy_entry(entry); 749 } else { 750 entry->set_tag(tag); 751 } 752 } 753 } 754 755 // get the tag for an object 756 jlong JvmtiTagMap::get_tag(jobject object) { 757 MutexLocker ml(lock()); 758 759 // resolve the object 760 oop o = JNIHandles::resolve_non_null(object); 761 762 return tag_for(this, o); 763 } 764 765 766 // Helper class used to describe the static or instance fields of a class. 767 // For each field it holds the field index (as defined by the JVMTI specification), 768 // the field type, and the offset. 769 770 class ClassFieldDescriptor: public CHeapObj<mtInternal> { 771 private: 772 int _field_index; 773 int _field_offset; 774 char _field_type; 775 public: 776 ClassFieldDescriptor(int index, char type, int offset) : 777 _field_index(index), _field_offset(offset), _field_type(type) { 778 } 779 int field_index() const { return _field_index; } 780 char field_type() const { return _field_type; } 781 int field_offset() const { return _field_offset; } 782 }; 783 784 class ClassFieldMap: public CHeapObj<mtInternal> { 785 private: 786 enum { 787 initial_field_count = 5 788 }; 789 790 // list of field descriptors 791 GrowableArray<ClassFieldDescriptor*>* _fields; 792 793 // constructor 794 ClassFieldMap(); 795 796 // add a field 797 void add(int index, char type, int offset); 798 799 // returns the field count for the given class 800 static int compute_field_count(InstanceKlass* ik); 801 802 public: 803 ~ClassFieldMap(); 804 805 // access 806 int field_count() { return _fields->length(); } 807 ClassFieldDescriptor* field_at(int i) { return _fields->at(i); } 808 809 // functions to create maps of static or instance fields 810 static ClassFieldMap* create_map_of_static_fields(Klass* k); 811 static ClassFieldMap* create_map_of_instance_fields(oop obj); 812 }; 813 814 ClassFieldMap::ClassFieldMap() { 815 _fields = new (ResourceObj::C_HEAP, mtInternal) 816 GrowableArray<ClassFieldDescriptor*>(initial_field_count, true); 817 } 818 819 ClassFieldMap::~ClassFieldMap() { 820 for (int i=0; i<_fields->length(); i++) { 821 delete _fields->at(i); 822 } 823 delete _fields; 824 } 825 826 void ClassFieldMap::add(int index, char type, int offset) { 827 ClassFieldDescriptor* field = new ClassFieldDescriptor(index, type, offset); 828 _fields->append(field); 829 } 830 831 // Returns a heap allocated ClassFieldMap to describe the static fields 832 // of the given class. 833 // 834 ClassFieldMap* ClassFieldMap::create_map_of_static_fields(Klass* k) { 835 HandleMark hm; 836 InstanceKlass* ik = InstanceKlass::cast(k); 837 838 // create the field map 839 ClassFieldMap* field_map = new ClassFieldMap(); 840 841 FilteredFieldStream f(ik, false, false); 842 int max_field_index = f.field_count()-1; 843 844 int index = 0; 845 for (FilteredFieldStream fld(ik, true, true); !fld.eos(); fld.next(), index++) { 846 // ignore instance fields 847 if (!fld.access_flags().is_static()) { 848 continue; 849 } 850 field_map->add(max_field_index - index, fld.signature()->char_at(0), fld.offset()); 851 } 852 return field_map; 853 } 854 855 // Returns a heap allocated ClassFieldMap to describe the instance fields 856 // of the given class. All instance fields are included (this means public 857 // and private fields declared in superclasses and superinterfaces too). 858 // 859 ClassFieldMap* ClassFieldMap::create_map_of_instance_fields(oop obj) { 860 HandleMark hm; 861 InstanceKlass* ik = InstanceKlass::cast(obj->klass()); 862 863 // create the field map 864 ClassFieldMap* field_map = new ClassFieldMap(); 865 866 FilteredFieldStream f(ik, false, false); 867 868 int max_field_index = f.field_count()-1; 869 870 int index = 0; 871 for (FilteredFieldStream fld(ik, false, false); !fld.eos(); fld.next(), index++) { 872 // ignore static fields 873 if (fld.access_flags().is_static()) { 874 continue; 875 } 876 field_map->add(max_field_index - index, fld.signature()->char_at(0), fld.offset()); 877 } 878 879 return field_map; 880 } 881 882 // Helper class used to cache a ClassFileMap for the instance fields of 883 // a cache. A JvmtiCachedClassFieldMap can be cached by an InstanceKlass during 884 // heap iteration and avoid creating a field map for each object in the heap 885 // (only need to create the map when the first instance of a class is encountered). 886 // 887 class JvmtiCachedClassFieldMap : public CHeapObj<mtInternal> { 888 private: 889 enum { 890 initial_class_count = 200 891 }; 892 ClassFieldMap* _field_map; 893 894 ClassFieldMap* field_map() const { return _field_map; } 895 896 JvmtiCachedClassFieldMap(ClassFieldMap* field_map); 897 ~JvmtiCachedClassFieldMap(); 898 899 static GrowableArray<InstanceKlass*>* _class_list; 900 static void add_to_class_list(InstanceKlass* ik); 901 902 public: 903 // returns the field map for a given object (returning map cached 904 // by InstanceKlass if possible 905 static ClassFieldMap* get_map_of_instance_fields(oop obj); 906 907 // removes the field map from all instanceKlasses - should be 908 // called before VM operation completes 909 static void clear_cache(); 910 911 // returns the number of ClassFieldMap cached by instanceKlasses 912 static int cached_field_map_count(); 913 }; 914 915 GrowableArray<InstanceKlass*>* JvmtiCachedClassFieldMap::_class_list; 916 917 JvmtiCachedClassFieldMap::JvmtiCachedClassFieldMap(ClassFieldMap* field_map) { 918 _field_map = field_map; 919 } 920 921 JvmtiCachedClassFieldMap::~JvmtiCachedClassFieldMap() { 922 if (_field_map != NULL) { 923 delete _field_map; 924 } 925 } 926 927 // Marker class to ensure that the class file map cache is only used in a defined 928 // scope. 929 class ClassFieldMapCacheMark : public StackObj { 930 private: 931 static bool _is_active; 932 public: 933 ClassFieldMapCacheMark() { 934 assert(Thread::current()->is_VM_thread(), "must be VMThread"); 935 assert(JvmtiCachedClassFieldMap::cached_field_map_count() == 0, "cache not empty"); 936 assert(!_is_active, "ClassFieldMapCacheMark cannot be nested"); 937 _is_active = true; 938 } 939 ~ClassFieldMapCacheMark() { 940 JvmtiCachedClassFieldMap::clear_cache(); 941 _is_active = false; 942 } 943 static bool is_active() { return _is_active; } 944 }; 945 946 bool ClassFieldMapCacheMark::_is_active; 947 948 949 // record that the given InstanceKlass is caching a field map 950 void JvmtiCachedClassFieldMap::add_to_class_list(InstanceKlass* ik) { 951 if (_class_list == NULL) { 952 _class_list = new (ResourceObj::C_HEAP, mtInternal) 953 GrowableArray<InstanceKlass*>(initial_class_count, true); 954 } 955 _class_list->push(ik); 956 } 957 958 // returns the instance field map for the given object 959 // (returns field map cached by the InstanceKlass if possible) 960 ClassFieldMap* JvmtiCachedClassFieldMap::get_map_of_instance_fields(oop obj) { 961 assert(Thread::current()->is_VM_thread(), "must be VMThread"); 962 assert(ClassFieldMapCacheMark::is_active(), "ClassFieldMapCacheMark not active"); 963 964 Klass* k = obj->klass(); 965 InstanceKlass* ik = InstanceKlass::cast(k); 966 967 // return cached map if possible 968 JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map(); 969 if (cached_map != NULL) { 970 assert(cached_map->field_map() != NULL, "missing field list"); 971 return cached_map->field_map(); 972 } else { 973 ClassFieldMap* field_map = ClassFieldMap::create_map_of_instance_fields(obj); 974 cached_map = new JvmtiCachedClassFieldMap(field_map); 975 ik->set_jvmti_cached_class_field_map(cached_map); 976 add_to_class_list(ik); 977 return field_map; 978 } 979 } 980 981 // remove the fields maps cached from all instanceKlasses 982 void JvmtiCachedClassFieldMap::clear_cache() { 983 assert(Thread::current()->is_VM_thread(), "must be VMThread"); 984 if (_class_list != NULL) { 985 for (int i = 0; i < _class_list->length(); i++) { 986 InstanceKlass* ik = _class_list->at(i); 987 JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map(); 988 assert(cached_map != NULL, "should not be NULL"); 989 ik->set_jvmti_cached_class_field_map(NULL); 990 delete cached_map; // deletes the encapsulated field map 991 } 992 delete _class_list; 993 _class_list = NULL; 994 } 995 } 996 997 // returns the number of ClassFieldMap cached by instanceKlasses 998 int JvmtiCachedClassFieldMap::cached_field_map_count() { 999 return (_class_list == NULL) ? 0 : _class_list->length(); 1000 } 1001 1002 // helper function to indicate if an object is filtered by its tag or class tag 1003 static inline bool is_filtered_by_heap_filter(jlong obj_tag, 1004 jlong klass_tag, 1005 int heap_filter) { 1006 // apply the heap filter 1007 if (obj_tag != 0) { 1008 // filter out tagged objects 1009 if (heap_filter & JVMTI_HEAP_FILTER_TAGGED) return true; 1010 } else { 1011 // filter out untagged objects 1012 if (heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) return true; 1013 } 1014 if (klass_tag != 0) { 1015 // filter out objects with tagged classes 1016 if (heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) return true; 1017 } else { 1018 // filter out objects with untagged classes. 1019 if (heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) return true; 1020 } 1021 return false; 1022 } 1023 1024 // helper function to indicate if an object is filtered by a klass filter 1025 static inline bool is_filtered_by_klass_filter(oop obj, Klass* klass_filter) { 1026 if (klass_filter != NULL) { 1027 if (obj->klass() != klass_filter) { 1028 return true; 1029 } 1030 } 1031 return false; 1032 } 1033 1034 // helper function to tell if a field is a primitive field or not 1035 static inline bool is_primitive_field_type(char type) { 1036 return (type != 'L' && type != '['); 1037 } 1038 1039 // helper function to copy the value from location addr to jvalue. 1040 static inline void copy_to_jvalue(jvalue *v, address addr, jvmtiPrimitiveType value_type) { 1041 switch (value_type) { 1042 case JVMTI_PRIMITIVE_TYPE_BOOLEAN : { v->z = *(jboolean*)addr; break; } 1043 case JVMTI_PRIMITIVE_TYPE_BYTE : { v->b = *(jbyte*)addr; break; } 1044 case JVMTI_PRIMITIVE_TYPE_CHAR : { v->c = *(jchar*)addr; break; } 1045 case JVMTI_PRIMITIVE_TYPE_SHORT : { v->s = *(jshort*)addr; break; } 1046 case JVMTI_PRIMITIVE_TYPE_INT : { v->i = *(jint*)addr; break; } 1047 case JVMTI_PRIMITIVE_TYPE_LONG : { v->j = *(jlong*)addr; break; } 1048 case JVMTI_PRIMITIVE_TYPE_FLOAT : { v->f = *(jfloat*)addr; break; } 1049 case JVMTI_PRIMITIVE_TYPE_DOUBLE : { v->d = *(jdouble*)addr; break; } 1050 default: ShouldNotReachHere(); 1051 } 1052 } 1053 1054 // helper function to invoke string primitive value callback 1055 // returns visit control flags 1056 static jint invoke_string_value_callback(jvmtiStringPrimitiveValueCallback cb, 1057 CallbackWrapper* wrapper, 1058 oop str, 1059 void* user_data) 1060 { 1061 assert(str->klass() == SystemDictionary::String_klass(), "not a string"); 1062 1063 typeArrayOop s_value = java_lang_String::value(str); 1064 1065 // JDK-6584008: the value field may be null if a String instance is 1066 // partially constructed. 1067 if (s_value == NULL) { 1068 return 0; 1069 } 1070 // get the string value and length 1071 // (string value may be offset from the base) 1072 int s_len = java_lang_String::length(str); 1073 bool is_latin1 = java_lang_String::is_latin1(str); 1074 jchar* value; 1075 if (s_len > 0) { 1076 if (!is_latin1) { 1077 value = s_value->char_at_addr(0); 1078 } else { 1079 // Inflate latin1 encoded string to UTF16 1080 jchar* buf = NEW_C_HEAP_ARRAY(jchar, s_len, mtInternal); 1081 for (int i = 0; i < s_len; i++) { 1082 buf[i] = ((jchar) s_value->byte_at(i)) & 0xff; 1083 } 1084 value = &buf[0]; 1085 } 1086 } else { 1087 // Don't use char_at_addr(0) if length is 0 1088 value = (jchar*) s_value->base(T_CHAR); 1089 } 1090 1091 // invoke the callback 1092 jint res = (*cb)(wrapper->klass_tag(), 1093 wrapper->obj_size(), 1094 wrapper->obj_tag_p(), 1095 value, 1096 (jint)s_len, 1097 user_data); 1098 1099 if (is_latin1 && s_len > 0) { 1100 FREE_C_HEAP_ARRAY(jchar, value); 1101 } 1102 return res; 1103 } 1104 1105 // helper function to invoke string primitive value callback 1106 // returns visit control flags 1107 static jint invoke_array_primitive_value_callback(jvmtiArrayPrimitiveValueCallback cb, 1108 CallbackWrapper* wrapper, 1109 oop obj, 1110 void* user_data) 1111 { 1112 assert(obj->is_typeArray(), "not a primitive array"); 1113 1114 // get base address of first element 1115 typeArrayOop array = typeArrayOop(obj); 1116 BasicType type = TypeArrayKlass::cast(array->klass())->element_type(); 1117 void* elements = array->base(type); 1118 1119 // jvmtiPrimitiveType is defined so this mapping is always correct 1120 jvmtiPrimitiveType elem_type = (jvmtiPrimitiveType)type2char(type); 1121 1122 return (*cb)(wrapper->klass_tag(), 1123 wrapper->obj_size(), 1124 wrapper->obj_tag_p(), 1125 (jint)array->length(), 1126 elem_type, 1127 elements, 1128 user_data); 1129 } 1130 1131 // helper function to invoke the primitive field callback for all static fields 1132 // of a given class 1133 static jint invoke_primitive_field_callback_for_static_fields 1134 (CallbackWrapper* wrapper, 1135 oop obj, 1136 jvmtiPrimitiveFieldCallback cb, 1137 void* user_data) 1138 { 1139 // for static fields only the index will be set 1140 static jvmtiHeapReferenceInfo reference_info = { 0 }; 1141 1142 assert(obj->klass() == SystemDictionary::Class_klass(), "not a class"); 1143 if (java_lang_Class::is_primitive(obj)) { 1144 return 0; 1145 } 1146 Klass* klass = java_lang_Class::as_Klass(obj); 1147 1148 // ignore classes for object and type arrays 1149 if (!klass->is_instance_klass()) { 1150 return 0; 1151 } 1152 1153 // ignore classes which aren't linked yet 1154 InstanceKlass* ik = InstanceKlass::cast(klass); 1155 if (!ik->is_linked()) { 1156 return 0; 1157 } 1158 1159 // get the field map 1160 ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(klass); 1161 1162 // invoke the callback for each static primitive field 1163 for (int i=0; i<field_map->field_count(); i++) { 1164 ClassFieldDescriptor* field = field_map->field_at(i); 1165 1166 // ignore non-primitive fields 1167 char type = field->field_type(); 1168 if (!is_primitive_field_type(type)) { 1169 continue; 1170 } 1171 // one-to-one mapping 1172 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; 1173 1174 // get offset and field value 1175 int offset = field->field_offset(); 1176 address addr = (address)klass->java_mirror() + offset; 1177 jvalue value; 1178 copy_to_jvalue(&value, addr, value_type); 1179 1180 // field index 1181 reference_info.field.index = field->field_index(); 1182 1183 // invoke the callback 1184 jint res = (*cb)(JVMTI_HEAP_REFERENCE_STATIC_FIELD, 1185 &reference_info, 1186 wrapper->klass_tag(), 1187 wrapper->obj_tag_p(), 1188 value, 1189 value_type, 1190 user_data); 1191 if (res & JVMTI_VISIT_ABORT) { 1192 delete field_map; 1193 return res; 1194 } 1195 } 1196 1197 delete field_map; 1198 return 0; 1199 } 1200 1201 // helper function to invoke the primitive field callback for all instance fields 1202 // of a given object 1203 static jint invoke_primitive_field_callback_for_instance_fields( 1204 CallbackWrapper* wrapper, 1205 oop obj, 1206 jvmtiPrimitiveFieldCallback cb, 1207 void* user_data) 1208 { 1209 // for instance fields only the index will be set 1210 static jvmtiHeapReferenceInfo reference_info = { 0 }; 1211 1212 // get the map of the instance fields 1213 ClassFieldMap* fields = JvmtiCachedClassFieldMap::get_map_of_instance_fields(obj); 1214 1215 // invoke the callback for each instance primitive field 1216 for (int i=0; i<fields->field_count(); i++) { 1217 ClassFieldDescriptor* field = fields->field_at(i); 1218 1219 // ignore non-primitive fields 1220 char type = field->field_type(); 1221 if (!is_primitive_field_type(type)) { 1222 continue; 1223 } 1224 // one-to-one mapping 1225 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; 1226 1227 // get offset and field value 1228 int offset = field->field_offset(); 1229 address addr = (address)obj + offset; 1230 jvalue value; 1231 copy_to_jvalue(&value, addr, value_type); 1232 1233 // field index 1234 reference_info.field.index = field->field_index(); 1235 1236 // invoke the callback 1237 jint res = (*cb)(JVMTI_HEAP_REFERENCE_FIELD, 1238 &reference_info, 1239 wrapper->klass_tag(), 1240 wrapper->obj_tag_p(), 1241 value, 1242 value_type, 1243 user_data); 1244 if (res & JVMTI_VISIT_ABORT) { 1245 return res; 1246 } 1247 } 1248 return 0; 1249 } 1250 1251 1252 // VM operation to iterate over all objects in the heap (both reachable 1253 // and unreachable) 1254 class VM_HeapIterateOperation: public VM_Operation { 1255 private: 1256 ObjectClosure* _blk; 1257 public: 1258 VM_HeapIterateOperation(ObjectClosure* blk) { _blk = blk; } 1259 1260 VMOp_Type type() const { return VMOp_HeapIterateOperation; } 1261 void doit() { 1262 // allows class files maps to be cached during iteration 1263 ClassFieldMapCacheMark cm; 1264 1265 // make sure that heap is parsable (fills TLABs with filler objects) 1266 Universe::heap()->ensure_parsability(false); // no need to retire TLABs 1267 1268 // Verify heap before iteration - if the heap gets corrupted then 1269 // JVMTI's IterateOverHeap will crash. 1270 if (VerifyBeforeIteration) { 1271 Universe::verify(); 1272 } 1273 1274 // do the iteration 1275 // If this operation encounters a bad object when using CMS, 1276 // consider using safe_object_iterate() which avoids perm gen 1277 // objects that may contain bad references. 1278 Universe::heap()->object_iterate(_blk); 1279 } 1280 1281 }; 1282 1283 1284 // An ObjectClosure used to support the deprecated IterateOverHeap and 1285 // IterateOverInstancesOfClass functions 1286 class IterateOverHeapObjectClosure: public ObjectClosure { 1287 private: 1288 JvmtiTagMap* _tag_map; 1289 Klass* _klass; 1290 jvmtiHeapObjectFilter _object_filter; 1291 jvmtiHeapObjectCallback _heap_object_callback; 1292 const void* _user_data; 1293 1294 // accessors 1295 JvmtiTagMap* tag_map() const { return _tag_map; } 1296 jvmtiHeapObjectFilter object_filter() const { return _object_filter; } 1297 jvmtiHeapObjectCallback object_callback() const { return _heap_object_callback; } 1298 Klass* klass() const { return _klass; } 1299 const void* user_data() const { return _user_data; } 1300 1301 // indicates if iteration has been aborted 1302 bool _iteration_aborted; 1303 bool is_iteration_aborted() const { return _iteration_aborted; } 1304 void set_iteration_aborted(bool aborted) { _iteration_aborted = aborted; } 1305 1306 public: 1307 IterateOverHeapObjectClosure(JvmtiTagMap* tag_map, 1308 Klass* klass, 1309 jvmtiHeapObjectFilter object_filter, 1310 jvmtiHeapObjectCallback heap_object_callback, 1311 const void* user_data) : 1312 _tag_map(tag_map), 1313 _klass(klass), 1314 _object_filter(object_filter), 1315 _heap_object_callback(heap_object_callback), 1316 _user_data(user_data), 1317 _iteration_aborted(false) 1318 { 1319 } 1320 1321 void do_object(oop o); 1322 }; 1323 1324 // invoked for each object in the heap 1325 void IterateOverHeapObjectClosure::do_object(oop o) { 1326 // check if iteration has been halted 1327 if (is_iteration_aborted()) return; 1328 1329 // instanceof check when filtering by klass 1330 if (klass() != NULL && !o->is_a(klass())) { 1331 return; 1332 } 1333 // prepare for the calllback 1334 CallbackWrapper wrapper(tag_map(), o); 1335 1336 // if the object is tagged and we're only interested in untagged objects 1337 // then don't invoke the callback. Similiarly, if the object is untagged 1338 // and we're only interested in tagged objects we skip the callback. 1339 if (wrapper.obj_tag() != 0) { 1340 if (object_filter() == JVMTI_HEAP_OBJECT_UNTAGGED) return; 1341 } else { 1342 if (object_filter() == JVMTI_HEAP_OBJECT_TAGGED) return; 1343 } 1344 1345 // invoke the agent's callback 1346 jvmtiIterationControl control = (*object_callback())(wrapper.klass_tag(), 1347 wrapper.obj_size(), 1348 wrapper.obj_tag_p(), 1349 (void*)user_data()); 1350 if (control == JVMTI_ITERATION_ABORT) { 1351 set_iteration_aborted(true); 1352 } 1353 } 1354 1355 // An ObjectClosure used to support the IterateThroughHeap function 1356 class IterateThroughHeapObjectClosure: public ObjectClosure { 1357 private: 1358 JvmtiTagMap* _tag_map; 1359 Klass* _klass; 1360 int _heap_filter; 1361 const jvmtiHeapCallbacks* _callbacks; 1362 const void* _user_data; 1363 1364 // accessor functions 1365 JvmtiTagMap* tag_map() const { return _tag_map; } 1366 int heap_filter() const { return _heap_filter; } 1367 const jvmtiHeapCallbacks* callbacks() const { return _callbacks; } 1368 Klass* klass() const { return _klass; } 1369 const void* user_data() const { return _user_data; } 1370 1371 // indicates if the iteration has been aborted 1372 bool _iteration_aborted; 1373 bool is_iteration_aborted() const { return _iteration_aborted; } 1374 1375 // used to check the visit control flags. If the abort flag is set 1376 // then we set the iteration aborted flag so that the iteration completes 1377 // without processing any further objects 1378 bool check_flags_for_abort(jint flags) { 1379 bool is_abort = (flags & JVMTI_VISIT_ABORT) != 0; 1380 if (is_abort) { 1381 _iteration_aborted = true; 1382 } 1383 return is_abort; 1384 } 1385 1386 public: 1387 IterateThroughHeapObjectClosure(JvmtiTagMap* tag_map, 1388 Klass* klass, 1389 int heap_filter, 1390 const jvmtiHeapCallbacks* heap_callbacks, 1391 const void* user_data) : 1392 _tag_map(tag_map), 1393 _klass(klass), 1394 _heap_filter(heap_filter), 1395 _callbacks(heap_callbacks), 1396 _user_data(user_data), 1397 _iteration_aborted(false) 1398 { 1399 } 1400 1401 void do_object(oop o); 1402 }; 1403 1404 // invoked for each object in the heap 1405 void IterateThroughHeapObjectClosure::do_object(oop obj) { 1406 // check if iteration has been halted 1407 if (is_iteration_aborted()) return; 1408 1409 // apply class filter 1410 if (is_filtered_by_klass_filter(obj, klass())) return; 1411 1412 // prepare for callback 1413 CallbackWrapper wrapper(tag_map(), obj); 1414 1415 // check if filtered by the heap filter 1416 if (is_filtered_by_heap_filter(wrapper.obj_tag(), wrapper.klass_tag(), heap_filter())) { 1417 return; 1418 } 1419 1420 // for arrays we need the length, otherwise -1 1421 bool is_array = obj->is_array(); 1422 int len = is_array ? arrayOop(obj)->length() : -1; 1423 1424 // invoke the object callback (if callback is provided) 1425 if (callbacks()->heap_iteration_callback != NULL) { 1426 jvmtiHeapIterationCallback cb = callbacks()->heap_iteration_callback; 1427 jint res = (*cb)(wrapper.klass_tag(), 1428 wrapper.obj_size(), 1429 wrapper.obj_tag_p(), 1430 (jint)len, 1431 (void*)user_data()); 1432 if (check_flags_for_abort(res)) return; 1433 } 1434 1435 // for objects and classes we report primitive fields if callback provided 1436 if (callbacks()->primitive_field_callback != NULL && obj->is_instance()) { 1437 jint res; 1438 jvmtiPrimitiveFieldCallback cb = callbacks()->primitive_field_callback; 1439 if (obj->klass() == SystemDictionary::Class_klass()) { 1440 res = invoke_primitive_field_callback_for_static_fields(&wrapper, 1441 obj, 1442 cb, 1443 (void*)user_data()); 1444 } else { 1445 res = invoke_primitive_field_callback_for_instance_fields(&wrapper, 1446 obj, 1447 cb, 1448 (void*)user_data()); 1449 } 1450 if (check_flags_for_abort(res)) return; 1451 } 1452 1453 // string callback 1454 if (!is_array && 1455 callbacks()->string_primitive_value_callback != NULL && 1456 obj->klass() == SystemDictionary::String_klass()) { 1457 jint res = invoke_string_value_callback( 1458 callbacks()->string_primitive_value_callback, 1459 &wrapper, 1460 obj, 1461 (void*)user_data() ); 1462 if (check_flags_for_abort(res)) return; 1463 } 1464 1465 // array callback 1466 if (is_array && 1467 callbacks()->array_primitive_value_callback != NULL && 1468 obj->is_typeArray()) { 1469 jint res = invoke_array_primitive_value_callback( 1470 callbacks()->array_primitive_value_callback, 1471 &wrapper, 1472 obj, 1473 (void*)user_data() ); 1474 if (check_flags_for_abort(res)) return; 1475 } 1476 }; 1477 1478 1479 // Deprecated function to iterate over all objects in the heap 1480 void JvmtiTagMap::iterate_over_heap(jvmtiHeapObjectFilter object_filter, 1481 Klass* klass, 1482 jvmtiHeapObjectCallback heap_object_callback, 1483 const void* user_data) 1484 { 1485 // EA based optimizations on tagged objects are already reverted. 1486 JVMTIEscapeBarrier eb(JavaThread::current(), 1487 object_filter == JVMTI_HEAP_OBJECT_UNTAGGED || object_filter == JVMTI_HEAP_OBJECT_EITHER); 1488 eb.deoptimize_objects_all_threads(); 1489 MutexLocker ml(Heap_lock); 1490 IterateOverHeapObjectClosure blk(this, 1491 klass, 1492 object_filter, 1493 heap_object_callback, 1494 user_data); 1495 VM_HeapIterateOperation op(&blk); 1496 VMThread::execute(&op); 1497 } 1498 1499 1500 // Iterates over all objects in the heap 1501 void JvmtiTagMap::iterate_through_heap(jint heap_filter, 1502 Klass* klass, 1503 const jvmtiHeapCallbacks* callbacks, 1504 const void* user_data) 1505 { 1506 // EA based optimizations on tagged objects are already reverted. 1507 JVMTIEscapeBarrier eb(JavaThread::current(), !(heap_filter & JVMTI_HEAP_FILTER_UNTAGGED)); 1508 eb.deoptimize_objects_all_threads(); 1509 MutexLocker ml(Heap_lock); 1510 IterateThroughHeapObjectClosure blk(this, 1511 klass, 1512 heap_filter, 1513 callbacks, 1514 user_data); 1515 VM_HeapIterateOperation op(&blk); 1516 VMThread::execute(&op); 1517 } 1518 1519 // support class for get_objects_with_tags 1520 1521 class TagObjectCollector : public JvmtiTagHashmapEntryClosure { 1522 private: 1523 JvmtiEnv* _env; 1524 jlong* _tags; 1525 jint _tag_count; 1526 1527 GrowableArray<jobject>* _object_results; // collected objects (JNI weak refs) 1528 GrowableArray<uint64_t>* _tag_results; // collected tags 1529 1530 public: 1531 TagObjectCollector(JvmtiEnv* env, const jlong* tags, jint tag_count) { 1532 _env = env; 1533 _tags = (jlong*)tags; 1534 _tag_count = tag_count; 1535 _object_results = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jobject>(1,true); 1536 _tag_results = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<uint64_t>(1,true); 1537 } 1538 1539 ~TagObjectCollector() { 1540 delete _object_results; 1541 delete _tag_results; 1542 } 1543 1544 // for each tagged object check if the tag value matches 1545 // - if it matches then we create a JNI local reference to the object 1546 // and record the reference and tag value. 1547 // 1548 void do_entry(JvmtiTagHashmapEntry* entry) { 1549 for (int i=0; i<_tag_count; i++) { 1550 if (_tags[i] == entry->tag()) { 1551 // The reference in this tag map could be the only (implicitly weak) 1552 // reference to that object. If we hand it out, we need to keep it live wrt 1553 // SATB marking similar to other j.l.ref.Reference referents. This is 1554 // achieved by using a phantom load in the object() accessor. 1555 oop o = entry->object(); 1556 assert(o != NULL && Universe::heap()->is_in_reserved(o), "sanity check"); 1557 jobject ref = JNIHandles::make_local(JavaThread::current(), o); 1558 _object_results->append(ref); 1559 _tag_results->append((uint64_t)entry->tag()); 1560 } 1561 } 1562 } 1563 1564 // return the results from the collection 1565 // 1566 jvmtiError result(jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) { 1567 jvmtiError error; 1568 int count = _object_results->length(); 1569 assert(count >= 0, "sanity check"); 1570 1571 // if object_result_ptr is not NULL then allocate the result and copy 1572 // in the object references. 1573 if (object_result_ptr != NULL) { 1574 error = _env->Allocate(count * sizeof(jobject), (unsigned char**)object_result_ptr); 1575 if (error != JVMTI_ERROR_NONE) { 1576 return error; 1577 } 1578 for (int i=0; i<count; i++) { 1579 (*object_result_ptr)[i] = _object_results->at(i); 1580 } 1581 } 1582 1583 // if tag_result_ptr is not NULL then allocate the result and copy 1584 // in the tag values. 1585 if (tag_result_ptr != NULL) { 1586 error = _env->Allocate(count * sizeof(jlong), (unsigned char**)tag_result_ptr); 1587 if (error != JVMTI_ERROR_NONE) { 1588 if (object_result_ptr != NULL) { 1589 _env->Deallocate((unsigned char*)object_result_ptr); 1590 } 1591 return error; 1592 } 1593 for (int i=0; i<count; i++) { 1594 (*tag_result_ptr)[i] = (jlong)_tag_results->at(i); 1595 } 1596 } 1597 1598 *count_ptr = count; 1599 return JVMTI_ERROR_NONE; 1600 } 1601 }; 1602 1603 // return the list of objects with the specified tags 1604 jvmtiError JvmtiTagMap::get_objects_with_tags(const jlong* tags, 1605 jint count, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) { 1606 1607 TagObjectCollector collector(env(), tags, count); 1608 { 1609 // iterate over all tagged objects 1610 MutexLocker ml(lock()); 1611 entry_iterate(&collector); 1612 } 1613 return collector.result(count_ptr, object_result_ptr, tag_result_ptr); 1614 } 1615 1616 1617 // ObjectMarker is used to support the marking objects when walking the 1618 // heap. 1619 // 1620 // This implementation uses the existing mark bits in an object for 1621 // marking. Objects that are marked must later have their headers restored. 1622 // As most objects are unlocked and don't have their identity hash computed 1623 // we don't have to save their headers. Instead we save the headers that 1624 // are "interesting". Later when the headers are restored this implementation 1625 // restores all headers to their initial value and then restores the few 1626 // objects that had interesting headers. 1627 // 1628 // Future work: This implementation currently uses growable arrays to save 1629 // the oop and header of interesting objects. As an optimization we could 1630 // use the same technique as the GC and make use of the unused area 1631 // between top() and end(). 1632 // 1633 1634 // An ObjectClosure used to restore the mark bits of an object 1635 class RestoreMarksClosure : public ObjectClosure { 1636 public: 1637 void do_object(oop o) { 1638 if (o != NULL) { 1639 markWord mark = o->mark(); 1640 if (mark.is_marked()) { 1641 o->init_mark(); 1642 } 1643 } 1644 } 1645 }; 1646 1647 // ObjectMarker provides the mark and visited functions 1648 class ObjectMarker : AllStatic { 1649 private: 1650 // saved headers 1651 static GrowableArray<oop>* _saved_oop_stack; 1652 static GrowableArray<markWord>* _saved_mark_stack; 1653 static bool _needs_reset; // do we need to reset mark bits? 1654 1655 public: 1656 static void init(); // initialize 1657 static void done(); // clean-up 1658 1659 static inline void mark(oop o); // mark an object 1660 static inline bool visited(oop o); // check if object has been visited 1661 1662 static inline bool needs_reset() { return _needs_reset; } 1663 static inline void set_needs_reset(bool v) { _needs_reset = v; } 1664 }; 1665 1666 GrowableArray<oop>* ObjectMarker::_saved_oop_stack = NULL; 1667 GrowableArray<markWord>* ObjectMarker::_saved_mark_stack = NULL; 1668 bool ObjectMarker::_needs_reset = true; // need to reset mark bits by default 1669 1670 // initialize ObjectMarker - prepares for object marking 1671 void ObjectMarker::init() { 1672 assert(Thread::current()->is_VM_thread(), "must be VMThread"); 1673 1674 // prepare heap for iteration 1675 Universe::heap()->ensure_parsability(false); // no need to retire TLABs 1676 1677 // create stacks for interesting headers 1678 _saved_mark_stack = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<markWord>(4000, true); 1679 _saved_oop_stack = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(4000, true); 1680 1681 if (UseBiasedLocking) { 1682 BiasedLocking::preserve_marks(); 1683 } 1684 } 1685 1686 // Object marking is done so restore object headers 1687 void ObjectMarker::done() { 1688 // iterate over all objects and restore the mark bits to 1689 // their initial value 1690 RestoreMarksClosure blk; 1691 if (needs_reset()) { 1692 Universe::heap()->object_iterate(&blk); 1693 } else { 1694 // We don't need to reset mark bits on this call, but reset the 1695 // flag to the default for the next call. 1696 set_needs_reset(true); 1697 } 1698 1699 // now restore the interesting headers 1700 for (int i = 0; i < _saved_oop_stack->length(); i++) { 1701 oop o = _saved_oop_stack->at(i); 1702 markWord mark = _saved_mark_stack->at(i); 1703 o->set_mark(mark); 1704 } 1705 1706 if (UseBiasedLocking) { 1707 BiasedLocking::restore_marks(); 1708 } 1709 1710 // free the stacks 1711 delete _saved_oop_stack; 1712 delete _saved_mark_stack; 1713 } 1714 1715 // mark an object 1716 inline void ObjectMarker::mark(oop o) { 1717 assert(Universe::heap()->is_in(o), "sanity check"); 1718 assert(!o->mark().is_marked(), "should only mark an object once"); 1719 1720 // object's mark word 1721 markWord mark = o->mark(); 1722 1723 if (o->mark_must_be_preserved(mark)) { 1724 _saved_mark_stack->push(mark); 1725 _saved_oop_stack->push(o); 1726 } 1727 1728 // mark the object 1729 o->set_mark(markWord::prototype().set_marked()); 1730 } 1731 1732 // return true if object is marked 1733 inline bool ObjectMarker::visited(oop o) { 1734 return o->mark().is_marked(); 1735 } 1736 1737 // Stack allocated class to help ensure that ObjectMarker is used 1738 // correctly. Constructor initializes ObjectMarker, destructor calls 1739 // ObjectMarker's done() function to restore object headers. 1740 class ObjectMarkerController : public StackObj { 1741 public: 1742 ObjectMarkerController() { 1743 ObjectMarker::init(); 1744 } 1745 ~ObjectMarkerController() { 1746 ObjectMarker::done(); 1747 } 1748 }; 1749 1750 1751 // helper to map a jvmtiHeapReferenceKind to an old style jvmtiHeapRootKind 1752 // (not performance critical as only used for roots) 1753 static jvmtiHeapRootKind toJvmtiHeapRootKind(jvmtiHeapReferenceKind kind) { 1754 switch (kind) { 1755 case JVMTI_HEAP_REFERENCE_JNI_GLOBAL: return JVMTI_HEAP_ROOT_JNI_GLOBAL; 1756 case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: return JVMTI_HEAP_ROOT_SYSTEM_CLASS; 1757 case JVMTI_HEAP_REFERENCE_MONITOR: return JVMTI_HEAP_ROOT_MONITOR; 1758 case JVMTI_HEAP_REFERENCE_STACK_LOCAL: return JVMTI_HEAP_ROOT_STACK_LOCAL; 1759 case JVMTI_HEAP_REFERENCE_JNI_LOCAL: return JVMTI_HEAP_ROOT_JNI_LOCAL; 1760 case JVMTI_HEAP_REFERENCE_THREAD: return JVMTI_HEAP_ROOT_THREAD; 1761 case JVMTI_HEAP_REFERENCE_OTHER: return JVMTI_HEAP_ROOT_OTHER; 1762 default: ShouldNotReachHere(); return JVMTI_HEAP_ROOT_OTHER; 1763 } 1764 } 1765 1766 // Base class for all heap walk contexts. The base class maintains a flag 1767 // to indicate if the context is valid or not. 1768 class HeapWalkContext { 1769 private: 1770 bool _valid; 1771 public: 1772 HeapWalkContext(bool valid) { _valid = valid; } 1773 void invalidate() { _valid = false; } 1774 bool is_valid() const { return _valid; } 1775 }; 1776 1777 // A basic heap walk context for the deprecated heap walking functions. 1778 // The context for a basic heap walk are the callbacks and fields used by 1779 // the referrer caching scheme. 1780 class BasicHeapWalkContext: public HeapWalkContext { 1781 private: 1782 jvmtiHeapRootCallback _heap_root_callback; 1783 jvmtiStackReferenceCallback _stack_ref_callback; 1784 jvmtiObjectReferenceCallback _object_ref_callback; 1785 1786 // used for caching 1787 oop _last_referrer; 1788 jlong _last_referrer_tag; 1789 1790 public: 1791 BasicHeapWalkContext() : HeapWalkContext(false) { } 1792 1793 BasicHeapWalkContext(jvmtiHeapRootCallback heap_root_callback, 1794 jvmtiStackReferenceCallback stack_ref_callback, 1795 jvmtiObjectReferenceCallback object_ref_callback) : 1796 HeapWalkContext(true), 1797 _heap_root_callback(heap_root_callback), 1798 _stack_ref_callback(stack_ref_callback), 1799 _object_ref_callback(object_ref_callback), 1800 _last_referrer(NULL), 1801 _last_referrer_tag(0) { 1802 } 1803 1804 // accessors 1805 jvmtiHeapRootCallback heap_root_callback() const { return _heap_root_callback; } 1806 jvmtiStackReferenceCallback stack_ref_callback() const { return _stack_ref_callback; } 1807 jvmtiObjectReferenceCallback object_ref_callback() const { return _object_ref_callback; } 1808 1809 oop last_referrer() const { return _last_referrer; } 1810 void set_last_referrer(oop referrer) { _last_referrer = referrer; } 1811 jlong last_referrer_tag() const { return _last_referrer_tag; } 1812 void set_last_referrer_tag(jlong value) { _last_referrer_tag = value; } 1813 }; 1814 1815 // The advanced heap walk context for the FollowReferences functions. 1816 // The context is the callbacks, and the fields used for filtering. 1817 class AdvancedHeapWalkContext: public HeapWalkContext { 1818 private: 1819 jint _heap_filter; 1820 Klass* _klass_filter; 1821 const jvmtiHeapCallbacks* _heap_callbacks; 1822 1823 public: 1824 AdvancedHeapWalkContext() : HeapWalkContext(false) { } 1825 1826 AdvancedHeapWalkContext(jint heap_filter, 1827 Klass* klass_filter, 1828 const jvmtiHeapCallbacks* heap_callbacks) : 1829 HeapWalkContext(true), 1830 _heap_filter(heap_filter), 1831 _klass_filter(klass_filter), 1832 _heap_callbacks(heap_callbacks) { 1833 } 1834 1835 // accessors 1836 jint heap_filter() const { return _heap_filter; } 1837 Klass* klass_filter() const { return _klass_filter; } 1838 1839 const jvmtiHeapReferenceCallback heap_reference_callback() const { 1840 return _heap_callbacks->heap_reference_callback; 1841 }; 1842 const jvmtiPrimitiveFieldCallback primitive_field_callback() const { 1843 return _heap_callbacks->primitive_field_callback; 1844 } 1845 const jvmtiArrayPrimitiveValueCallback array_primitive_value_callback() const { 1846 return _heap_callbacks->array_primitive_value_callback; 1847 } 1848 const jvmtiStringPrimitiveValueCallback string_primitive_value_callback() const { 1849 return _heap_callbacks->string_primitive_value_callback; 1850 } 1851 }; 1852 1853 // The CallbackInvoker is a class with static functions that the heap walk can call 1854 // into to invoke callbacks. It works in one of two modes. The "basic" mode is 1855 // used for the deprecated IterateOverReachableObjects functions. The "advanced" 1856 // mode is for the newer FollowReferences function which supports a lot of 1857 // additional callbacks. 1858 class CallbackInvoker : AllStatic { 1859 private: 1860 // heap walk styles 1861 enum { basic, advanced }; 1862 static int _heap_walk_type; 1863 static bool is_basic_heap_walk() { return _heap_walk_type == basic; } 1864 static bool is_advanced_heap_walk() { return _heap_walk_type == advanced; } 1865 1866 // context for basic style heap walk 1867 static BasicHeapWalkContext _basic_context; 1868 static BasicHeapWalkContext* basic_context() { 1869 assert(_basic_context.is_valid(), "invalid"); 1870 return &_basic_context; 1871 } 1872 1873 // context for advanced style heap walk 1874 static AdvancedHeapWalkContext _advanced_context; 1875 static AdvancedHeapWalkContext* advanced_context() { 1876 assert(_advanced_context.is_valid(), "invalid"); 1877 return &_advanced_context; 1878 } 1879 1880 // context needed for all heap walks 1881 static JvmtiTagMap* _tag_map; 1882 static const void* _user_data; 1883 static GrowableArray<oop>* _visit_stack; 1884 1885 // accessors 1886 static JvmtiTagMap* tag_map() { return _tag_map; } 1887 static const void* user_data() { return _user_data; } 1888 static GrowableArray<oop>* visit_stack() { return _visit_stack; } 1889 1890 // if the object hasn't been visited then push it onto the visit stack 1891 // so that it will be visited later 1892 static inline bool check_for_visit(oop obj) { 1893 if (!ObjectMarker::visited(obj)) visit_stack()->push(obj); 1894 return true; 1895 } 1896 1897 // invoke basic style callbacks 1898 static inline bool invoke_basic_heap_root_callback 1899 (jvmtiHeapRootKind root_kind, oop obj); 1900 static inline bool invoke_basic_stack_ref_callback 1901 (jvmtiHeapRootKind root_kind, jlong thread_tag, jint depth, jmethodID method, 1902 int slot, oop obj); 1903 static inline bool invoke_basic_object_reference_callback 1904 (jvmtiObjectReferenceKind ref_kind, oop referrer, oop referree, jint index); 1905 1906 // invoke advanced style callbacks 1907 static inline bool invoke_advanced_heap_root_callback 1908 (jvmtiHeapReferenceKind ref_kind, oop obj); 1909 static inline bool invoke_advanced_stack_ref_callback 1910 (jvmtiHeapReferenceKind ref_kind, jlong thread_tag, jlong tid, int depth, 1911 jmethodID method, jlocation bci, jint slot, oop obj); 1912 static inline bool invoke_advanced_object_reference_callback 1913 (jvmtiHeapReferenceKind ref_kind, oop referrer, oop referree, jint index); 1914 1915 // used to report the value of primitive fields 1916 static inline bool report_primitive_field 1917 (jvmtiHeapReferenceKind ref_kind, oop obj, jint index, address addr, char type); 1918 1919 public: 1920 // initialize for basic mode 1921 static void initialize_for_basic_heap_walk(JvmtiTagMap* tag_map, 1922 GrowableArray<oop>* visit_stack, 1923 const void* user_data, 1924 BasicHeapWalkContext context); 1925 1926 // initialize for advanced mode 1927 static void initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map, 1928 GrowableArray<oop>* visit_stack, 1929 const void* user_data, 1930 AdvancedHeapWalkContext context); 1931 1932 // functions to report roots 1933 static inline bool report_simple_root(jvmtiHeapReferenceKind kind, oop o); 1934 static inline bool report_jni_local_root(jlong thread_tag, jlong tid, jint depth, 1935 jmethodID m, oop o); 1936 static inline bool report_stack_ref_root(jlong thread_tag, jlong tid, jint depth, 1937 jmethodID method, jlocation bci, jint slot, oop o); 1938 1939 // functions to report references 1940 static inline bool report_array_element_reference(oop referrer, oop referree, jint index); 1941 static inline bool report_class_reference(oop referrer, oop referree); 1942 static inline bool report_class_loader_reference(oop referrer, oop referree); 1943 static inline bool report_signers_reference(oop referrer, oop referree); 1944 static inline bool report_protection_domain_reference(oop referrer, oop referree); 1945 static inline bool report_superclass_reference(oop referrer, oop referree); 1946 static inline bool report_interface_reference(oop referrer, oop referree); 1947 static inline bool report_static_field_reference(oop referrer, oop referree, jint slot); 1948 static inline bool report_field_reference(oop referrer, oop referree, jint slot); 1949 static inline bool report_constant_pool_reference(oop referrer, oop referree, jint index); 1950 static inline bool report_primitive_array_values(oop array); 1951 static inline bool report_string_value(oop str); 1952 static inline bool report_primitive_instance_field(oop o, jint index, address value, char type); 1953 static inline bool report_primitive_static_field(oop o, jint index, address value, char type); 1954 }; 1955 1956 // statics 1957 int CallbackInvoker::_heap_walk_type; 1958 BasicHeapWalkContext CallbackInvoker::_basic_context; 1959 AdvancedHeapWalkContext CallbackInvoker::_advanced_context; 1960 JvmtiTagMap* CallbackInvoker::_tag_map; 1961 const void* CallbackInvoker::_user_data; 1962 GrowableArray<oop>* CallbackInvoker::_visit_stack; 1963 1964 // initialize for basic heap walk (IterateOverReachableObjects et al) 1965 void CallbackInvoker::initialize_for_basic_heap_walk(JvmtiTagMap* tag_map, 1966 GrowableArray<oop>* visit_stack, 1967 const void* user_data, 1968 BasicHeapWalkContext context) { 1969 _tag_map = tag_map; 1970 _visit_stack = visit_stack; 1971 _user_data = user_data; 1972 _basic_context = context; 1973 _advanced_context.invalidate(); // will trigger assertion if used 1974 _heap_walk_type = basic; 1975 } 1976 1977 // initialize for advanced heap walk (FollowReferences) 1978 void CallbackInvoker::initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map, 1979 GrowableArray<oop>* visit_stack, 1980 const void* user_data, 1981 AdvancedHeapWalkContext context) { 1982 _tag_map = tag_map; 1983 _visit_stack = visit_stack; 1984 _user_data = user_data; 1985 _advanced_context = context; 1986 _basic_context.invalidate(); // will trigger assertion if used 1987 _heap_walk_type = advanced; 1988 } 1989 1990 1991 // invoke basic style heap root callback 1992 inline bool CallbackInvoker::invoke_basic_heap_root_callback(jvmtiHeapRootKind root_kind, oop obj) { 1993 // if we heap roots should be reported 1994 jvmtiHeapRootCallback cb = basic_context()->heap_root_callback(); 1995 if (cb == NULL) { 1996 return check_for_visit(obj); 1997 } 1998 1999 CallbackWrapper wrapper(tag_map(), obj); 2000 jvmtiIterationControl control = (*cb)(root_kind, 2001 wrapper.klass_tag(), 2002 wrapper.obj_size(), 2003 wrapper.obj_tag_p(), 2004 (void*)user_data()); 2005 // push root to visit stack when following references 2006 if (control == JVMTI_ITERATION_CONTINUE && 2007 basic_context()->object_ref_callback() != NULL) { 2008 visit_stack()->push(obj); 2009 } 2010 return control != JVMTI_ITERATION_ABORT; 2011 } 2012 2013 // invoke basic style stack ref callback 2014 inline bool CallbackInvoker::invoke_basic_stack_ref_callback(jvmtiHeapRootKind root_kind, 2015 jlong thread_tag, 2016 jint depth, 2017 jmethodID method, 2018 int slot, 2019 oop obj) { 2020 // if we stack refs should be reported 2021 jvmtiStackReferenceCallback cb = basic_context()->stack_ref_callback(); 2022 if (cb == NULL) { 2023 return check_for_visit(obj); 2024 } 2025 2026 CallbackWrapper wrapper(tag_map(), obj); 2027 jvmtiIterationControl control = (*cb)(root_kind, 2028 wrapper.klass_tag(), 2029 wrapper.obj_size(), 2030 wrapper.obj_tag_p(), 2031 thread_tag, 2032 depth, 2033 method, 2034 slot, 2035 (void*)user_data()); 2036 // push root to visit stack when following references 2037 if (control == JVMTI_ITERATION_CONTINUE && 2038 basic_context()->object_ref_callback() != NULL) { 2039 visit_stack()->push(obj); 2040 } 2041 return control != JVMTI_ITERATION_ABORT; 2042 } 2043 2044 // invoke basic style object reference callback 2045 inline bool CallbackInvoker::invoke_basic_object_reference_callback(jvmtiObjectReferenceKind ref_kind, 2046 oop referrer, 2047 oop referree, 2048 jint index) { 2049 2050 BasicHeapWalkContext* context = basic_context(); 2051 2052 // callback requires the referrer's tag. If it's the same referrer 2053 // as the last call then we use the cached value. 2054 jlong referrer_tag; 2055 if (referrer == context->last_referrer()) { 2056 referrer_tag = context->last_referrer_tag(); 2057 } else { 2058 referrer_tag = tag_for(tag_map(), referrer); 2059 } 2060 2061 // do the callback 2062 CallbackWrapper wrapper(tag_map(), referree); 2063 jvmtiObjectReferenceCallback cb = context->object_ref_callback(); 2064 jvmtiIterationControl control = (*cb)(ref_kind, 2065 wrapper.klass_tag(), 2066 wrapper.obj_size(), 2067 wrapper.obj_tag_p(), 2068 referrer_tag, 2069 index, 2070 (void*)user_data()); 2071 2072 // record referrer and referrer tag. For self-references record the 2073 // tag value from the callback as this might differ from referrer_tag. 2074 context->set_last_referrer(referrer); 2075 if (referrer == referree) { 2076 context->set_last_referrer_tag(*wrapper.obj_tag_p()); 2077 } else { 2078 context->set_last_referrer_tag(referrer_tag); 2079 } 2080 2081 if (control == JVMTI_ITERATION_CONTINUE) { 2082 return check_for_visit(referree); 2083 } else { 2084 return control != JVMTI_ITERATION_ABORT; 2085 } 2086 } 2087 2088 // invoke advanced style heap root callback 2089 inline bool CallbackInvoker::invoke_advanced_heap_root_callback(jvmtiHeapReferenceKind ref_kind, 2090 oop obj) { 2091 AdvancedHeapWalkContext* context = advanced_context(); 2092 2093 // check that callback is provided 2094 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); 2095 if (cb == NULL) { 2096 return check_for_visit(obj); 2097 } 2098 2099 // apply class filter 2100 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { 2101 return check_for_visit(obj); 2102 } 2103 2104 // setup the callback wrapper 2105 CallbackWrapper wrapper(tag_map(), obj); 2106 2107 // apply tag filter 2108 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2109 wrapper.klass_tag(), 2110 context->heap_filter())) { 2111 return check_for_visit(obj); 2112 } 2113 2114 // for arrays we need the length, otherwise -1 2115 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); 2116 2117 // invoke the callback 2118 jint res = (*cb)(ref_kind, 2119 NULL, // referrer info 2120 wrapper.klass_tag(), 2121 0, // referrer_class_tag is 0 for heap root 2122 wrapper.obj_size(), 2123 wrapper.obj_tag_p(), 2124 NULL, // referrer_tag_p 2125 len, 2126 (void*)user_data()); 2127 if (res & JVMTI_VISIT_ABORT) { 2128 return false;// referrer class tag 2129 } 2130 if (res & JVMTI_VISIT_OBJECTS) { 2131 check_for_visit(obj); 2132 } 2133 return true; 2134 } 2135 2136 // report a reference from a thread stack to an object 2137 inline bool CallbackInvoker::invoke_advanced_stack_ref_callback(jvmtiHeapReferenceKind ref_kind, 2138 jlong thread_tag, 2139 jlong tid, 2140 int depth, 2141 jmethodID method, 2142 jlocation bci, 2143 jint slot, 2144 oop obj) { 2145 AdvancedHeapWalkContext* context = advanced_context(); 2146 2147 // check that callback is provider 2148 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); 2149 if (cb == NULL) { 2150 return check_for_visit(obj); 2151 } 2152 2153 // apply class filter 2154 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { 2155 return check_for_visit(obj); 2156 } 2157 2158 // setup the callback wrapper 2159 CallbackWrapper wrapper(tag_map(), obj); 2160 2161 // apply tag filter 2162 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2163 wrapper.klass_tag(), 2164 context->heap_filter())) { 2165 return check_for_visit(obj); 2166 } 2167 2168 // setup the referrer info 2169 jvmtiHeapReferenceInfo reference_info; 2170 reference_info.stack_local.thread_tag = thread_tag; 2171 reference_info.stack_local.thread_id = tid; 2172 reference_info.stack_local.depth = depth; 2173 reference_info.stack_local.method = method; 2174 reference_info.stack_local.location = bci; 2175 reference_info.stack_local.slot = slot; 2176 2177 // for arrays we need the length, otherwise -1 2178 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); 2179 2180 // call into the agent 2181 int res = (*cb)(ref_kind, 2182 &reference_info, 2183 wrapper.klass_tag(), 2184 0, // referrer_class_tag is 0 for heap root (stack) 2185 wrapper.obj_size(), 2186 wrapper.obj_tag_p(), 2187 NULL, // referrer_tag is 0 for root 2188 len, 2189 (void*)user_data()); 2190 2191 if (res & JVMTI_VISIT_ABORT) { 2192 return false; 2193 } 2194 if (res & JVMTI_VISIT_OBJECTS) { 2195 check_for_visit(obj); 2196 } 2197 return true; 2198 } 2199 2200 // This mask is used to pass reference_info to a jvmtiHeapReferenceCallback 2201 // only for ref_kinds defined by the JVM TI spec. Otherwise, NULL is passed. 2202 #define REF_INFO_MASK ((1 << JVMTI_HEAP_REFERENCE_FIELD) \ 2203 | (1 << JVMTI_HEAP_REFERENCE_STATIC_FIELD) \ 2204 | (1 << JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT) \ 2205 | (1 << JVMTI_HEAP_REFERENCE_CONSTANT_POOL) \ 2206 | (1 << JVMTI_HEAP_REFERENCE_STACK_LOCAL) \ 2207 | (1 << JVMTI_HEAP_REFERENCE_JNI_LOCAL)) 2208 2209 // invoke the object reference callback to report a reference 2210 inline bool CallbackInvoker::invoke_advanced_object_reference_callback(jvmtiHeapReferenceKind ref_kind, 2211 oop referrer, 2212 oop obj, 2213 jint index) 2214 { 2215 // field index is only valid field in reference_info 2216 static jvmtiHeapReferenceInfo reference_info = { 0 }; 2217 2218 AdvancedHeapWalkContext* context = advanced_context(); 2219 2220 // check that callback is provider 2221 jvmtiHeapReferenceCallback cb = context->heap_reference_callback(); 2222 if (cb == NULL) { 2223 return check_for_visit(obj); 2224 } 2225 2226 // apply class filter 2227 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { 2228 return check_for_visit(obj); 2229 } 2230 2231 // setup the callback wrapper 2232 TwoOopCallbackWrapper wrapper(tag_map(), referrer, obj); 2233 2234 // apply tag filter 2235 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2236 wrapper.klass_tag(), 2237 context->heap_filter())) { 2238 return check_for_visit(obj); 2239 } 2240 2241 // field index is only valid field in reference_info 2242 reference_info.field.index = index; 2243 2244 // for arrays we need the length, otherwise -1 2245 jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1); 2246 2247 // invoke the callback 2248 int res = (*cb)(ref_kind, 2249 (REF_INFO_MASK & (1 << ref_kind)) ? &reference_info : NULL, 2250 wrapper.klass_tag(), 2251 wrapper.referrer_klass_tag(), 2252 wrapper.obj_size(), 2253 wrapper.obj_tag_p(), 2254 wrapper.referrer_tag_p(), 2255 len, 2256 (void*)user_data()); 2257 2258 if (res & JVMTI_VISIT_ABORT) { 2259 return false; 2260 } 2261 if (res & JVMTI_VISIT_OBJECTS) { 2262 check_for_visit(obj); 2263 } 2264 return true; 2265 } 2266 2267 // report a "simple root" 2268 inline bool CallbackInvoker::report_simple_root(jvmtiHeapReferenceKind kind, oop obj) { 2269 assert(kind != JVMTI_HEAP_REFERENCE_STACK_LOCAL && 2270 kind != JVMTI_HEAP_REFERENCE_JNI_LOCAL, "not a simple root"); 2271 2272 if (is_basic_heap_walk()) { 2273 // map to old style root kind 2274 jvmtiHeapRootKind root_kind = toJvmtiHeapRootKind(kind); 2275 return invoke_basic_heap_root_callback(root_kind, obj); 2276 } else { 2277 assert(is_advanced_heap_walk(), "wrong heap walk type"); 2278 return invoke_advanced_heap_root_callback(kind, obj); 2279 } 2280 } 2281 2282 2283 // invoke the primitive array values 2284 inline bool CallbackInvoker::report_primitive_array_values(oop obj) { 2285 assert(obj->is_typeArray(), "not a primitive array"); 2286 2287 AdvancedHeapWalkContext* context = advanced_context(); 2288 assert(context->array_primitive_value_callback() != NULL, "no callback"); 2289 2290 // apply class filter 2291 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { 2292 return true; 2293 } 2294 2295 CallbackWrapper wrapper(tag_map(), obj); 2296 2297 // apply tag filter 2298 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2299 wrapper.klass_tag(), 2300 context->heap_filter())) { 2301 return true; 2302 } 2303 2304 // invoke the callback 2305 int res = invoke_array_primitive_value_callback(context->array_primitive_value_callback(), 2306 &wrapper, 2307 obj, 2308 (void*)user_data()); 2309 return (!(res & JVMTI_VISIT_ABORT)); 2310 } 2311 2312 // invoke the string value callback 2313 inline bool CallbackInvoker::report_string_value(oop str) { 2314 assert(str->klass() == SystemDictionary::String_klass(), "not a string"); 2315 2316 AdvancedHeapWalkContext* context = advanced_context(); 2317 assert(context->string_primitive_value_callback() != NULL, "no callback"); 2318 2319 // apply class filter 2320 if (is_filtered_by_klass_filter(str, context->klass_filter())) { 2321 return true; 2322 } 2323 2324 CallbackWrapper wrapper(tag_map(), str); 2325 2326 // apply tag filter 2327 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2328 wrapper.klass_tag(), 2329 context->heap_filter())) { 2330 return true; 2331 } 2332 2333 // invoke the callback 2334 int res = invoke_string_value_callback(context->string_primitive_value_callback(), 2335 &wrapper, 2336 str, 2337 (void*)user_data()); 2338 return (!(res & JVMTI_VISIT_ABORT)); 2339 } 2340 2341 // invoke the primitive field callback 2342 inline bool CallbackInvoker::report_primitive_field(jvmtiHeapReferenceKind ref_kind, 2343 oop obj, 2344 jint index, 2345 address addr, 2346 char type) 2347 { 2348 // for primitive fields only the index will be set 2349 static jvmtiHeapReferenceInfo reference_info = { 0 }; 2350 2351 AdvancedHeapWalkContext* context = advanced_context(); 2352 assert(context->primitive_field_callback() != NULL, "no callback"); 2353 2354 // apply class filter 2355 if (is_filtered_by_klass_filter(obj, context->klass_filter())) { 2356 return true; 2357 } 2358 2359 CallbackWrapper wrapper(tag_map(), obj); 2360 2361 // apply tag filter 2362 if (is_filtered_by_heap_filter(wrapper.obj_tag(), 2363 wrapper.klass_tag(), 2364 context->heap_filter())) { 2365 return true; 2366 } 2367 2368 // the field index in the referrer 2369 reference_info.field.index = index; 2370 2371 // map the type 2372 jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type; 2373 2374 // setup the jvalue 2375 jvalue value; 2376 copy_to_jvalue(&value, addr, value_type); 2377 2378 jvmtiPrimitiveFieldCallback cb = context->primitive_field_callback(); 2379 int res = (*cb)(ref_kind, 2380 &reference_info, 2381 wrapper.klass_tag(), 2382 wrapper.obj_tag_p(), 2383 value, 2384 value_type, 2385 (void*)user_data()); 2386 return (!(res & JVMTI_VISIT_ABORT)); 2387 } 2388 2389 2390 // instance field 2391 inline bool CallbackInvoker::report_primitive_instance_field(oop obj, 2392 jint index, 2393 address value, 2394 char type) { 2395 return report_primitive_field(JVMTI_HEAP_REFERENCE_FIELD, 2396 obj, 2397 index, 2398 value, 2399 type); 2400 } 2401 2402 // static field 2403 inline bool CallbackInvoker::report_primitive_static_field(oop obj, 2404 jint index, 2405 address value, 2406 char type) { 2407 return report_primitive_field(JVMTI_HEAP_REFERENCE_STATIC_FIELD, 2408 obj, 2409 index, 2410 value, 2411 type); 2412 } 2413 2414 // report a JNI local (root object) to the profiler 2415 inline bool CallbackInvoker::report_jni_local_root(jlong thread_tag, jlong tid, jint depth, jmethodID m, oop obj) { 2416 if (is_basic_heap_walk()) { 2417 return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_JNI_LOCAL, 2418 thread_tag, 2419 depth, 2420 m, 2421 -1, 2422 obj); 2423 } else { 2424 return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_JNI_LOCAL, 2425 thread_tag, tid, 2426 depth, 2427 m, 2428 (jlocation)-1, 2429 -1, 2430 obj); 2431 } 2432 } 2433 2434 2435 // report a local (stack reference, root object) 2436 inline bool CallbackInvoker::report_stack_ref_root(jlong thread_tag, 2437 jlong tid, 2438 jint depth, 2439 jmethodID method, 2440 jlocation bci, 2441 jint slot, 2442 oop obj) { 2443 if (is_basic_heap_walk()) { 2444 return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_STACK_LOCAL, 2445 thread_tag, 2446 depth, 2447 method, 2448 slot, 2449 obj); 2450 } else { 2451 return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_STACK_LOCAL, 2452 thread_tag, 2453 tid, 2454 depth, 2455 method, 2456 bci, 2457 slot, 2458 obj); 2459 } 2460 } 2461 2462 // report an object referencing a class. 2463 inline bool CallbackInvoker::report_class_reference(oop referrer, oop referree) { 2464 if (is_basic_heap_walk()) { 2465 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1); 2466 } else { 2467 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS, referrer, referree, -1); 2468 } 2469 } 2470 2471 // report a class referencing its class loader. 2472 inline bool CallbackInvoker::report_class_loader_reference(oop referrer, oop referree) { 2473 if (is_basic_heap_walk()) { 2474 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS_LOADER, referrer, referree, -1); 2475 } else { 2476 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS_LOADER, referrer, referree, -1); 2477 } 2478 } 2479 2480 // report a class referencing its signers. 2481 inline bool CallbackInvoker::report_signers_reference(oop referrer, oop referree) { 2482 if (is_basic_heap_walk()) { 2483 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_SIGNERS, referrer, referree, -1); 2484 } else { 2485 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SIGNERS, referrer, referree, -1); 2486 } 2487 } 2488 2489 // report a class referencing its protection domain.. 2490 inline bool CallbackInvoker::report_protection_domain_reference(oop referrer, oop referree) { 2491 if (is_basic_heap_walk()) { 2492 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1); 2493 } else { 2494 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1); 2495 } 2496 } 2497 2498 // report a class referencing its superclass. 2499 inline bool CallbackInvoker::report_superclass_reference(oop referrer, oop referree) { 2500 if (is_basic_heap_walk()) { 2501 // Send this to be consistent with past implementation 2502 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1); 2503 } else { 2504 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SUPERCLASS, referrer, referree, -1); 2505 } 2506 } 2507 2508 // report a class referencing one of its interfaces. 2509 inline bool CallbackInvoker::report_interface_reference(oop referrer, oop referree) { 2510 if (is_basic_heap_walk()) { 2511 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_INTERFACE, referrer, referree, -1); 2512 } else { 2513 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_INTERFACE, referrer, referree, -1); 2514 } 2515 } 2516 2517 // report a class referencing one of its static fields. 2518 inline bool CallbackInvoker::report_static_field_reference(oop referrer, oop referree, jint slot) { 2519 if (is_basic_heap_walk()) { 2520 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_STATIC_FIELD, referrer, referree, slot); 2521 } else { 2522 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_STATIC_FIELD, referrer, referree, slot); 2523 } 2524 } 2525 2526 // report an array referencing an element object 2527 inline bool CallbackInvoker::report_array_element_reference(oop referrer, oop referree, jint index) { 2528 if (is_basic_heap_walk()) { 2529 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_ARRAY_ELEMENT, referrer, referree, index); 2530 } else { 2531 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT, referrer, referree, index); 2532 } 2533 } 2534 2535 // report an object referencing an instance field object 2536 inline bool CallbackInvoker::report_field_reference(oop referrer, oop referree, jint slot) { 2537 if (is_basic_heap_walk()) { 2538 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_FIELD, referrer, referree, slot); 2539 } else { 2540 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_FIELD, referrer, referree, slot); 2541 } 2542 } 2543 2544 // report an array referencing an element object 2545 inline bool CallbackInvoker::report_constant_pool_reference(oop referrer, oop referree, jint index) { 2546 if (is_basic_heap_walk()) { 2547 return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CONSTANT_POOL, referrer, referree, index); 2548 } else { 2549 return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CONSTANT_POOL, referrer, referree, index); 2550 } 2551 } 2552 2553 // A supporting closure used to process simple roots 2554 class SimpleRootsClosure : public OopClosure { 2555 private: 2556 jvmtiHeapReferenceKind _kind; 2557 bool _continue; 2558 2559 jvmtiHeapReferenceKind root_kind() { return _kind; } 2560 2561 public: 2562 void set_kind(jvmtiHeapReferenceKind kind) { 2563 _kind = kind; 2564 _continue = true; 2565 } 2566 2567 inline bool stopped() { 2568 return !_continue; 2569 } 2570 2571 void do_oop(oop* obj_p) { 2572 // iteration has terminated 2573 if (stopped()) { 2574 return; 2575 } 2576 2577 oop o = NativeAccess<AS_NO_KEEPALIVE>::oop_load(obj_p); 2578 // ignore null 2579 if (o == NULL) { 2580 return; 2581 } 2582 2583 assert(Universe::heap()->is_in_reserved(o), "should be impossible"); 2584 2585 jvmtiHeapReferenceKind kind = root_kind(); 2586 if (kind == JVMTI_HEAP_REFERENCE_SYSTEM_CLASS) { 2587 // SystemDictionary::oops_do reports the application 2588 // class loader as a root. We want this root to be reported as 2589 // a root kind of "OTHER" rather than "SYSTEM_CLASS". 2590 if (!o->is_instance() || !InstanceKlass::cast(o->klass())->is_mirror_instance_klass()) { 2591 kind = JVMTI_HEAP_REFERENCE_OTHER; 2592 } 2593 } 2594 2595 // invoke the callback 2596 _continue = CallbackInvoker::report_simple_root(kind, o); 2597 2598 } 2599 virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } 2600 }; 2601 2602 // A supporting closure used to process JNI locals 2603 class JNILocalRootsClosure : public OopClosure { 2604 private: 2605 jlong _thread_tag; 2606 jlong _tid; 2607 jint _depth; 2608 jmethodID _method; 2609 bool _continue; 2610 public: 2611 void set_context(jlong thread_tag, jlong tid, jint depth, jmethodID method) { 2612 _thread_tag = thread_tag; 2613 _tid = tid; 2614 _depth = depth; 2615 _method = method; 2616 _continue = true; 2617 } 2618 2619 inline bool stopped() { 2620 return !_continue; 2621 } 2622 2623 void do_oop(oop* obj_p) { 2624 // iteration has terminated 2625 if (stopped()) { 2626 return; 2627 } 2628 2629 oop o = *obj_p; 2630 // ignore null 2631 if (o == NULL) { 2632 return; 2633 } 2634 2635 // invoke the callback 2636 _continue = CallbackInvoker::report_jni_local_root(_thread_tag, _tid, _depth, _method, o); 2637 } 2638 virtual void do_oop(narrowOop* obj_p) { ShouldNotReachHere(); } 2639 }; 2640 2641 2642 // A VM operation to iterate over objects that are reachable from 2643 // a set of roots or an initial object. 2644 // 2645 // For VM_HeapWalkOperation the set of roots used is :- 2646 // 2647 // - All JNI global references 2648 // - All inflated monitors 2649 // - All classes loaded by the boot class loader (or all classes 2650 // in the event that class unloading is disabled) 2651 // - All java threads 2652 // - For each java thread then all locals and JNI local references 2653 // on the thread's execution stack 2654 // - All visible/explainable objects from Universes::oops_do 2655 // 2656 class VM_HeapWalkOperation: public VM_Operation { 2657 private: 2658 enum { 2659 initial_visit_stack_size = 4000 2660 }; 2661 2662 bool _is_advanced_heap_walk; // indicates FollowReferences 2663 JvmtiTagMap* _tag_map; 2664 Handle _initial_object; 2665 GrowableArray<oop>* _visit_stack; // the visit stack 2666 2667 bool _collecting_heap_roots; // are we collecting roots 2668 bool _following_object_refs; // are we following object references 2669 2670 bool _reporting_primitive_fields; // optional reporting 2671 bool _reporting_primitive_array_values; 2672 bool _reporting_string_values; 2673 2674 GrowableArray<oop>* create_visit_stack() { 2675 return new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(initial_visit_stack_size, true); 2676 } 2677 2678 // accessors 2679 bool is_advanced_heap_walk() const { return _is_advanced_heap_walk; } 2680 JvmtiTagMap* tag_map() const { return _tag_map; } 2681 Handle initial_object() const { return _initial_object; } 2682 2683 bool is_following_references() const { return _following_object_refs; } 2684 2685 bool is_reporting_primitive_fields() const { return _reporting_primitive_fields; } 2686 bool is_reporting_primitive_array_values() const { return _reporting_primitive_array_values; } 2687 bool is_reporting_string_values() const { return _reporting_string_values; } 2688 2689 GrowableArray<oop>* visit_stack() const { return _visit_stack; } 2690 2691 // iterate over the various object types 2692 inline bool iterate_over_array(oop o); 2693 inline bool iterate_over_type_array(oop o); 2694 inline bool iterate_over_class(oop o); 2695 inline bool iterate_over_object(oop o); 2696 2697 // root collection 2698 inline bool collect_simple_roots(); 2699 inline bool collect_stack_roots(); 2700 inline bool collect_stack_roots(JavaThread* java_thread, JNILocalRootsClosure* blk); 2701 2702 // visit an object 2703 inline bool visit(oop o); 2704 2705 public: 2706 VM_HeapWalkOperation(JvmtiTagMap* tag_map, 2707 Handle initial_object, 2708 BasicHeapWalkContext callbacks, 2709 const void* user_data); 2710 2711 VM_HeapWalkOperation(JvmtiTagMap* tag_map, 2712 Handle initial_object, 2713 AdvancedHeapWalkContext callbacks, 2714 const void* user_data); 2715 2716 ~VM_HeapWalkOperation(); 2717 2718 VMOp_Type type() const { return VMOp_HeapWalkOperation; } 2719 void doit(); 2720 }; 2721 2722 2723 VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map, 2724 Handle initial_object, 2725 BasicHeapWalkContext callbacks, 2726 const void* user_data) { 2727 _is_advanced_heap_walk = false; 2728 _tag_map = tag_map; 2729 _initial_object = initial_object; 2730 _following_object_refs = (callbacks.object_ref_callback() != NULL); 2731 _reporting_primitive_fields = false; 2732 _reporting_primitive_array_values = false; 2733 _reporting_string_values = false; 2734 _visit_stack = create_visit_stack(); 2735 2736 2737 CallbackInvoker::initialize_for_basic_heap_walk(tag_map, _visit_stack, user_data, callbacks); 2738 } 2739 2740 VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map, 2741 Handle initial_object, 2742 AdvancedHeapWalkContext callbacks, 2743 const void* user_data) { 2744 _is_advanced_heap_walk = true; 2745 _tag_map = tag_map; 2746 _initial_object = initial_object; 2747 _following_object_refs = true; 2748 _reporting_primitive_fields = (callbacks.primitive_field_callback() != NULL);; 2749 _reporting_primitive_array_values = (callbacks.array_primitive_value_callback() != NULL);; 2750 _reporting_string_values = (callbacks.string_primitive_value_callback() != NULL);; 2751 _visit_stack = create_visit_stack(); 2752 2753 CallbackInvoker::initialize_for_advanced_heap_walk(tag_map, _visit_stack, user_data, callbacks); 2754 } 2755 2756 VM_HeapWalkOperation::~VM_HeapWalkOperation() { 2757 if (_following_object_refs) { 2758 assert(_visit_stack != NULL, "checking"); 2759 delete _visit_stack; 2760 _visit_stack = NULL; 2761 } 2762 } 2763 2764 // an array references its class and has a reference to 2765 // each element in the array 2766 inline bool VM_HeapWalkOperation::iterate_over_array(oop o) { 2767 objArrayOop array = objArrayOop(o); 2768 2769 // array reference to its class 2770 oop mirror = ObjArrayKlass::cast(array->klass())->java_mirror(); 2771 if (!CallbackInvoker::report_class_reference(o, mirror)) { 2772 return false; 2773 } 2774 2775 // iterate over the array and report each reference to a 2776 // non-null element 2777 for (int index=0; index<array->length(); index++) { 2778 oop elem = array->obj_at(index); 2779 if (elem == NULL) { 2780 continue; 2781 } 2782 2783 // report the array reference o[index] = elem 2784 if (!CallbackInvoker::report_array_element_reference(o, elem, index)) { 2785 return false; 2786 } 2787 } 2788 return true; 2789 } 2790 2791 // a type array references its class 2792 inline bool VM_HeapWalkOperation::iterate_over_type_array(oop o) { 2793 Klass* k = o->klass(); 2794 oop mirror = k->java_mirror(); 2795 if (!CallbackInvoker::report_class_reference(o, mirror)) { 2796 return false; 2797 } 2798 2799 // report the array contents if required 2800 if (is_reporting_primitive_array_values()) { 2801 if (!CallbackInvoker::report_primitive_array_values(o)) { 2802 return false; 2803 } 2804 } 2805 return true; 2806 } 2807 2808 #ifdef ASSERT 2809 // verify that a static oop field is in range 2810 static inline bool verify_static_oop(InstanceKlass* ik, 2811 oop mirror, int offset) { 2812 address obj_p = (address)mirror + offset; 2813 address start = (address)InstanceMirrorKlass::start_of_static_fields(mirror); 2814 address end = start + (java_lang_Class::static_oop_field_count(mirror) * heapOopSize); 2815 assert(end >= start, "sanity check"); 2816 2817 if (obj_p >= start && obj_p < end) { 2818 return true; 2819 } else { 2820 return false; 2821 } 2822 } 2823 #endif // #ifdef ASSERT 2824 2825 // a class references its super class, interfaces, class loader, ... 2826 // and finally its static fields 2827 inline bool VM_HeapWalkOperation::iterate_over_class(oop java_class) { 2828 int i; 2829 Klass* klass = java_lang_Class::as_Klass(java_class); 2830 2831 if (klass->is_instance_klass()) { 2832 InstanceKlass* ik = InstanceKlass::cast(klass); 2833 2834 // Ignore the class if it hasn't been initialized yet 2835 if (!ik->is_linked()) { 2836 return true; 2837 } 2838 2839 // get the java mirror 2840 oop mirror = klass->java_mirror(); 2841 2842 // super (only if something more interesting than java.lang.Object) 2843 InstanceKlass* java_super = ik->java_super(); 2844 if (java_super != NULL && java_super != SystemDictionary::Object_klass()) { 2845 oop super = java_super->java_mirror(); 2846 if (!CallbackInvoker::report_superclass_reference(mirror, super)) { 2847 return false; 2848 } 2849 } 2850 2851 // class loader 2852 oop cl = ik->class_loader(); 2853 if (cl != NULL) { 2854 if (!CallbackInvoker::report_class_loader_reference(mirror, cl)) { 2855 return false; 2856 } 2857 } 2858 2859 // protection domain 2860 oop pd = ik->protection_domain(); 2861 if (pd != NULL) { 2862 if (!CallbackInvoker::report_protection_domain_reference(mirror, pd)) { 2863 return false; 2864 } 2865 } 2866 2867 // signers 2868 oop signers = ik->signers(); 2869 if (signers != NULL) { 2870 if (!CallbackInvoker::report_signers_reference(mirror, signers)) { 2871 return false; 2872 } 2873 } 2874 2875 // references from the constant pool 2876 { 2877 ConstantPool* pool = ik->constants(); 2878 for (int i = 1; i < pool->length(); i++) { 2879 constantTag tag = pool->tag_at(i).value(); 2880 if (tag.is_string() || tag.is_klass() || tag.is_unresolved_klass()) { 2881 oop entry; 2882 if (tag.is_string()) { 2883 entry = pool->resolved_string_at(i); 2884 // If the entry is non-null it is resolved. 2885 if (entry == NULL) { 2886 continue; 2887 } 2888 } else if (tag.is_klass()) { 2889 entry = pool->resolved_klass_at(i)->java_mirror(); 2890 } else { 2891 // Code generated by JIT and AOT compilers might not resolve constant 2892 // pool entries. Treat them as resolved if they are loaded. 2893 assert(tag.is_unresolved_klass(), "must be"); 2894 constantPoolHandle cp(Thread::current(), pool); 2895 Klass* klass = ConstantPool::klass_at_if_loaded(cp, i); 2896 if (klass == NULL) { 2897 continue; 2898 } 2899 entry = klass->java_mirror(); 2900 } 2901 if (!CallbackInvoker::report_constant_pool_reference(mirror, entry, (jint)i)) { 2902 return false; 2903 } 2904 } 2905 } 2906 } 2907 2908 // interfaces 2909 // (These will already have been reported as references from the constant pool 2910 // but are specified by IterateOverReachableObjects and must be reported). 2911 Array<InstanceKlass*>* interfaces = ik->local_interfaces(); 2912 for (i = 0; i < interfaces->length(); i++) { 2913 oop interf = interfaces->at(i)->java_mirror(); 2914 if (interf == NULL) { 2915 continue; 2916 } 2917 if (!CallbackInvoker::report_interface_reference(mirror, interf)) { 2918 return false; 2919 } 2920 } 2921 2922 // iterate over the static fields 2923 2924 ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(klass); 2925 for (i=0; i<field_map->field_count(); i++) { 2926 ClassFieldDescriptor* field = field_map->field_at(i); 2927 char type = field->field_type(); 2928 if (!is_primitive_field_type(type)) { 2929 oop fld_o = mirror->obj_field(field->field_offset()); 2930 assert(verify_static_oop(ik, mirror, field->field_offset()), "sanity check"); 2931 if (fld_o != NULL) { 2932 int slot = field->field_index(); 2933 if (!CallbackInvoker::report_static_field_reference(mirror, fld_o, slot)) { 2934 delete field_map; 2935 return false; 2936 } 2937 } 2938 } else { 2939 if (is_reporting_primitive_fields()) { 2940 address addr = (address)mirror + field->field_offset(); 2941 int slot = field->field_index(); 2942 if (!CallbackInvoker::report_primitive_static_field(mirror, slot, addr, type)) { 2943 delete field_map; 2944 return false; 2945 } 2946 } 2947 } 2948 } 2949 delete field_map; 2950 2951 return true; 2952 } 2953 2954 return true; 2955 } 2956 2957 // an object references a class and its instance fields 2958 // (static fields are ignored here as we report these as 2959 // references from the class). 2960 inline bool VM_HeapWalkOperation::iterate_over_object(oop o) { 2961 // reference to the class 2962 if (!CallbackInvoker::report_class_reference(o, o->klass()->java_mirror())) { 2963 return false; 2964 } 2965 2966 // iterate over instance fields 2967 ClassFieldMap* field_map = JvmtiCachedClassFieldMap::get_map_of_instance_fields(o); 2968 for (int i=0; i<field_map->field_count(); i++) { 2969 ClassFieldDescriptor* field = field_map->field_at(i); 2970 char type = field->field_type(); 2971 if (!is_primitive_field_type(type)) { 2972 oop fld_o = o->obj_field(field->field_offset()); 2973 // ignore any objects that aren't visible to profiler 2974 if (fld_o != NULL) { 2975 assert(Universe::heap()->is_in_reserved(fld_o), "unsafe code should not " 2976 "have references to Klass* anymore"); 2977 int slot = field->field_index(); 2978 if (!CallbackInvoker::report_field_reference(o, fld_o, slot)) { 2979 return false; 2980 } 2981 } 2982 } else { 2983 if (is_reporting_primitive_fields()) { 2984 // primitive instance field 2985 address addr = (address)o + field->field_offset(); 2986 int slot = field->field_index(); 2987 if (!CallbackInvoker::report_primitive_instance_field(o, slot, addr, type)) { 2988 return false; 2989 } 2990 } 2991 } 2992 } 2993 2994 // if the object is a java.lang.String 2995 if (is_reporting_string_values() && 2996 o->klass() == SystemDictionary::String_klass()) { 2997 if (!CallbackInvoker::report_string_value(o)) { 2998 return false; 2999 } 3000 } 3001 return true; 3002 } 3003 3004 3005 // Collects all simple (non-stack) roots except for threads; 3006 // threads are handled in collect_stack_roots() as an optimization. 3007 // if there's a heap root callback provided then the callback is 3008 // invoked for each simple root. 3009 // if an object reference callback is provided then all simple 3010 // roots are pushed onto the marking stack so that they can be 3011 // processed later 3012 // 3013 inline bool VM_HeapWalkOperation::collect_simple_roots() { 3014 SimpleRootsClosure blk; 3015 3016 // JNI globals 3017 blk.set_kind(JVMTI_HEAP_REFERENCE_JNI_GLOBAL); 3018 JNIHandles::oops_do(&blk); 3019 if (blk.stopped()) { 3020 return false; 3021 } 3022 3023 // Preloaded classes and loader from the system dictionary 3024 blk.set_kind(JVMTI_HEAP_REFERENCE_SYSTEM_CLASS); 3025 SystemDictionary::oops_do(&blk); 3026 CLDToOopClosure cld_closure(&blk, false); 3027 ClassLoaderDataGraph::always_strong_cld_do(&cld_closure); 3028 if (blk.stopped()) { 3029 return false; 3030 } 3031 3032 // Inflated monitors 3033 blk.set_kind(JVMTI_HEAP_REFERENCE_MONITOR); 3034 ObjectSynchronizer::oops_do(&blk); 3035 if (blk.stopped()) { 3036 return false; 3037 } 3038 3039 // threads are now handled in collect_stack_roots() 3040 3041 // Other kinds of roots maintained by HotSpot 3042 // Many of these won't be visible but others (such as instances of important 3043 // exceptions) will be visible. 3044 blk.set_kind(JVMTI_HEAP_REFERENCE_OTHER); 3045 Universe::oops_do(&blk); 3046 if (blk.stopped()) { 3047 return false; 3048 } 3049 3050 return true; 3051 } 3052 3053 // Walk the stack of a given thread and find all references (locals 3054 // and JNI calls) and report these as stack references 3055 inline bool VM_HeapWalkOperation::collect_stack_roots(JavaThread* java_thread, 3056 JNILocalRootsClosure* blk) 3057 { 3058 oop threadObj = java_thread->threadObj(); 3059 assert(threadObj != NULL, "sanity check"); 3060 3061 // only need to get the thread's tag once per thread 3062 jlong thread_tag = tag_for(_tag_map, threadObj); 3063 3064 // also need the thread id 3065 jlong tid = java_lang_Thread::thread_id(threadObj); 3066 3067 3068 if (java_thread->has_last_Java_frame()) { 3069 3070 // vframes are resource allocated 3071 Thread* current_thread = Thread::current(); 3072 ResourceMark rm(current_thread); 3073 HandleMark hm(current_thread); 3074 3075 RegisterMap reg_map(java_thread); 3076 frame f = java_thread->last_frame(); 3077 vframe* vf = vframe::new_vframe(&f, ®_map, java_thread); 3078 3079 bool is_top_frame = true; 3080 int depth = 0; 3081 frame* last_entry_frame = NULL; 3082 3083 while (vf != NULL) { 3084 if (vf->is_java_frame()) { 3085 3086 // java frame (interpreted, compiled, ...) 3087 javaVFrame *jvf = javaVFrame::cast(vf); 3088 3089 // the jmethodID 3090 jmethodID method = jvf->method()->jmethod_id(); 3091 3092 if (!(jvf->method()->is_native())) { 3093 jlocation bci = (jlocation)jvf->bci(); 3094 StackValueCollection* locals = jvf->locals(); 3095 for (int slot=0; slot<locals->size(); slot++) { 3096 if (locals->at(slot)->type() == T_OBJECT) { 3097 oop o = locals->obj_at(slot)(); 3098 if (o == NULL) { 3099 continue; 3100 } 3101 3102 // stack reference 3103 if (!CallbackInvoker::report_stack_ref_root(thread_tag, tid, depth, method, 3104 bci, slot, o)) { 3105 return false; 3106 } 3107 } 3108 } 3109 3110 StackValueCollection* exprs = jvf->expressions(); 3111 for (int index=0; index < exprs->size(); index++) { 3112 if (exprs->at(index)->type() == T_OBJECT) { 3113 oop o = exprs->obj_at(index)(); 3114 if (o == NULL) { 3115 continue; 3116 } 3117 3118 // stack reference 3119 if (!CallbackInvoker::report_stack_ref_root(thread_tag, tid, depth, method, 3120 bci, locals->size() + index, o)) { 3121 return false; 3122 } 3123 } 3124 } 3125 3126 // Follow oops from compiled nmethod 3127 if (jvf->cb() != NULL && jvf->cb()->is_nmethod()) { 3128 blk->set_context(thread_tag, tid, depth, method); 3129 jvf->cb()->as_nmethod()->oops_do(blk); 3130 } 3131 } else { 3132 blk->set_context(thread_tag, tid, depth, method); 3133 if (is_top_frame) { 3134 // JNI locals for the top frame. 3135 java_thread->active_handles()->oops_do(blk); 3136 } else { 3137 if (last_entry_frame != NULL) { 3138 // JNI locals for the entry frame 3139 assert(last_entry_frame->is_entry_frame(), "checking"); 3140 last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(blk); 3141 } 3142 } 3143 } 3144 last_entry_frame = NULL; 3145 depth++; 3146 } else { 3147 // externalVFrame - for an entry frame then we report the JNI locals 3148 // when we find the corresponding javaVFrame 3149 frame* fr = vf->frame_pointer(); 3150 assert(fr != NULL, "sanity check"); 3151 if (fr->is_entry_frame()) { 3152 last_entry_frame = fr; 3153 } 3154 } 3155 3156 vf = vf->sender(); 3157 is_top_frame = false; 3158 } 3159 } else { 3160 // no last java frame but there may be JNI locals 3161 blk->set_context(thread_tag, tid, 0, (jmethodID)NULL); 3162 java_thread->active_handles()->oops_do(blk); 3163 } 3164 return true; 3165 } 3166 3167 3168 // Collects the simple roots for all threads and collects all 3169 // stack roots - for each thread it walks the execution 3170 // stack to find all references and local JNI refs. 3171 inline bool VM_HeapWalkOperation::collect_stack_roots() { 3172 JNILocalRootsClosure blk; 3173 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) { 3174 oop threadObj = thread->threadObj(); 3175 if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) { 3176 // Collect the simple root for this thread before we 3177 // collect its stack roots 3178 if (!CallbackInvoker::report_simple_root(JVMTI_HEAP_REFERENCE_THREAD, 3179 threadObj)) { 3180 return false; 3181 } 3182 if (!collect_stack_roots(thread, &blk)) { 3183 return false; 3184 } 3185 } 3186 } 3187 return true; 3188 } 3189 3190 // visit an object 3191 // first mark the object as visited 3192 // second get all the outbound references from this object (in other words, all 3193 // the objects referenced by this object). 3194 // 3195 bool VM_HeapWalkOperation::visit(oop o) { 3196 // mark object as visited 3197 assert(!ObjectMarker::visited(o), "can't visit same object more than once"); 3198 ObjectMarker::mark(o); 3199 3200 // instance 3201 if (o->is_instance()) { 3202 if (o->klass() == SystemDictionary::Class_klass()) { 3203 if (!java_lang_Class::is_primitive(o)) { 3204 // a java.lang.Class 3205 return iterate_over_class(o); 3206 } 3207 } else { 3208 return iterate_over_object(o); 3209 } 3210 } 3211 3212 // object array 3213 if (o->is_objArray()) { 3214 return iterate_over_array(o); 3215 } 3216 3217 // type array 3218 if (o->is_typeArray()) { 3219 return iterate_over_type_array(o); 3220 } 3221 3222 return true; 3223 } 3224 3225 void VM_HeapWalkOperation::doit() { 3226 ResourceMark rm; 3227 ObjectMarkerController marker; 3228 ClassFieldMapCacheMark cm; 3229 3230 assert(visit_stack()->is_empty(), "visit stack must be empty"); 3231 3232 // the heap walk starts with an initial object or the heap roots 3233 if (initial_object().is_null()) { 3234 // If either collect_stack_roots() or collect_simple_roots() 3235 // returns false at this point, then there are no mark bits 3236 // to reset. 3237 ObjectMarker::set_needs_reset(false); 3238 3239 // Calling collect_stack_roots() before collect_simple_roots() 3240 // can result in a big performance boost for an agent that is 3241 // focused on analyzing references in the thread stacks. 3242 if (!collect_stack_roots()) return; 3243 3244 if (!collect_simple_roots()) return; 3245 3246 // no early return so enable heap traversal to reset the mark bits 3247 ObjectMarker::set_needs_reset(true); 3248 } else { 3249 visit_stack()->push(initial_object()()); 3250 } 3251 3252 // object references required 3253 if (is_following_references()) { 3254 3255 // visit each object until all reachable objects have been 3256 // visited or the callback asked to terminate the iteration. 3257 while (!visit_stack()->is_empty()) { 3258 oop o = visit_stack()->pop(); 3259 if (!ObjectMarker::visited(o)) { 3260 if (!visit(o)) { 3261 break; 3262 } 3263 } 3264 } 3265 } 3266 } 3267 3268 // iterate over all objects that are reachable from a set of roots 3269 void JvmtiTagMap::iterate_over_reachable_objects(jvmtiHeapRootCallback heap_root_callback, 3270 jvmtiStackReferenceCallback stack_ref_callback, 3271 jvmtiObjectReferenceCallback object_ref_callback, 3272 const void* user_data) { 3273 JavaThread* jt = JavaThread::current(); 3274 JVMTIEscapeBarrier eb(jt, true); 3275 eb.deoptimize_objects_all_threads(); 3276 MutexLocker ml(Heap_lock); 3277 BasicHeapWalkContext context(heap_root_callback, stack_ref_callback, object_ref_callback); 3278 VM_HeapWalkOperation op(this, Handle(), context, user_data); 3279 VMThread::execute(&op); 3280 } 3281 3282 // iterate over all objects that are reachable from a given object 3283 void JvmtiTagMap::iterate_over_objects_reachable_from_object(jobject object, 3284 jvmtiObjectReferenceCallback object_ref_callback, 3285 const void* user_data) { 3286 oop obj = JNIHandles::resolve(object); 3287 Handle initial_object(Thread::current(), obj); 3288 3289 MutexLocker ml(Heap_lock); 3290 BasicHeapWalkContext context(NULL, NULL, object_ref_callback); 3291 VM_HeapWalkOperation op(this, initial_object, context, user_data); 3292 VMThread::execute(&op); 3293 } 3294 3295 // follow references from an initial object or the GC roots 3296 void JvmtiTagMap::follow_references(jint heap_filter, 3297 Klass* klass, 3298 jobject object, 3299 const jvmtiHeapCallbacks* callbacks, 3300 const void* user_data) 3301 { 3302 oop obj = JNIHandles::resolve(object); 3303 JavaThread* jt = JavaThread::current(); 3304 Handle initial_object(jt, obj); 3305 // EA based optimizations that are tagged or reachable from initial_object are already reverted. 3306 JVMTIEscapeBarrier eb(jt, 3307 initial_object.is_null() && !(heap_filter & JVMTI_HEAP_FILTER_UNTAGGED)); 3308 eb.deoptimize_objects_all_threads(); 3309 MutexLocker ml(Heap_lock); 3310 AdvancedHeapWalkContext context(heap_filter, klass, callbacks); 3311 VM_HeapWalkOperation op(this, initial_object, context, user_data); 3312 VMThread::execute(&op); 3313 } 3314 3315 3316 void JvmtiTagMap::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { 3317 // No locks during VM bring-up (0 threads) and no safepoints after main 3318 // thread creation and before VMThread creation (1 thread); initial GC 3319 // verification can happen in that window which gets to here. 3320 assert(Threads::number_of_threads() <= 1 || 3321 SafepointSynchronize::is_at_safepoint(), 3322 "must be executed at a safepoint"); 3323 if (JvmtiEnv::environments_might_exist()) { 3324 JvmtiEnvIterator it; 3325 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { 3326 JvmtiTagMap* tag_map = env->tag_map_acquire(); 3327 if (tag_map != NULL && !tag_map->is_empty()) { 3328 tag_map->do_weak_oops(is_alive, f); 3329 } 3330 } 3331 } 3332 } 3333 3334 void JvmtiTagMap::do_weak_oops(BoolObjectClosure* is_alive, OopClosure* f) { 3335 3336 // does this environment have the OBJECT_FREE event enabled 3337 bool post_object_free = env()->is_enabled(JVMTI_EVENT_OBJECT_FREE); 3338 3339 // counters used for trace message 3340 int freed = 0; 3341 int moved = 0; 3342 3343 JvmtiTagHashmap* hashmap = this->hashmap(); 3344 3345 // reenable sizing (if disabled) 3346 hashmap->set_resizing_enabled(true); 3347 3348 // if the hashmap is empty then we can skip it 3349 if (hashmap->_entry_count == 0) { 3350 return; 3351 } 3352 3353 // now iterate through each entry in the table 3354 3355 JvmtiTagHashmapEntry** table = hashmap->table(); 3356 int size = hashmap->size(); 3357 3358 JvmtiTagHashmapEntry* delayed_add = NULL; 3359 3360 for (int pos = 0; pos < size; ++pos) { 3361 JvmtiTagHashmapEntry* entry = table[pos]; 3362 JvmtiTagHashmapEntry* prev = NULL; 3363 3364 while (entry != NULL) { 3365 JvmtiTagHashmapEntry* next = entry->next(); 3366 3367 // has object been GC'ed 3368 if (!is_alive->do_object_b(entry->object_peek())) { 3369 // grab the tag 3370 jlong tag = entry->tag(); 3371 guarantee(tag != 0, "checking"); 3372 3373 // remove GC'ed entry from hashmap and return the 3374 // entry to the free list 3375 hashmap->remove(prev, pos, entry); 3376 destroy_entry(entry); 3377 3378 // post the event to the profiler 3379 if (post_object_free) { 3380 JvmtiExport::post_object_free(env(), tag); 3381 } 3382 3383 ++freed; 3384 } else { 3385 f->do_oop(entry->object_addr()); 3386 oop new_oop = entry->object_peek(); 3387 3388 // if the object has moved then re-hash it and move its 3389 // entry to its new location. 3390 unsigned int new_pos = JvmtiTagHashmap::hash(new_oop, size); 3391 if (new_pos != (unsigned int)pos) { 3392 if (prev == NULL) { 3393 table[pos] = next; 3394 } else { 3395 prev->set_next(next); 3396 } 3397 if (new_pos < (unsigned int)pos) { 3398 entry->set_next(table[new_pos]); 3399 table[new_pos] = entry; 3400 } else { 3401 // Delay adding this entry to it's new position as we'd end up 3402 // hitting it again during this iteration. 3403 entry->set_next(delayed_add); 3404 delayed_add = entry; 3405 } 3406 moved++; 3407 } else { 3408 // object didn't move 3409 prev = entry; 3410 } 3411 } 3412 3413 entry = next; 3414 } 3415 } 3416 3417 // Re-add all the entries which were kept aside 3418 while (delayed_add != NULL) { 3419 JvmtiTagHashmapEntry* next = delayed_add->next(); 3420 unsigned int pos = JvmtiTagHashmap::hash(delayed_add->object_peek(), size); 3421 delayed_add->set_next(table[pos]); 3422 table[pos] = delayed_add; 3423 delayed_add = next; 3424 } 3425 3426 log_debug(jvmti, objecttagging)("(%d->%d, %d freed, %d total moves)", 3427 hashmap->_entry_count + freed, hashmap->_entry_count, freed, moved); 3428 }