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