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