src/share/vm/prims/jvmtiTagMap.cpp

Thu, 24 May 2018 17:06:56 +0800

author
aoqi
date
Thu, 24 May 2018 17:06:56 +0800
changeset 8604
04d83ba48607
parent 7994
04ff2f6cd0eb
child 9448
73d689add964
permissions
-rw-r--r--

Merge

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

mercurial