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.
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 {
177 // Create unnamed module for all other loaders
178 _unnamed_module = ModuleEntry::create_unnamed_module(this);
179 }
180 _dictionary = create_dictionary();
181 }
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());
1055 if (exchanged == next) {
1056 LogTarget(Debug, class, loader, data) lt;
1057 if (lt.is_enabled()) {
1058 ResourceMark rm;
1059 LogStream ls(lt);
1060 ls.print("create ");
1061 cld->print_value_on(&ls);
1062 ls.cr();
1063 }
1064 return cld;
1065 }
1066 next = exchanged;
1067 } while (true);
1068 }
1069
1070 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
1071 ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
1072 // Initialize name and class after the loader data is added to the CLDG
1073 // because adding the Symbol for the name might safepoint.
1074 if (loader.not_null()) {
1075 loader_data->initialize_name_and_klass(loader);
1076 }
1077 return loader_data;
1078 }
1079
1080 void ClassLoaderDataGraph::oops_do(OopClosure* f, bool must_claim) {
1081 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1082 cld->oops_do(f, must_claim);
1083 }
1084 }
1085
1086 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, bool must_claim) {
1087 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1088 if (cld->keep_alive()) {
1089 cld->oops_do(f, must_claim);
1090 }
1091 }
1092 }
1093
1094 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, bool must_claim) {
1095 if (ClassUnloading) {
|
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(Handle class_loader) {
113 Thread* THREAD = Thread::current();
114 ResourceMark rm(THREAD);
115
116 // Obtain the class loader's name. If the class loader's name was not
117 // explicitly set during construction, the CLD's _name field will be null.
118 oop cl_name = java_lang_ClassLoader::name(class_loader());
119 if (cl_name != NULL) {
120 const char* cl_instance_name = java_lang_String::as_utf8_string(cl_name);
121
122 if (cl_instance_name != NULL && cl_instance_name[0] != '\0') {
123 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
124 _name = SymbolTable::new_symbol(cl_instance_name, CATCH);
125 }
126 }
127
128 // Obtain the class loader's name and identity hash. If the class loader's
129 // name was not explicitly set during construction, the class loader's name and id
130 // will be set to the qualified class name of the class loader along with its
131 // identity hash.
132 // If for some reason the ClassLoader's constructor has not been run, instead of
133 // leaving the _name_and_id field null, fall back to the external qualified class
134 // name. Thus CLD's _name_and_id field should never have a null value.
141 _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
142 }
143
144 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
145 _is_anonymous(is_anonymous),
146 // An anonymous class loader data doesn't have anything to keep
147 // it from being unloaded during parsing of the anonymous class.
148 // The null-class-loader should always be kept alive.
149 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
150 _metaspace(NULL), _unloading(false), _klasses(NULL),
151 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
152 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
153 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
154 _next(NULL),
155 _class_loader_klass(NULL), _name(NULL), _name_and_id(NULL),
156 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
157 Monitor::_safepoint_check_never)) {
158
159 if (!h_class_loader.is_null()) {
160 _class_loader = _handles.add(h_class_loader());
161 _class_loader_klass = h_class_loader->klass();
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 {
177 // Create unnamed module for all other loaders
178 _unnamed_module = ModuleEntry::create_unnamed_module(this);
179 }
180 _dictionary = create_dictionary();
181 }
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 if (_name_and_id != NULL) {
955 return _name_and_id->as_C_string();
956 } else {
957 // May be called in a race before _name_and_id is initialized.
958 return _class_loader_klass->external_name();
959 }
960 }
961
962 void ClassLoaderData::print_value_on(outputStream* out) const {
963 if (!is_unloading() && class_loader() != NULL) {
964 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
965 class_loader()->print_value_on(out); // includes loader_name_and_id() and address of class loader instance
966 } else {
967 // loader data: 0xsomeaddr of 'bootstrap'
968 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
969 }
970 if (is_anonymous()) {
971 out->print(" anonymous");
972 }
973 }
974
975 #ifndef PRODUCT
976 void ClassLoaderData::print_on(outputStream* out) const {
977 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
978 p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
1057 if (exchanged == next) {
1058 LogTarget(Debug, class, loader, data) lt;
1059 if (lt.is_enabled()) {
1060 ResourceMark rm;
1061 LogStream ls(lt);
1062 ls.print("create ");
1063 cld->print_value_on(&ls);
1064 ls.cr();
1065 }
1066 return cld;
1067 }
1068 next = exchanged;
1069 } while (true);
1070 }
1071
1072 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
1073 ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
1074 // Initialize name and class after the loader data is added to the CLDG
1075 // because adding the Symbol for the name might safepoint.
1076 if (loader.not_null()) {
1077 loader_data->initialize_name(loader);
1078 }
1079 return loader_data;
1080 }
1081
1082 void ClassLoaderDataGraph::oops_do(OopClosure* f, bool must_claim) {
1083 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1084 cld->oops_do(f, must_claim);
1085 }
1086 }
1087
1088 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, bool must_claim) {
1089 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1090 if (cld->keep_alive()) {
1091 cld->oops_do(f, must_claim);
1092 }
1093 }
1094 }
1095
1096 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, bool must_claim) {
1097 if (ClassUnloading) {
|