src/share/vm/prims/jvmtiRedefineClasses.cpp

Print this page




 137   MetadataOnStackMark md_on_stack;
 138   HandleMark hm(thread);   // make sure any handles created are deleted
 139                            // before the stack walk again.
 140 
 141   for (int i = 0; i < _class_count; i++) {
 142     redefine_single_class(_class_defs[i].klass, _scratch_classes[i], thread);
 143     ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
 144     // Free the memory for this class at class unloading time.  Not before
 145     // because CMS might think this is still live.
 146     cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
 147     _scratch_classes[i] = NULL;
 148   }
 149 
 150   // Disable any dependent concurrent compilations
 151   SystemDictionary::notice_modification();
 152 
 153   // Set flag indicating that some invariants are no longer true.
 154   // See jvmtiExport.hpp for detailed explanation.
 155   JvmtiExport::set_has_redefined_a_class();
 156 
 157 #ifdef ASSERT





 158   SystemDictionary::classes_do(check_class, thread);


 159 #endif
 160 }
 161 
 162 void VM_RedefineClasses::doit_epilogue() {
 163   // Free os::malloc allocated memory.
 164   os::free(_scratch_classes);
 165 
 166   if (RC_TRACE_ENABLED(0x00000004)) {
 167     // Used to have separate timers for "doit" and "all", but the timer
 168     // overhead skewed the measurements.
 169     jlong doit_time = _timer_rsc_phase1.milliseconds() +
 170                       _timer_rsc_phase2.milliseconds();
 171     jlong all_time = _timer_vm_op_prologue.milliseconds() + doit_time;
 172 
 173     RC_TRACE(0x00000004, ("vm_op: all=" UINT64_FORMAT
 174       "  prologue=" UINT64_FORMAT "  doit=" UINT64_FORMAT, all_time,
 175       _timer_vm_op_prologue.milliseconds(), doit_time));
 176     RC_TRACE(0x00000004,
 177       ("redefine_single_class: phase1=" UINT64_FORMAT "  phase2=" UINT64_FORMAT,
 178        _timer_rsc_phase1.milliseconds(), _timer_rsc_phase2.milliseconds()));


1568       case Bytecodes::_invokespecial  : // fall through
1569       case Bytecodes::_invokestatic   : // fall through
1570       case Bytecodes::_invokevirtual  : // fall through
1571       case Bytecodes::_ldc_w          : // fall through
1572       case Bytecodes::_ldc2_w         : // fall through
1573       case Bytecodes::_multianewarray : // fall through
1574       case Bytecodes::_new            : // fall through
1575       case Bytecodes::_putfield       : // fall through
1576       case Bytecodes::_putstatic      :
1577       {
1578         address p = bcp + 1;
1579         int cp_index = Bytes::get_Java_u2(p);
1580         int new_index = find_new_index(cp_index);
1581         if (new_index != 0) {
1582           // the original index is mapped so update w/ new value
1583           RC_TRACE_WITH_THREAD(0x00080000, THREAD,
1584             ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c),
1585             bcp, cp_index, new_index));
1586           // Rewriter::rewrite_method() uses put_native_u2() in this
1587           // situation because it is reusing the constant pool index
1588           // location for a native index into the constantPoolCache.
1589           // Since we are updating the constant pool index prior to
1590           // verification and constantPoolCache initialization, we
1591           // need to keep the new index in Java byte order.
1592           Bytes::put_Java_u2(p, new_index);
1593         }
1594       } break;
1595     }
1596   } // end for each bytecode
1597 } // end rewrite_cp_refs_in_method()
1598 
1599 
1600 // Rewrite constant pool references in the class_annotations field.
1601 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations(
1602        instanceKlassHandle scratch_class, TRAPS) {
1603 
1604   AnnotationArray* class_annotations = scratch_class->class_annotations();
1605   if (class_annotations == NULL || class_annotations->length() == 0) {
1606     // no class_annotations so nothing to do
1607     return true;
1608   }
1609 
1610   RC_TRACE_WITH_THREAD(0x02000000, THREAD,


3375   int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1;
3376   java_lang_Class::set_classRedefinedCount(class_mirror, new_count);
3377 
3378   if (class_oop != _the_class_oop) {
3379     // _the_class_oop count is printed at end of redefine_single_class()
3380     RC_TRACE_WITH_THREAD(0x00000008, THREAD,
3381       ("updated count in subclass=%s to %d", ik->external_name(), new_count));
3382   }
3383 
3384   for (Klass *subk = ik->subklass(); subk != NULL;
3385        subk = subk->next_sibling()) {
3386     if (subk->oop_is_instance()) {
3387       // Only update instanceKlasses
3388       InstanceKlass *subik = (InstanceKlass*)subk;
3389       // recursively do subclasses of the current subclass
3390       increment_class_counter(subik, THREAD);
3391     }
3392   }
3393 }
3394 
3395 #ifndef PRODUCT
3396 void VM_RedefineClasses::check_class(Klass* k_oop,
3397                                      ClassLoaderData* initiating_loader,
3398                                      TRAPS) {
3399   Klass *k = k_oop;
3400   if (k->oop_is_instance()) {
3401     HandleMark hm(THREAD);
3402     InstanceKlass *ik = (InstanceKlass *) k;
3403 
3404     if (ik->vtable_length() > 0) {
3405       ResourceMark rm(THREAD);
3406       if (!ik->vtable()->check_no_old_entries()) {
3407         tty->print_cr("klassVtable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());







3408         ik->vtable()->dump_vtable();
3409         assert(false, "OLD method found");
3410       }

3411     }
3412     if (ik->itable_length() > 0) {
3413       ResourceMark rm(THREAD);
3414       if (!ik->itable()->check_no_old_entries()) {
3415         tty->print_cr("klassItable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
3416         assert(false, "OLD method found");





3417       }

3418     }
3419     // Check that the constant pool cache has no deleted entries.

3420     if (ik->constants() != NULL &&
3421         ik->constants()->cache() != NULL &&
3422        !ik->constants()->cache()->check_no_old_entries()) {
3423       tty->print_cr("klassVtable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
3424       assert(false, "OLD method found");




3425     }

3426   }











3427 }
3428 
3429 void VM_RedefineClasses::dump_methods() {
3430         int j;
3431         tty->print_cr("_old_methods --");
3432         for (j = 0; j < _old_methods->length(); ++j) {
3433           Method* m = _old_methods->at(j);
3434           tty->print("%4d  (%5d)  ", j, m->vtable_index());
3435           m->access_flags().print_on(tty);
3436           tty->print(" --  ");
3437           m->print_name(tty);
3438           tty->cr();
3439         }
3440         tty->print_cr("_new_methods --");
3441         for (j = 0; j < _new_methods->length(); ++j) {
3442           Method* m = _new_methods->at(j);
3443           tty->print("%4d  (%5d)  ", j, m->vtable_index());
3444           m->access_flags().print_on(tty);
3445           tty->print(" --  ");
3446           m->print_name(tty);
3447           tty->cr();
3448         }
3449         tty->print_cr("_matching_(old/new)_methods --");
3450         for (j = 0; j < _matching_methods_length; ++j) {
3451           Method* m = _matching_old_methods[j];
3452           tty->print("%4d  (%5d)  ", j, m->vtable_index());
3453           m->access_flags().print_on(tty);
3454           tty->print(" --  ");
3455           m->print_name(tty);
3456           tty->cr();
3457           m = _matching_new_methods[j];
3458           tty->print("      (%5d)  ", m->vtable_index());
3459           m->access_flags().print_on(tty);
3460           tty->cr();
3461         }
3462         tty->print_cr("_deleted_methods --");
3463         for (j = 0; j < _deleted_methods_length; ++j) {
3464           Method* m = _deleted_methods[j];
3465           tty->print("%4d  (%5d)  ", j, m->vtable_index());
3466           m->access_flags().print_on(tty);
3467           tty->print(" --  ");
3468           m->print_name(tty);
3469           tty->cr();
3470         }
3471         tty->print_cr("_added_methods --");
3472         for (j = 0; j < _added_methods_length; ++j) {
3473           Method* m = _added_methods[j];
3474           tty->print("%4d  (%5d)  ", j, m->vtable_index());
3475           m->access_flags().print_on(tty);
3476           tty->print(" --  ");
3477           m->print_name(tty);
3478           tty->cr();
3479         }
3480 }
3481 #endif


 137   MetadataOnStackMark md_on_stack;
 138   HandleMark hm(thread);   // make sure any handles created are deleted
 139                            // before the stack walk again.
 140 
 141   for (int i = 0; i < _class_count; i++) {
 142     redefine_single_class(_class_defs[i].klass, _scratch_classes[i], thread);
 143     ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
 144     // Free the memory for this class at class unloading time.  Not before
 145     // because CMS might think this is still live.
 146     cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
 147     _scratch_classes[i] = NULL;
 148   }
 149 
 150   // Disable any dependent concurrent compilations
 151   SystemDictionary::notice_modification();
 152 
 153   // Set flag indicating that some invariants are no longer true.
 154   // See jvmtiExport.hpp for detailed explanation.
 155   JvmtiExport::set_has_redefined_a_class();
 156 
 157 // check_class() is optionally called for product bits, but is
 158 // always called for non-product bits.
 159 #ifdef PRODUCT
 160   if (RC_TRACE_ENABLED(0x00004000)) {
 161 #endif
 162     RC_TRACE_WITH_THREAD(0x00004000, thread, ("calling check_class"));
 163     SystemDictionary::classes_do(check_class, thread);
 164 #ifdef PRODUCT
 165   }
 166 #endif
 167 }
 168 
 169 void VM_RedefineClasses::doit_epilogue() {
 170   // Free os::malloc allocated memory.
 171   os::free(_scratch_classes);
 172 
 173   if (RC_TRACE_ENABLED(0x00000004)) {
 174     // Used to have separate timers for "doit" and "all", but the timer
 175     // overhead skewed the measurements.
 176     jlong doit_time = _timer_rsc_phase1.milliseconds() +
 177                       _timer_rsc_phase2.milliseconds();
 178     jlong all_time = _timer_vm_op_prologue.milliseconds() + doit_time;
 179 
 180     RC_TRACE(0x00000004, ("vm_op: all=" UINT64_FORMAT
 181       "  prologue=" UINT64_FORMAT "  doit=" UINT64_FORMAT, all_time,
 182       _timer_vm_op_prologue.milliseconds(), doit_time));
 183     RC_TRACE(0x00000004,
 184       ("redefine_single_class: phase1=" UINT64_FORMAT "  phase2=" UINT64_FORMAT,
 185        _timer_rsc_phase1.milliseconds(), _timer_rsc_phase2.milliseconds()));


1575       case Bytecodes::_invokespecial  : // fall through
1576       case Bytecodes::_invokestatic   : // fall through
1577       case Bytecodes::_invokevirtual  : // fall through
1578       case Bytecodes::_ldc_w          : // fall through
1579       case Bytecodes::_ldc2_w         : // fall through
1580       case Bytecodes::_multianewarray : // fall through
1581       case Bytecodes::_new            : // fall through
1582       case Bytecodes::_putfield       : // fall through
1583       case Bytecodes::_putstatic      :
1584       {
1585         address p = bcp + 1;
1586         int cp_index = Bytes::get_Java_u2(p);
1587         int new_index = find_new_index(cp_index);
1588         if (new_index != 0) {
1589           // the original index is mapped so update w/ new value
1590           RC_TRACE_WITH_THREAD(0x00080000, THREAD,
1591             ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c),
1592             bcp, cp_index, new_index));
1593           // Rewriter::rewrite_method() uses put_native_u2() in this
1594           // situation because it is reusing the constant pool index
1595           // location for a native index into the ConstantPoolCache.
1596           // Since we are updating the constant pool index prior to
1597           // verification and ConstantPoolCache initialization, we
1598           // need to keep the new index in Java byte order.
1599           Bytes::put_Java_u2(p, new_index);
1600         }
1601       } break;
1602     }
1603   } // end for each bytecode
1604 } // end rewrite_cp_refs_in_method()
1605 
1606 
1607 // Rewrite constant pool references in the class_annotations field.
1608 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations(
1609        instanceKlassHandle scratch_class, TRAPS) {
1610 
1611   AnnotationArray* class_annotations = scratch_class->class_annotations();
1612   if (class_annotations == NULL || class_annotations->length() == 0) {
1613     // no class_annotations so nothing to do
1614     return true;
1615   }
1616 
1617   RC_TRACE_WITH_THREAD(0x02000000, THREAD,


3382   int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1;
3383   java_lang_Class::set_classRedefinedCount(class_mirror, new_count);
3384 
3385   if (class_oop != _the_class_oop) {
3386     // _the_class_oop count is printed at end of redefine_single_class()
3387     RC_TRACE_WITH_THREAD(0x00000008, THREAD,
3388       ("updated count in subclass=%s to %d", ik->external_name(), new_count));
3389   }
3390 
3391   for (Klass *subk = ik->subklass(); subk != NULL;
3392        subk = subk->next_sibling()) {
3393     if (subk->oop_is_instance()) {
3394       // Only update instanceKlasses
3395       InstanceKlass *subik = (InstanceKlass*)subk;
3396       // recursively do subclasses of the current subclass
3397       increment_class_counter(subik, THREAD);
3398     }
3399   }
3400 }
3401 

3402 void VM_RedefineClasses::check_class(Klass* k_oop,
3403                                      ClassLoaderData* initiating_loader,
3404                                      TRAPS) {
3405   Klass *k = k_oop;
3406   if (k->oop_is_instance()) {
3407     HandleMark hm(THREAD);
3408     InstanceKlass *ik = (InstanceKlass *) k;
3409     bool no_old_methods = true;  // be optimistic

3410     ResourceMark rm(THREAD);
3411 
3412     // a vtable should never contain old or obsolete methods
3413     if (ik->vtable_length() > 0 &&
3414         !ik->vtable()->check_no_old_or_obsolete_entries()) {
3415       if (RC_TRACE_ENABLED(0x00004000)) {
3416         RC_TRACE_WITH_THREAD(0x00004000, THREAD,
3417           ("klassVtable::check_no_old_or_obsolete_entries failure"
3418            " -- OLD or OBSOLETE method found -- class: %s",
3419            ik->signature_name()));
3420         ik->vtable()->dump_vtable();

3421       }
3422       no_old_methods = false;
3423     }
3424 
3425     // an itable should never contain old or obsolete methods
3426     if (ik->itable_length() > 0 &&
3427         !ik->itable()->check_no_old_or_obsolete_entries()) {
3428       if (RC_TRACE_ENABLED(0x00004000)) {
3429         RC_TRACE_WITH_THREAD(0x00004000, THREAD,
3430           ("klassItable::check_no_old_or_obsolete_entries failure"
3431            " -- OLD or OBSOLETE method found -- class: %s",
3432            ik->signature_name()));
3433         ik->itable()->dump_itable();
3434       }
3435       no_old_methods = false;
3436     }
3437 
3438     // the constant pool cache should never contain old or obsolete methods
3439     if (ik->constants() != NULL &&
3440         ik->constants()->cache() != NULL &&
3441         !ik->constants()->cache()->check_no_old_or_obsolete_entries()) {
3442       if (RC_TRACE_ENABLED(0x00004000)) {
3443         RC_TRACE_WITH_THREAD(0x00004000, THREAD,
3444           ("cp-cache::check_no_old_or_obsolete_entries failure"
3445            " -- OLD or OBSOLETE method found -- class: %s",
3446            ik->signature_name()));
3447         ik->constants()->cache()->dump_cache();
3448       }
3449       no_old_methods = false;
3450     }
3451 
3452     if (!no_old_methods) {
3453       if (RC_TRACE_ENABLED(0x00004000)) {
3454         dump_methods();
3455       } else {
3456         tty->print_cr("INFO: use the '-XX:TraceRedefineClasses=16384' option "
3457           "to see more info about the following guarantee() failure.");
3458       }
3459       guarantee(false, "OLD and/or OBSOLETE method(s) found");
3460     }
3461   }
3462 }
3463 
3464 void VM_RedefineClasses::dump_methods() {
3465   int j;
3466   RC_TRACE(0x00004000, ("_old_methods --"));
3467   for (j = 0; j < _old_methods->length(); ++j) {
3468     Method* m = _old_methods->at(j);
3469     RC_TRACE_NO_CR(0x00004000, ("%4d  (%5d)  ", j, m->vtable_index()));
3470     m->access_flags().print_on(tty);
3471     tty->print(" --  ");
3472     m->print_name(tty);
3473     tty->cr();
3474   }
3475   RC_TRACE(0x00004000, ("_new_methods --"));
3476   for (j = 0; j < _new_methods->length(); ++j) {
3477     Method* m = _new_methods->at(j);
3478     RC_TRACE_NO_CR(0x00004000, ("%4d  (%5d)  ", j, m->vtable_index()));
3479     m->access_flags().print_on(tty);
3480     tty->print(" --  ");
3481     m->print_name(tty);
3482     tty->cr();
3483   }
3484   RC_TRACE(0x00004000, ("_matching_(old/new)_methods --"));
3485   for (j = 0; j < _matching_methods_length; ++j) {
3486     Method* m = _matching_old_methods[j];
3487     RC_TRACE_NO_CR(0x00004000, ("%4d  (%5d)  ", j, m->vtable_index()));
3488     m->access_flags().print_on(tty);
3489     tty->print(" --  ");
3490     m->print_name(tty);
3491     tty->cr();
3492     m = _matching_new_methods[j];
3493     RC_TRACE_NO_CR(0x00004000, ("      (%5d)  ", m->vtable_index()));
3494     m->access_flags().print_on(tty);
3495     tty->cr();
3496   }
3497   RC_TRACE(0x00004000, ("_deleted_methods --"));
3498   for (j = 0; j < _deleted_methods_length; ++j) {
3499     Method* m = _deleted_methods[j];
3500     RC_TRACE_NO_CR(0x00004000, ("%4d  (%5d)  ", j, m->vtable_index()));
3501     m->access_flags().print_on(tty);
3502     tty->print(" --  ");
3503     m->print_name(tty);
3504     tty->cr();
3505   }
3506   RC_TRACE(0x00004000, ("_added_methods --"));
3507   for (j = 0; j < _added_methods_length; ++j) {
3508     Method* m = _added_methods[j];
3509     RC_TRACE_NO_CR(0x00004000, ("%4d  (%5d)  ", j, m->vtable_index()));
3510     m->access_flags().print_on(tty);
3511     tty->print(" --  ");
3512     m->print_name(tty);
3513     tty->cr();
3514   }
3515 }