1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/dictionary.hpp" 27 #include "classfile/javaClasses.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "gc/shared/collectedHeap.inline.hpp" 31 #include "logging/log.hpp" 32 #include "memory/heapInspection.hpp" 33 #include "memory/metadataFactory.hpp" 34 #include "memory/metaspaceClosure.hpp" 35 #include "memory/metaspaceShared.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/access.inline.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/klass.inline.hpp" 41 #include "oops/oop.inline.hpp" 42 #include "runtime/atomic.hpp" 43 #include "runtime/orderAccess.inline.hpp" 44 #include "trace/traceMacros.hpp" 45 #include "utilities/macros.hpp" 46 #include "utilities/stack.inline.hpp" 47 48 void Klass::set_java_mirror(Handle m) { 49 assert(!m.is_null(), "New mirror should never be null."); 50 assert(_java_mirror.resolve() == NULL, "should only be used to initialize mirror"); 51 _java_mirror = class_loader_data()->add_handle(m); 52 } 53 54 oop Klass::java_mirror() const { 55 return _java_mirror.resolve(); 56 } 57 58 oop Klass::java_mirror_phantom() { 59 // Loading the klass_holder as a phantom oop ref keeps the class alive. 60 // After the holder has been kept alive, it is safe to return the mirror. 61 (void)klass_holder_phantom(); 62 return java_mirror(); 63 } 64 65 oop Klass::klass_holder_phantom() { 66 oop* addr = &class_loader_data()->_class_loader; 67 return RootAccess<IN_CONCURRENT_ROOT | ON_PHANTOM_OOP_REF>::oop_load(addr); 68 } 69 70 bool Klass::is_cloneable() const { 71 return _access_flags.is_cloneable_fast() || 72 is_subtype_of(SystemDictionary::Cloneable_klass()); 73 } 74 75 void Klass::set_is_cloneable() { 76 if (name() != vmSymbols::java_lang_invoke_MemberName()) { 77 _access_flags.set_is_cloneable_fast(); 78 } else { 79 assert(is_final(), "no subclasses allowed"); 80 // MemberName cloning should not be intrinsified and always happen in JVM_Clone. 81 } 82 } 83 84 void Klass::set_name(Symbol* n) { 85 _name = n; 86 if (_name != NULL) _name->increment_refcount(); 87 } 88 89 bool Klass::is_subclass_of(const Klass* k) const { 90 // Run up the super chain and check 91 if (this == k) return true; 92 93 Klass* t = const_cast<Klass*>(this)->super(); 94 95 while (t != NULL) { 96 if (t == k) return true; 97 t = t->super(); 98 } 99 return false; 100 } 101 102 bool Klass::search_secondary_supers(Klass* k) const { 103 // Put some extra logic here out-of-line, before the search proper. 104 // This cuts down the size of the inline method. 105 106 // This is necessary, since I am never in my own secondary_super list. 107 if (this == k) 108 return true; 109 // Scan the array-of-objects for a match 110 int cnt = secondary_supers()->length(); 111 for (int i = 0; i < cnt; i++) { 112 if (secondary_supers()->at(i) == k) { 113 ((Klass*)this)->set_secondary_super_cache(k); 114 return true; 115 } 116 } 117 return false; 118 } 119 120 // Return self, except for abstract classes with exactly 1 121 // implementor. Then return the 1 concrete implementation. 122 Klass *Klass::up_cast_abstract() { 123 Klass *r = this; 124 while( r->is_abstract() ) { // Receiver is abstract? 125 Klass *s = r->subklass(); // Check for exactly 1 subklass 126 if( !s || s->next_sibling() ) // Oops; wrong count; give up 127 return this; // Return 'this' as a no-progress flag 128 r = s; // Loop till find concrete class 129 } 130 return r; // Return the 1 concrete class 131 } 132 133 // Find LCA in class hierarchy 134 Klass *Klass::LCA( Klass *k2 ) { 135 Klass *k1 = this; 136 while( 1 ) { 137 if( k1->is_subtype_of(k2) ) return k2; 138 if( k2->is_subtype_of(k1) ) return k1; 139 k1 = k1->super(); 140 k2 = k2->super(); 141 } 142 } 143 144 145 void Klass::check_valid_for_instantiation(bool throwError, TRAPS) { 146 ResourceMark rm(THREAD); 147 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError() 148 : vmSymbols::java_lang_InstantiationException(), external_name()); 149 } 150 151 152 void Klass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) { 153 THROW(vmSymbols::java_lang_ArrayStoreException()); 154 } 155 156 157 void Klass::initialize(TRAPS) { 158 ShouldNotReachHere(); 159 } 160 161 bool Klass::compute_is_subtype_of(Klass* k) { 162 assert(k->is_klass(), "argument must be a class"); 163 return is_subclass_of(k); 164 } 165 166 Klass* Klass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const { 167 #ifdef ASSERT 168 tty->print_cr("Error: find_field called on a klass oop." 169 " Likely error: reflection method does not correctly" 170 " wrap return value in a mirror object."); 171 #endif 172 ShouldNotReachHere(); 173 return NULL; 174 } 175 176 Method* Klass::uncached_lookup_method(const Symbol* name, const Symbol* signature, OverpassLookupMode overpass_mode) const { 177 #ifdef ASSERT 178 tty->print_cr("Error: uncached_lookup_method called on a klass oop." 179 " Likely error: reflection method does not correctly" 180 " wrap return value in a mirror object."); 181 #endif 182 ShouldNotReachHere(); 183 return NULL; 184 } 185 186 void* Klass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw() { 187 return Metaspace::allocate(loader_data, word_size, MetaspaceObj::ClassType, THREAD); 188 } 189 190 // "Normal" instantiation is preceeded by a MetaspaceObj allocation 191 // which zeros out memory - calloc equivalent. 192 // The constructor is also used from CppVtableCloner, 193 // which doesn't zero out the memory before calling the constructor. 194 // Need to set the _java_mirror field explicitly to not hit an assert that the field 195 // should be NULL before setting it. 196 Klass::Klass() : _prototype_header(markOopDesc::prototype()), 197 _shared_class_path_index(-1), 198 _java_mirror(NULL) { 199 200 _primary_supers[0] = this; 201 set_super_check_offset(in_bytes(primary_supers_offset())); 202 } 203 204 jint Klass::array_layout_helper(BasicType etype) { 205 assert(etype >= T_BOOLEAN && etype <= T_OBJECT, "valid etype"); 206 // Note that T_ARRAY is not allowed here. 207 int hsize = arrayOopDesc::base_offset_in_bytes(etype); 208 int esize = type2aelembytes(etype); 209 bool isobj = (etype == T_OBJECT); 210 int tag = isobj ? _lh_array_tag_obj_value : _lh_array_tag_type_value; 211 int lh = array_layout_helper(tag, hsize, etype, exact_log2(esize)); 212 213 assert(lh < (int)_lh_neutral_value, "must look like an array layout"); 214 assert(layout_helper_is_array(lh), "correct kind"); 215 assert(layout_helper_is_objArray(lh) == isobj, "correct kind"); 216 assert(layout_helper_is_typeArray(lh) == !isobj, "correct kind"); 217 assert(layout_helper_header_size(lh) == hsize, "correct decode"); 218 assert(layout_helper_element_type(lh) == etype, "correct decode"); 219 assert(1 << layout_helper_log2_element_size(lh) == esize, "correct decode"); 220 221 return lh; 222 } 223 224 bool Klass::can_be_primary_super_slow() const { 225 if (super() == NULL) 226 return true; 227 else if (super()->super_depth() >= primary_super_limit()-1) 228 return false; 229 else 230 return true; 231 } 232 233 void Klass::initialize_supers(Klass* k, TRAPS) { 234 if (FastSuperclassLimit == 0) { 235 // None of the other machinery matters. 236 set_super(k); 237 return; 238 } 239 if (k == NULL) { 240 set_super(NULL); 241 _primary_supers[0] = this; 242 assert(super_depth() == 0, "Object must already be initialized properly"); 243 } else if (k != super() || k == SystemDictionary::Object_klass()) { 244 assert(super() == NULL || super() == SystemDictionary::Object_klass(), 245 "initialize this only once to a non-trivial value"); 246 set_super(k); 247 Klass* sup = k; 248 int sup_depth = sup->super_depth(); 249 juint my_depth = MIN2(sup_depth + 1, (int)primary_super_limit()); 250 if (!can_be_primary_super_slow()) 251 my_depth = primary_super_limit(); 252 for (juint i = 0; i < my_depth; i++) { 253 _primary_supers[i] = sup->_primary_supers[i]; 254 } 255 Klass* *super_check_cell; 256 if (my_depth < primary_super_limit()) { 257 _primary_supers[my_depth] = this; 258 super_check_cell = &_primary_supers[my_depth]; 259 } else { 260 // Overflow of the primary_supers array forces me to be secondary. 261 super_check_cell = &_secondary_super_cache; 262 } 263 set_super_check_offset((address)super_check_cell - (address) this); 264 265 #ifdef ASSERT 266 { 267 juint j = super_depth(); 268 assert(j == my_depth, "computed accessor gets right answer"); 269 Klass* t = this; 270 while (!t->can_be_primary_super()) { 271 t = t->super(); 272 j = t->super_depth(); 273 } 274 for (juint j1 = j+1; j1 < primary_super_limit(); j1++) { 275 assert(primary_super_of_depth(j1) == NULL, "super list padding"); 276 } 277 while (t != NULL) { 278 assert(primary_super_of_depth(j) == t, "super list initialization"); 279 t = t->super(); 280 --j; 281 } 282 assert(j == (juint)-1, "correct depth count"); 283 } 284 #endif 285 } 286 287 if (secondary_supers() == NULL) { 288 289 // Now compute the list of secondary supertypes. 290 // Secondaries can occasionally be on the super chain, 291 // if the inline "_primary_supers" array overflows. 292 int extras = 0; 293 Klass* p; 294 for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) { 295 ++extras; 296 } 297 298 ResourceMark rm(THREAD); // need to reclaim GrowableArrays allocated below 299 300 // Compute the "real" non-extra secondaries. 301 GrowableArray<Klass*>* secondaries = compute_secondary_supers(extras); 302 if (secondaries == NULL) { 303 // secondary_supers set by compute_secondary_supers 304 return; 305 } 306 307 GrowableArray<Klass*>* primaries = new GrowableArray<Klass*>(extras); 308 309 for (p = super(); !(p == NULL || p->can_be_primary_super()); p = p->super()) { 310 int i; // Scan for overflow primaries being duplicates of 2nd'arys 311 312 // This happens frequently for very deeply nested arrays: the 313 // primary superclass chain overflows into the secondary. The 314 // secondary list contains the element_klass's secondaries with 315 // an extra array dimension added. If the element_klass's 316 // secondary list already contains some primary overflows, they 317 // (with the extra level of array-ness) will collide with the 318 // normal primary superclass overflows. 319 for( i = 0; i < secondaries->length(); i++ ) { 320 if( secondaries->at(i) == p ) 321 break; 322 } 323 if( i < secondaries->length() ) 324 continue; // It's a dup, don't put it in 325 primaries->push(p); 326 } 327 // Combine the two arrays into a metadata object to pack the array. 328 // The primaries are added in the reverse order, then the secondaries. 329 int new_length = primaries->length() + secondaries->length(); 330 Array<Klass*>* s2 = MetadataFactory::new_array<Klass*>( 331 class_loader_data(), new_length, CHECK); 332 int fill_p = primaries->length(); 333 for (int j = 0; j < fill_p; j++) { 334 s2->at_put(j, primaries->pop()); // add primaries in reverse order. 335 } 336 for( int j = 0; j < secondaries->length(); j++ ) { 337 s2->at_put(j+fill_p, secondaries->at(j)); // add secondaries on the end. 338 } 339 340 #ifdef ASSERT 341 // We must not copy any NULL placeholders left over from bootstrap. 342 for (int j = 0; j < s2->length(); j++) { 343 assert(s2->at(j) != NULL, "correct bootstrapping order"); 344 } 345 #endif 346 347 set_secondary_supers(s2); 348 } 349 } 350 351 GrowableArray<Klass*>* Klass::compute_secondary_supers(int num_extra_slots) { 352 assert(num_extra_slots == 0, "override for complex klasses"); 353 set_secondary_supers(Universe::the_empty_klass_array()); 354 return NULL; 355 } 356 357 358 InstanceKlass* Klass::superklass() const { 359 assert(super() == NULL || super()->is_instance_klass(), "must be instance klass"); 360 return _super == NULL ? NULL : InstanceKlass::cast(_super); 361 } 362 363 void Klass::set_subklass(Klass* s) { 364 assert(s != this, "sanity check"); 365 _subklass = s; 366 } 367 368 void Klass::set_next_sibling(Klass* s) { 369 assert(s != this, "sanity check"); 370 _next_sibling = s; 371 } 372 373 void Klass::append_to_sibling_list() { 374 debug_only(verify();) 375 // add ourselves to superklass' subklass list 376 InstanceKlass* super = superklass(); 377 if (super == NULL) return; // special case: class Object 378 assert((!super->is_interface() // interfaces cannot be supers 379 && (super->superklass() == NULL || !is_interface())), 380 "an interface can only be a subklass of Object"); 381 Klass* prev_first_subklass = super->subklass(); 382 if (prev_first_subklass != NULL) { 383 // set our sibling to be the superklass' previous first subklass 384 set_next_sibling(prev_first_subklass); 385 } 386 // make ourselves the superklass' first subklass 387 super->set_subklass(this); 388 debug_only(verify();) 389 } 390 391 bool Klass::is_loader_alive(BoolObjectClosure* is_alive) { 392 #ifdef ASSERT 393 // The class is alive iff the class loader is alive. 394 oop loader = class_loader(); 395 bool loader_alive = (loader == NULL) || is_alive->do_object_b(loader); 396 #endif // ASSERT 397 398 // The class is alive if it's mirror is alive (which should be marked if the 399 // loader is alive) unless it's an anoymous class. 400 bool mirror_alive = is_alive->do_object_b(java_mirror()); 401 assert(!mirror_alive || loader_alive, "loader must be alive if the mirror is" 402 " but not the other way around with anonymous classes"); 403 return mirror_alive; 404 } 405 406 void Klass::clean_weak_klass_links(BoolObjectClosure* is_alive, bool clean_alive_klasses) { 407 if (!ClassUnloading) { 408 return; 409 } 410 411 Klass* root = SystemDictionary::Object_klass(); 412 Stack<Klass*, mtGC> stack; 413 414 stack.push(root); 415 while (!stack.is_empty()) { 416 Klass* current = stack.pop(); 417 418 assert(current->is_loader_alive(is_alive), "just checking, this should be live"); 419 420 // Find and set the first alive subklass 421 Klass* sub = current->subklass(); 422 while (sub != NULL && !sub->is_loader_alive(is_alive)) { 423 #ifndef PRODUCT 424 if (log_is_enabled(Trace, class, unload)) { 425 ResourceMark rm; 426 log_trace(class, unload)("unlinking class (subclass): %s", sub->external_name()); 427 } 428 #endif 429 sub = sub->next_sibling(); 430 } 431 current->set_subklass(sub); 432 if (sub != NULL) { 433 stack.push(sub); 434 } 435 436 // Find and set the first alive sibling 437 Klass* sibling = current->next_sibling(); 438 while (sibling != NULL && !sibling->is_loader_alive(is_alive)) { 439 if (log_is_enabled(Trace, class, unload)) { 440 ResourceMark rm; 441 log_trace(class, unload)("[Unlinking class (sibling) %s]", sibling->external_name()); 442 } 443 sibling = sibling->next_sibling(); 444 } 445 current->set_next_sibling(sibling); 446 if (sibling != NULL) { 447 stack.push(sibling); 448 } 449 450 // Clean the implementors list and method data. 451 if (clean_alive_klasses && current->is_instance_klass()) { 452 InstanceKlass* ik = InstanceKlass::cast(current); 453 ik->clean_weak_instanceklass_links(is_alive); 454 455 // JVMTI RedefineClasses creates previous versions that are not in 456 // the class hierarchy, so process them here. 457 while ((ik = ik->previous_versions()) != NULL) { 458 ik->clean_weak_instanceklass_links(is_alive); 459 } 460 } 461 } 462 } 463 464 void Klass::metaspace_pointers_do(MetaspaceClosure* it) { 465 if (log_is_enabled(Trace, cds)) { 466 ResourceMark rm; 467 log_trace(cds)("Iter(Klass): %p (%s)", this, external_name()); 468 } 469 470 it->push(&_name); 471 it->push(&_secondary_super_cache); 472 it->push(&_secondary_supers); 473 for (int i = 0; i < _primary_super_limit; i++) { 474 it->push(&_primary_supers[i]); 475 } 476 it->push(&_super); 477 it->push(&_subklass); 478 it->push(&_next_sibling); 479 it->push(&_next_link); 480 481 vtableEntry* vt = start_of_vtable(); 482 for (int i=0; i<vtable_length(); i++) { 483 it->push(vt[i].method_addr()); 484 } 485 } 486 487 void Klass::remove_unshareable_info() { 488 assert (DumpSharedSpaces, "only called for DumpSharedSpaces"); 489 TRACE_REMOVE_ID(this); 490 if (log_is_enabled(Trace, cds, unshareable)) { 491 ResourceMark rm; 492 log_trace(cds, unshareable)("remove: %s", external_name()); 493 } 494 495 set_subklass(NULL); 496 set_next_sibling(NULL); 497 set_next_link(NULL); 498 499 // Null out class_loader_data because we don't share that yet. 500 set_class_loader_data(NULL); 501 set_is_shared(); 502 } 503 504 void Klass::remove_java_mirror() { 505 assert (DumpSharedSpaces, "only called for DumpSharedSpaces"); 506 if (log_is_enabled(Trace, cds, unshareable)) { 507 ResourceMark rm; 508 log_trace(cds, unshareable)("remove java_mirror: %s", external_name()); 509 } 510 // Just null out the mirror. The class_loader_data() no longer exists. 511 _java_mirror = NULL; 512 } 513 514 void Klass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) { 515 assert(is_klass(), "ensure C++ vtable is restored"); 516 assert(is_shared(), "must be set"); 517 TRACE_RESTORE_ID(this); 518 if (log_is_enabled(Trace, cds, unshareable)) { 519 ResourceMark rm; 520 log_trace(cds, unshareable)("restore: %s", external_name()); 521 } 522 523 // If an exception happened during CDS restore, some of these fields may already be 524 // set. We leave the class on the CLD list, even if incomplete so that we don't 525 // modify the CLD list outside a safepoint. 526 if (class_loader_data() == NULL) { 527 // Restore class_loader_data to the null class loader data 528 set_class_loader_data(loader_data); 529 530 // Add to null class loader list first before creating the mirror 531 // (same order as class file parsing) 532 loader_data->add_class(this); 533 } 534 535 // Recreate the class mirror. 536 // Only recreate it if not present. A previous attempt to restore may have 537 // gotten an OOM later but keep the mirror if it was created. 538 if (java_mirror() == NULL) { 539 Handle loader(THREAD, loader_data->class_loader()); 540 ModuleEntry* module_entry = NULL; 541 Klass* k = this; 542 if (k->is_objArray_klass()) { 543 k = ObjArrayKlass::cast(k)->bottom_klass(); 544 } 545 // Obtain klass' module. 546 if (k->is_instance_klass()) { 547 InstanceKlass* ik = (InstanceKlass*) k; 548 module_entry = ik->module(); 549 } else { 550 module_entry = ModuleEntryTable::javabase_moduleEntry(); 551 } 552 // Obtain java.lang.Module, if available 553 Handle module_handle(THREAD, ((module_entry != NULL) ? module_entry->module() : (oop)NULL)); 554 java_lang_Class::create_mirror(this, loader, module_handle, protection_domain, CHECK); 555 } 556 } 557 558 Klass* Klass::array_klass_or_null(int rank) { 559 EXCEPTION_MARK; 560 // No exception can be thrown by array_klass_impl when called with or_null == true. 561 // (In anycase, the execption mark will fail if it do so) 562 return array_klass_impl(true, rank, THREAD); 563 } 564 565 566 Klass* Klass::array_klass_or_null() { 567 EXCEPTION_MARK; 568 // No exception can be thrown by array_klass_impl when called with or_null == true. 569 // (In anycase, the execption mark will fail if it do so) 570 return array_klass_impl(true, THREAD); 571 } 572 573 574 Klass* Klass::array_klass_impl(bool or_null, int rank, TRAPS) { 575 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); 576 return NULL; 577 } 578 579 580 Klass* Klass::array_klass_impl(bool or_null, TRAPS) { 581 fatal("array_klass should be dispatched to InstanceKlass, ObjArrayKlass or TypeArrayKlass"); 582 return NULL; 583 } 584 585 oop Klass::class_loader() const { return class_loader_data()->class_loader(); } 586 587 // In product mode, this function doesn't have virtual function calls so 588 // there might be some performance advantage to handling InstanceKlass here. 589 const char* Klass::external_name() const { 590 if (is_instance_klass()) { 591 const InstanceKlass* ik = static_cast<const InstanceKlass*>(this); 592 if (ik->is_anonymous()) { 593 intptr_t hash = 0; 594 if (ik->java_mirror() != NULL) { 595 // java_mirror might not be created yet, return 0 as hash. 596 hash = ik->java_mirror()->identity_hash(); 597 } 598 char hash_buf[40]; 599 sprintf(hash_buf, "/" UINTX_FORMAT, (uintx)hash); 600 size_t hash_len = strlen(hash_buf); 601 602 size_t result_len = name()->utf8_length(); 603 char* result = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1); 604 name()->as_klass_external_name(result, (int) result_len + 1); 605 assert(strlen(result) == result_len, ""); 606 strcpy(result + result_len, hash_buf); 607 assert(strlen(result) == result_len + hash_len, ""); 608 return result; 609 } 610 } 611 if (name() == NULL) return "<unknown>"; 612 return name()->as_klass_external_name(); 613 } 614 615 616 const char* Klass::signature_name() const { 617 if (name() == NULL) return "<unknown>"; 618 return name()->as_C_string(); 619 } 620 621 // Unless overridden, modifier_flags is 0. 622 jint Klass::compute_modifier_flags(TRAPS) const { 623 return 0; 624 } 625 626 int Klass::atomic_incr_biased_lock_revocation_count() { 627 return (int) Atomic::add(1, &_biased_lock_revocation_count); 628 } 629 630 // Unless overridden, jvmti_class_status has no flags set. 631 jint Klass::jvmti_class_status() const { 632 return 0; 633 } 634 635 636 // Printing 637 638 void Klass::print_on(outputStream* st) const { 639 ResourceMark rm; 640 // print title 641 st->print("%s", internal_name()); 642 print_address_on(st); 643 st->cr(); 644 } 645 646 void Klass::oop_print_on(oop obj, outputStream* st) { 647 ResourceMark rm; 648 // print title 649 st->print_cr("%s ", internal_name()); 650 obj->print_address_on(st); 651 652 if (WizardMode) { 653 // print header 654 obj->mark()->print_on(st); 655 } 656 657 // print class 658 st->print(" - klass: "); 659 obj->klass()->print_value_on(st); 660 st->cr(); 661 } 662 663 void Klass::oop_print_value_on(oop obj, outputStream* st) { 664 // print title 665 ResourceMark rm; // Cannot print in debug mode without this 666 st->print("%s", internal_name()); 667 obj->print_address_on(st); 668 } 669 670 #if INCLUDE_SERVICES 671 // Size Statistics 672 void Klass::collect_statistics(KlassSizeStats *sz) const { 673 sz->_klass_bytes = sz->count(this); 674 sz->_mirror_bytes = sz->count(java_mirror()); 675 sz->_secondary_supers_bytes = sz->count_array(secondary_supers()); 676 677 sz->_ro_bytes += sz->_secondary_supers_bytes; 678 sz->_rw_bytes += sz->_klass_bytes + sz->_mirror_bytes; 679 } 680 #endif // INCLUDE_SERVICES 681 682 // Verification 683 684 void Klass::verify_on(outputStream* st) { 685 686 // This can be expensive, but it is worth checking that this klass is actually 687 // in the CLD graph but not in production. 688 assert(Metaspace::contains((address)this), "Should be"); 689 690 guarantee(this->is_klass(),"should be klass"); 691 692 if (super() != NULL) { 693 guarantee(super()->is_klass(), "should be klass"); 694 } 695 if (secondary_super_cache() != NULL) { 696 Klass* ko = secondary_super_cache(); 697 guarantee(ko->is_klass(), "should be klass"); 698 } 699 for ( uint i = 0; i < primary_super_limit(); i++ ) { 700 Klass* ko = _primary_supers[i]; 701 if (ko != NULL) { 702 guarantee(ko->is_klass(), "should be klass"); 703 } 704 } 705 706 if (java_mirror() != NULL) { 707 guarantee(oopDesc::is_oop(java_mirror()), "should be instance"); 708 } 709 } 710 711 void Klass::oop_verify_on(oop obj, outputStream* st) { 712 guarantee(oopDesc::is_oop(obj), "should be oop"); 713 guarantee(obj->klass()->is_klass(), "klass field is not a klass"); 714 } 715 716 klassVtable Klass::vtable() const { 717 return klassVtable(const_cast<Klass*>(this), start_of_vtable(), vtable_length() / vtableEntry::size()); 718 } 719 720 vtableEntry* Klass::start_of_vtable() const { 721 return (vtableEntry*) ((address)this + in_bytes(vtable_start_offset())); 722 } 723 724 Method* Klass::method_at_vtable(int index) { 725 #ifndef PRODUCT 726 assert(index >= 0, "valid vtable index"); 727 if (DebugVtables) { 728 verify_vtable_index(index); 729 } 730 #endif 731 return start_of_vtable()[index].method(); 732 } 733 734 ByteSize Klass::vtable_start_offset() { 735 return in_ByteSize(InstanceKlass::header_size() * wordSize); 736 } 737 738 #ifndef PRODUCT 739 740 bool Klass::verify_vtable_index(int i) { 741 int limit = vtable_length()/vtableEntry::size(); 742 assert(i >= 0 && i < limit, "index %d out of bounds %d", i, limit); 743 return true; 744 } 745 746 bool Klass::verify_itable_index(int i) { 747 assert(is_instance_klass(), ""); 748 int method_count = klassItable::method_count_for_interface(this); 749 assert(i >= 0 && i < method_count, "index out of bounds"); 750 return true; 751 } 752 753 #endif