88 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
89
90 void ClassLoaderData::init_null_class_loader_data() {
91 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
92 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
93
94 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
95 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
96 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
97
98 LogTarget(Debug, class, loader, data) lt;
99 if (lt.is_enabled()) {
100 ResourceMark rm;
101 LogStream ls(lt);
102 ls.print("create ");
103 _the_null_class_loader_data->print_value_on(&ls);
104 ls.cr();
105 }
106 }
107
108 // JFR and logging support so that the name and klass are available after the
109 // class_loader oop is no longer alive, during unloading.
110 void ClassLoaderData::initialize_name_and_klass(Handle class_loader) {
111 _class_loader_klass = class_loader->klass();
112 oop class_loader_name = java_lang_ClassLoader::name(class_loader());
113 if (class_loader_name != NULL) {
114 Thread* THREAD = Thread::current();
115 ResourceMark rm(THREAD);
116 const char* class_loader_instance_name =
117 java_lang_String::as_utf8_string(class_loader_name);
118
119 if (class_loader_instance_name != NULL && class_loader_instance_name[0] != '\0') {
120 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
121 _class_loader_name = SymbolTable::new_symbol(class_loader_instance_name, CATCH);
122 }
123 }
124 }
125
126 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
127 _is_anonymous(is_anonymous),
128 // An anonymous class loader data doesn't have anything to keep
129 // it from being unloaded during parsing of the anonymous class.
130 // The null-class-loader should always be kept alive.
131 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
132 _metaspace(NULL), _unloading(false), _klasses(NULL),
133 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
134 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
135 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
136 _next(NULL),
137 _class_loader_klass(NULL), _class_loader_name(NULL),
138 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
139 Monitor::_safepoint_check_never)) {
140
141 if (!h_class_loader.is_null()) {
142 _class_loader = _handles.add(h_class_loader());
143 }
144
145 if (!is_anonymous) {
146 // The holder is initialized later for anonymous classes, and before calling anything
147 // that call class_loader().
148 initialize_holder(h_class_loader);
149
150 // A ClassLoaderData created solely for an anonymous class should never have a
151 // ModuleEntryTable or PackageEntryTable created for it. The defining package
152 // and module for an anonymous class will be found in its host class.
153 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
154 if (h_class_loader.is_null()) {
155 // Create unnamed module for boot loader
156 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
157 } else {
894 _deallocate_list->remove_at(i);
895 if (m->is_constantPool()) {
896 ((ConstantPool*)m)->release_C_heap_structures();
897 } else if (m->is_klass()) {
898 InstanceKlass* ik = (InstanceKlass*)m;
899 // also releases ik->constants() C heap memory
900 InstanceKlass::release_C_heap_structures(ik);
901 // Remove the class so unloading events aren't triggered for
902 // this class (scratch or error class) in do_unloading().
903 remove_class(ik);
904 }
905 }
906 }
907
908 // These anonymous class loaders are to contain classes used for JSR292
909 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
910 // Add a new class loader data to the graph.
911 return ClassLoaderDataGraph::add(loader, true);
912 }
913
914 const char* ClassLoaderData::loader_name() const {
915 if (is_unloading()) {
916 if (_class_loader_klass == NULL) {
917 return "<bootloader>";
918 } else if (_class_loader_name != NULL) {
919 return _class_loader_name->as_C_string();
920 } else {
921 return _class_loader_klass->name()->as_C_string();
922 }
923 } else {
924 // Handles null class loader
925 return SystemDictionary::loader_name(class_loader());
926 }
927 }
928
929
930 void ClassLoaderData::print_value_on(outputStream* out) const {
931 if (!is_unloading() && class_loader() != NULL) {
932 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
933 class_loader()->print_value_on(out); // includes loader_name() and address of class loader instance
934 } else {
935 // loader data: 0xsomeaddr of <bootloader>
936 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name());
937 }
938 if (is_anonymous()) {
939 out->print(" anonymous");
940 }
941 }
942
943 #ifndef PRODUCT
944 void ClassLoaderData::print_on(outputStream* out) const {
945 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
946 p2i(this), p2i(_class_loader.ptr_raw()), loader_name());
947 if (is_anonymous()) out->print(" anonymous");
948 if (claimed()) out->print(" claimed");
949 if (is_unloading()) out->print(" unloading");
950 out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
951
952 if (_jmethod_ids != NULL) {
953 Method::print_jmethod_ids(this, out);
954 }
955 out->print(" handles count %d", _handles.count());
956 out->print(" dependencies %d", _dependency_count);
957 out->print_cr("}");
958 }
959 #endif // PRODUCT
960
961 void ClassLoaderData::verify() {
962 assert_locked_or_safepoint(_metaspace_lock);
963 oop cl = class_loader();
964
965 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
966 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
1220 void ClassLoaderDataGraph::verify_dictionary() {
1221 FOR_ALL_DICTIONARY(cld) {
1222 cld->dictionary()->verify();
1223 }
1224 }
1225
1226 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
1227 FOR_ALL_DICTIONARY(cld) {
1228 st->print("Dictionary for ");
1229 cld->print_value_on(st);
1230 st->cr();
1231 cld->dictionary()->print_on(st);
1232 st->cr();
1233 }
1234 }
1235
1236 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) {
1237 FOR_ALL_DICTIONARY(cld) {
1238 ResourceMark rm;
1239 stringStream tempst;
1240 tempst.print("System Dictionary for %s", cld->loader_name());
1241 cld->dictionary()->print_table_statistics(st, tempst.as_string());
1242 }
1243 }
1244
1245 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
1246 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
1247
1248 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
1249
1250 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
1251 ClassLoaderData* curr = _head;
1252 while (curr != _saved_head) {
1253 if (!curr->claimed()) {
1254 array->push(curr);
1255 LogTarget(Debug, class, loader, data) lt;
1256 if (lt.is_enabled()) {
1257 LogStream ls(lt);
1258 ls.print("found new CLD: ");
1259 curr->print_value_on(&ls);
1260 ls.cr();
|
88 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
89
90 void ClassLoaderData::init_null_class_loader_data() {
91 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
92 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
93
94 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
95 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
96 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
97
98 LogTarget(Debug, class, loader, data) lt;
99 if (lt.is_enabled()) {
100 ResourceMark rm;
101 LogStream ls(lt);
102 ls.print("create ");
103 _the_null_class_loader_data->print_value_on(&ls);
104 ls.cr();
105 }
106 }
107
108 // Obtain and set the class loader's name within the ClassLoaderData so
109 // it will be available for error messages, logging, JFR, etc. The name
110 // and klass are available after the class_loader oop is no longer alive,
111 // during unloading.
112 void ClassLoaderData::initialize_name_and_klass(Handle class_loader) {
113 Thread* THREAD = Thread::current();
114 ResourceMark rm(THREAD);
115 _class_loader_klass = class_loader->klass();
116
117 // Obtain the class loader's name. If the class loader's name was not
118 // explicitly set during construction, the CLD's _name field will be null.
119 oop cl_name = java_lang_ClassLoader::name(class_loader());
120 if (cl_name != NULL) {
121 const char* cl_instance_name = java_lang_String::as_utf8_string(cl_name);
122
123 if (cl_instance_name != NULL && cl_instance_name[0] != '\0') {
124 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
125 _name = SymbolTable::new_symbol(cl_instance_name, CATCH);
126 }
127 }
128
129 // Obtain the class loader's name and identity hash. If the class loader's
130 // name was not explicitly set during construction, the class loader's name and id
131 // will be set to the qualified class name of the class loader along with its
132 // identity hash.
133 // If for some reason the ClassLoader's constructor has not been run, instead of
134 // leaving the _name_and_id field null, fall back to the external qualified class
135 // name. Thus CLD's _name_and_id field should never have a null value.
136 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader());
137 const char* cl_instance_name_and_id =
138 (cl_name_and_id == NULL) ? _class_loader_klass->external_name() :
139 java_lang_String::as_utf8_string(cl_name_and_id);
140 assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
141 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
142 _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
143 }
144
145 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
146 _is_anonymous(is_anonymous),
147 // An anonymous class loader data doesn't have anything to keep
148 // it from being unloaded during parsing of the anonymous class.
149 // The null-class-loader should always be kept alive.
150 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
151 _metaspace(NULL), _unloading(false), _klasses(NULL),
152 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
153 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
154 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
155 _next(NULL),
156 _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL),
157 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
158 Monitor::_safepoint_check_never)) {
159
160 if (!h_class_loader.is_null()) {
161 _class_loader = _handles.add(h_class_loader());
162 }
163
164 if (!is_anonymous) {
165 // The holder is initialized later for anonymous classes, and before calling anything
166 // that call class_loader().
167 initialize_holder(h_class_loader);
168
169 // A ClassLoaderData created solely for an anonymous class should never have a
170 // ModuleEntryTable or PackageEntryTable created for it. The defining package
171 // and module for an anonymous class will be found in its host class.
172 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
173 if (h_class_loader.is_null()) {
174 // Create unnamed module for boot loader
175 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
176 } else {
913 _deallocate_list->remove_at(i);
914 if (m->is_constantPool()) {
915 ((ConstantPool*)m)->release_C_heap_structures();
916 } else if (m->is_klass()) {
917 InstanceKlass* ik = (InstanceKlass*)m;
918 // also releases ik->constants() C heap memory
919 InstanceKlass::release_C_heap_structures(ik);
920 // Remove the class so unloading events aren't triggered for
921 // this class (scratch or error class) in do_unloading().
922 remove_class(ik);
923 }
924 }
925 }
926
927 // These anonymous class loaders are to contain classes used for JSR292
928 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
929 // Add a new class loader data to the graph.
930 return ClassLoaderDataGraph::add(loader, true);
931 }
932
933 // Caller needs ResourceMark
934 // If the class loader's _name has not been explicitly set, the class loader's
935 // qualified class name is returned.
936 const char* ClassLoaderData::loader_name() const {
937 if (_class_loader_klass == NULL) {
938 return BOOTSTRAP_LOADER_NAME;
939 } else if (_name != NULL) {
940 return _name->as_C_string();
941 } else {
942 return _class_loader_klass->external_name();
943 }
944 }
945
946 // Caller needs ResourceMark
947 // Format of the _name_and_id is as follows:
948 // If the defining loader has a name explicitly set then '<loader-name>' @<id>
949 // If the defining loader has no name then <qualified-class-name> @<id>
950 // If built-in loader, then omit '@<id>' as there is only one instance.
951 const char* ClassLoaderData::loader_name_and_id() const {
952 if (_class_loader_klass == NULL) {
953 return BOOTSTRAP_LOADER_NAME;
954 } else {
955 assert(_name_and_id != NULL, "encountered a class loader null name and id");
956 return _name_and_id->as_C_string();
957 }
958 }
959
960 void ClassLoaderData::print_value_on(outputStream* out) const {
961 if (!is_unloading() && class_loader() != NULL) {
962 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
963 class_loader()->print_value_on(out); // includes loader_name_and_id() and address of class loader instance
964 } else {
965 // loader data: 0xsomeaddr of 'bootstrap'
966 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
967 }
968 if (is_anonymous()) {
969 out->print(" anonymous");
970 }
971 }
972
973 #ifndef PRODUCT
974 void ClassLoaderData::print_on(outputStream* out) const {
975 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
976 p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
977 if (is_anonymous()) out->print(" anonymous");
978 if (claimed()) out->print(" claimed");
979 if (is_unloading()) out->print(" unloading");
980 out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
981
982 if (_jmethod_ids != NULL) {
983 Method::print_jmethod_ids(this, out);
984 }
985 out->print(" handles count %d", _handles.count());
986 out->print(" dependencies %d", _dependency_count);
987 out->print_cr("}");
988 }
989 #endif // PRODUCT
990
991 void ClassLoaderData::verify() {
992 assert_locked_or_safepoint(_metaspace_lock);
993 oop cl = class_loader();
994
995 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
996 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
1250 void ClassLoaderDataGraph::verify_dictionary() {
1251 FOR_ALL_DICTIONARY(cld) {
1252 cld->dictionary()->verify();
1253 }
1254 }
1255
1256 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
1257 FOR_ALL_DICTIONARY(cld) {
1258 st->print("Dictionary for ");
1259 cld->print_value_on(st);
1260 st->cr();
1261 cld->dictionary()->print_on(st);
1262 st->cr();
1263 }
1264 }
1265
1266 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) {
1267 FOR_ALL_DICTIONARY(cld) {
1268 ResourceMark rm;
1269 stringStream tempst;
1270 tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
1271 cld->dictionary()->print_table_statistics(st, tempst.as_string());
1272 }
1273 }
1274
1275 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
1276 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
1277
1278 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
1279
1280 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
1281 ClassLoaderData* curr = _head;
1282 while (curr != _saved_head) {
1283 if (!curr->claimed()) {
1284 array->push(curr);
1285 LogTarget(Debug, class, loader, data) lt;
1286 if (lt.is_enabled()) {
1287 LogStream ls(lt);
1288 ls.print("found new CLD: ");
1289 curr->print_value_on(&ls);
1290 ls.cr();
|