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