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