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