src/share/vm/prims/jvmtiTagMap.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6992
2c6ef90f030a
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial