src/share/vm/prims/jvmtiTagMap.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 9448
73d689add964
child 9756
2be326848943
permissions
-rw-r--r--

Merge

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

mercurial