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