--- old/src/share/vm/memory/heapInspection.cpp 2015-02-09 16:32:22.000000000 -0800 +++ new/src/share/vm/memory/heapInspection.cpp 2015-02-09 16:32:22.000000000 -0800 @@ -39,6 +39,19 @@ // HeapInspection +inline KlassInfoEntry::~KlassInfoEntry() { + if (_subclasses != NULL) { + delete _subclasses; + } +} + +inline void KlassInfoEntry::add_subclass(KlassInfoEntry* cie) { + if (_subclasses == NULL) { + _subclasses = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(4, true); + } + _subclasses->append(cie); +} + int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { if(e1->_instance_words > e2->_instance_words) { return -1; @@ -313,11 +326,10 @@ } }; -void KlassHierarchy::print_class_hierarchy(outputStream* st) { +void KlassHierarchy::print_class_hierarchy(outputStream* st, bool print_interfaces, + bool print_subclasses, char* classname) { ResourceMark rm; - int i; Stack class_stack; - Stack super_stack; GrowableArray elements; // Add all classes to the KlassInfoTable, which allows for quick lookup. @@ -333,22 +345,33 @@ HierarchyClosure hc(&elements); cit.iterate(&hc); - // Set the index for each class - for(i=0; i < elements.length(); i++) { - elements.at(i)->set_index(i+1); - } - - // Iterate over all the classes, adding each class to the subclass array of - // its superclass. - for(i=0; i < elements.length(); i++) { - KlassInfoEntry* e = (KlassInfoEntry*)elements.at(i); - const Klass* k = e->klass(); + for(int i = 0; i < elements.length(); i++) { + KlassInfoEntry* cie = elements.at(i); + const InstanceKlass* k = (InstanceKlass*)cie->klass(); Klass* super = ((InstanceKlass*)k)->java_super(); + + // Set the index for the class. + cie->set_index(i + 1); + + // Add the class to the subclass array of its superclass. if (super != NULL) { - KlassInfoEntry* super_e = cit.lookup(super); - assert(super_e != NULL, "could not lookup superclass"); - e->set_super_index(super_e->index()); - super_e->add_subclass(e); + KlassInfoEntry* super_cie = cit.lookup(super); + assert(super_cie != NULL, "could not lookup superclass"); + super_cie->add_subclass(cie); + } + } + + // Set the do_print flag for each class that should be printed. + for(int i = 0; i < elements.length(); i++) { + KlassInfoEntry* cie = elements.at(i); + if (classname == NULL) { + // We are printing all classes. + cie->set_do_print(true); + } else { + // We are only printing the hierarchy of a specific class. + if (strcmp(classname, cie->klass()->external_name()) == 0) { + KlassHierarchy::set_do_print_for_class_hierarchy(cie, &cit, print_subclasses); + } } } @@ -362,59 +385,120 @@ // Repeatedly pop the top item off the stack, print its class info, // and push all of its subclasses on to the stack. Do this until there // are no classes left on the stack. - // - // We also keep track of the stack of superclasses so we know - // all the current superclasses for the current class. This is how - // we determine the proper indentation when printing the class. - long curr_super_index = -1; while (!class_stack.is_empty()) { KlassInfoEntry* curr_cie = class_stack.pop(); - - // Make sure super_stack is current with the class we just popped. - while (curr_cie->super_index() != curr_super_index) { - assert(!super_stack.is_empty(), "super_stack should not be empty"); - curr_super_index = super_stack.pop()->super_index(); + if (curr_cie->do_print()) { + print_class(st, curr_cie, print_interfaces); + if (curr_cie->subclasses() != NULL) { + // Current class has subclasses, so push all of them onto the stack. + for (int i = 0; i < curr_cie->subclasses()->length(); i++) { + KlassInfoEntry* cie = curr_cie->subclasses()->at(i); + if (cie->do_print()) { + class_stack.push(cie); + } + } + } } + } - print_class(st, curr_cie, &super_stack); + st->flush(); +} - if (curr_cie->subclasses() != NULL) { - // Current class has subclasses, so push all of them onto the stack +// Sets the do_print flag for every superclass and subclass of the specified class. +void KlassHierarchy::set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit, + bool print_subclasses) { + // Set do_print for all superclasses of this class. + Klass* super = ((InstanceKlass*)cie->klass())->java_super(); + while (super != NULL) { + KlassInfoEntry* super_cie = cit->lookup(super); + super_cie->set_do_print(true); + super = super->super(); + } + + // Set do_print for this class and all of its subclasses. + Stack class_stack; + class_stack.push(cie); + while (!class_stack.is_empty()) { + KlassInfoEntry* curr_cie = class_stack.pop(); + curr_cie->set_do_print(true); + if (print_subclasses && curr_cie->subclasses() != NULL) { + // Current class has subclasses, so push all of them onto the stack. for (int i = 0; i < curr_cie->subclasses()->length(); i++) { - class_stack.push(curr_cie->subclasses()->at(i)); + KlassInfoEntry* cie = curr_cie->subclasses()->at(i); + class_stack.push(cie); } - // Add current class to superclass stack. - super_stack.push(curr_cie); - curr_super_index = curr_cie->index(); } } - - st->flush(); } -void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, - Stack *super_stack) { - ResourceMark rm; - - // print indentation with proper indicators of superclass. - StackIterator iter(*super_stack); - while (!iter.is_empty()) { - KlassInfoEntry* super_cie = iter.next(); +static void print_indent(outputStream* st, int indent) { + while (indent != 0) { st->print("|"); - if (iter.is_empty()) { - st->print("--"); - } else { + indent--; + if (indent != 0) { st->print(" "); } } +} - // print the class name - st->print("%s\n", cie->name()); +// Print the class name and its unique ClassLoader identifer. +static void print_classname(outputStream* st, Klass* klass) { + oop loader_oop = klass->class_loader_data()->class_loader(); + st->print("%s/", klass->external_name()); + if (loader_oop == NULL) { + st->print("null"); + } else { + st->print(INTPTR_FORMAT, loader_oop->klass()); + } +} + +static void print_interface(outputStream* st, Klass* intf_klass, const char* intf_type, int indent) { + print_indent(st, indent); + st->print(" implements "); + print_classname(st, intf_klass); + st->print(" (%s intf)\n", intf_type); +} + +void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, bool print_interfaces) { + ResourceMark rm; + InstanceKlass* klass = (InstanceKlass*)cie->klass(); + int indent = 0; + + // Print indentation with proper indicators of superclass. + Klass* super = klass->super(); + while (super != NULL) { + super = super->super(); + indent++; + } + print_indent(st, indent); + if (indent != 0) st->print("--"); + + // Print the class name, its unique ClassLoader identifer, and if it is an interface. + print_classname(st, klass); + if (klass->is_interface()) { + st->print(" (intf)"); + } + st->print("\n"); + + // Print any interfaces the class has. + if (print_interfaces) { + Array* local_intfs = klass->local_interfaces(); + Array* trans_intfs = klass->transitive_interfaces(); + for (int i = 0; i < local_intfs->length(); i++) { + print_interface(st, local_intfs->at(i), "declared", indent); + } + for (int i = 0; i < trans_intfs->length(); i++) { + Klass* trans_interface = trans_intfs->at(i); + // Only print transitive interfaces if they are not also declared. + if (!local_intfs->contains(trans_interface)) { + print_interface(st, trans_interface, "transitive", indent); + } + } + } } void KlassInfoHisto::print_class_stats(outputStream* st, bool csv_format, const char *columns) { - ResourceMark rm; KlassSizeStats sz, sz_sum; int i; julong *col_table = (julong*)(&sz); @@ -443,7 +527,7 @@ KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i); const Klass* k = e->klass(); - // Get the stats for this class + // Get the stats for this class. memset(&sz, 0, sizeof(sz)); sz._inst_count = e->count(); sz._inst_bytes = HeapWordSize * e->words(); @@ -451,30 +535,31 @@ sz._total_bytes = sz._ro_bytes + sz._rw_bytes; if (pass == 1) { - // Add the stats for this class to the overall totals + // Add the stats for this class to the overall totals. for (int c=0; coop_is_instance()) { Klass* super = ((InstanceKlass*)k)->java_super(); if (super) { KlassInfoEntry* super_e = _cit->lookup(super); if (super_e) { - e->set_super_index(super_e->index()); + super_index = super_e->index(); } } } if (csv_format) { - st->print("%d,%d", e->index(), e->super_index()); + st->print("%d,%d", e->index(), super_index); for (int c=0; cprint("," JULONG_FORMAT, col_table[c]);} } st->print(",%s",e->name()); } else { - st->print("%5d %5d", e->index(), e->super_index()); + st->print("%5d %5d", e->index(), super_index); for (int c=0; c* _subclasses; public: KlassInfoEntry(Klass* k, KlassInfoEntry* next) : _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1), - _super_index(-1), _subclasses(NULL) + _do_print(false), _subclasses(NULL) {} ~KlassInfoEntry(); KlassInfoEntry* next() const { return _next; } @@ -206,28 +206,15 @@ void set_words(size_t wds) { _instance_words = wds; } void set_index(long index) { _index = index; } long index() const { return _index; } - void set_super_index(long index) { _super_index = index; } - long super_index() const { return _super_index; } GrowableArray* subclasses() const { return _subclasses; } void add_subclass(KlassInfoEntry* cie); + void set_do_print(bool do_print) { _do_print = do_print; } + bool do_print() const { return _do_print; } int compare(KlassInfoEntry* e1, KlassInfoEntry* e2); void print_on(outputStream* st) const; const char* name() const; }; -inline void KlassInfoEntry::add_subclass(KlassInfoEntry* cie) { - if (_subclasses == NULL) { - _subclasses = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(4, true); - } - _subclasses->append(cie); -} - -inline KlassInfoEntry::~KlassInfoEntry() { - if (_subclasses != NULL) { - delete _subclasses; - } -} - class KlassInfoClosure : public StackObj { public: // Called for each KlassInfoEntry. @@ -284,11 +271,13 @@ public: KlassHierarchy(KlassInfoTable* cit, const char* title); ~KlassHierarchy(); - static void print_class_hierarchy(outputStream* st); + static void print_class_hierarchy(outputStream* st, bool print_interfaces, bool print_subclasses, + char* classname); -private: - static void print_class(outputStream* st, KlassInfoEntry* cie, - Stack *super_stack); + private: + static void set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit, + bool print_subclasse); + static void print_class(outputStream* st, KlassInfoEntry* cie, bool print_subclasses); }; class KlassInfoHisto : public StackObj { --- old/src/share/vm/runtime/vm_operations.cpp 2015-02-09 16:32:23.000000000 -0800 +++ new/src/share/vm/runtime/vm_operations.cpp 2015-02-09 16:32:23.000000000 -0800 @@ -490,6 +490,6 @@ #if INCLUDE_SERVICES void VM_PrintClassHierachry::doit() { - KlassHierarchy::print_class_hierarchy(_out); + KlassHierarchy::print_class_hierarchy(_out, _print_interfaces, _print_subclasses, _classname); } #endif --- old/src/share/vm/runtime/vm_operations.hpp 2015-02-09 16:32:23.000000000 -0800 +++ new/src/share/vm/runtime/vm_operations.hpp 2015-02-09 16:32:23.000000000 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -461,9 +461,14 @@ class VM_PrintClassHierachry: public VM_Operation { private: outputStream* _out; + bool _print_interfaces; + bool _print_subclasses; + char* _classname; public: - VM_PrintClassHierachry(outputStream* st) : _out(st) {} + VM_PrintClassHierachry(outputStream* st, bool print_interfaces, bool print_subclasses, char* classname) : + _out(st), _print_interfaces(print_interfaces), _print_subclasses(print_subclasses), + _classname(classname) {} VMOp_Type type() const { return VMOp_PrintClassHierachry; } void doit(); }; --- old/src/share/vm/services/diagnosticCommand.cpp 2015-02-09 16:32:24.000000000 -0800 +++ new/src/share/vm/services/diagnosticCommand.cpp 2015-02-09 16:32:24.000000000 -0800 @@ -57,6 +57,7 @@ DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(DCmd_Source_Internal | DCmd_Source_AttachAPI, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); #endif // INCLUDE_SERVICES @@ -66,9 +67,6 @@ DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); -#if INCLUDE_SERVICES - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); -#endif // Enhanced JMX Agent Support // These commands won't be exported via the DiagnosticCommandMBean until an @@ -699,8 +697,34 @@ } #if INCLUDE_SERVICES +ClassHierarchyDCmd::ClassHierarchyDCmd(outputStream* output, bool heap) : + DCmdWithParser(output, heap), + _print_interfaces("-i", "Inherited interfaces should be printed.", "BOOLEAN", false, "false"), + _print_subclasses("-s", "If a classname is specified, print its subclasses. " + "Otherwise only its superclasses are printed.", "BOOLEAN", false, "false"), + _classname("classname", "Name of class whose hierarchy should be printed. " + "If not specified, all class hierarchies are printed.", + "STRING", false) { + _dcmdparser.add_dcmd_option(&_print_interfaces); + _dcmdparser.add_dcmd_option(&_print_subclasses); + _dcmdparser.add_dcmd_argument(&_classname); +} + void ClassHierarchyDCmd::execute(DCmdSource source, TRAPS) { - VM_PrintClassHierachry printClassHierarchyOp(output()); + VM_PrintClassHierachry printClassHierarchyOp(output(), _print_interfaces.value(), + _print_subclasses.value(), _classname.value()); VMThread::execute(&printClassHierarchyOp); } + +int ClassHierarchyDCmd::num_arguments() { + ResourceMark rm; + ClassHierarchyDCmd* dcmd = new ClassHierarchyDCmd(NULL, false); + if (dcmd != NULL) { + DCmdMark mark(dcmd); + return dcmd->_dcmdparser.num_arguments(); + } else { + return 0; + } +} + #endif --- old/src/share/vm/services/diagnosticCommand.hpp 2015-02-09 16:32:24.000000000 -0800 +++ new/src/share/vm/services/diagnosticCommand.hpp 2015-02-09 16:32:24.000000000 -0800 @@ -272,18 +272,25 @@ }; -class ClassHierarchyDCmd : public DCmd { +class ClassHierarchyDCmd : public DCmdWithParser { +protected: + DCmdArgument _print_interfaces; // true if inherited interfaces should be printed. + DCmdArgument _print_subclasses; // true subclasses of specified classname should be printed. + DCmdArgument _classname; // Optional single class name whose hierarchy should be printed. public: - ClassHierarchyDCmd(outputStream* output, bool heap) : DCmd(output, heap) { } + ClassHierarchyDCmd(outputStream* output, bool heap); static const char* name() { return "VM.class_hierarchy"; } static const char* description() { - return "Print a list of all loaded classes, indented to show the class hiearchy."; + return "Print a list of all loaded classes, indented to show the class hiearchy. " + "The name of each class is followed by the Klass* of its ClassLoader, " + "or \"null\" if loaded by the bootstrap class loader."; } static const char* impact() { return "Medium: Depends on number of loaded classes."; } + static int num_arguments(); virtual void execute(DCmdSource source, TRAPS); };