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