annotate src/share/vm/prims/jvmtiTagMap.cpp @ 2125:7246a374a9f2

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