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