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