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 "cds/cds_globals.hpp"
26 #include "cds/classListWriter.hpp"
27 #include "cds/lambdaFormInvokers.inline.hpp"
28 #include "classfile/classFileStream.hpp"
29 #include "classfile/classLoader.hpp"
30 #include "classfile/classLoaderData.hpp"
31 #include "classfile/classLoaderDataGraph.hpp"
32 #include "classfile/moduleEntry.hpp"
33 #include "classfile/systemDictionaryShared.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "oops/constantPool.inline.hpp"
36 #include "oops/instanceKlass.hpp"
37 #include "runtime/mutexLocker.hpp"
38
39 fileStream* ClassListWriter::_classlist_file = nullptr;
40
41 void ClassListWriter::init() {
42 // For -XX:DumpLoadedClassList=<file> option
43 if (DumpLoadedClassList != nullptr) {
44 const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
45 _classlist_file = new(mtInternal)
46 fileStream(list_name);
47 _classlist_file->print_cr("# NOTE: Do not modify this file.");
48 _classlist_file->print_cr("#");
49 _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
50 _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
51 _classlist_file->print_cr("#");
52 FREE_C_HEAP_ARRAY(char, list_name);
53 }
54 }
55
56 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {
190 #endif
191 }
192
193 stream->cr();
194 stream->flush();
195 }
196
197 void ClassListWriter::delete_classlist() {
198 if (_classlist_file != nullptr) {
199 delete _classlist_file;
200 }
201 }
202
203 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
204 public:
205 void do_cld(ClassLoaderData* cld) {
206 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
207 if (klass->is_instance_klass()) {
208 InstanceKlass* ik = InstanceKlass::cast(klass);
209 write_resolved_constants_for(ik);
210 }
211 }
212 }
213 };
214
215 void ClassListWriter::write_resolved_constants() {
216 if (!is_enabled()) {
217 return;
218 }
219 MutexLocker lock(ClassLoaderDataGraph_lock);
220 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
221
222 WriteResolveConstantsCLDClosure closure;
223 ClassLoaderDataGraph::loaded_cld_do(&closure);
224 }
225
226 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
227 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
228 ik->is_hidden()) {
229 return;
230 }
231 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
232 return;
233 }
234 if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
235 // This class is regenerated during JDK build process, so the classlist
236 // may not match the version that's in the real jdk image.
237 return;
238 }
239
240 if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
241 return;
242 }
243
244 ResourceMark rm;
245 ConstantPool* cp = ik->constants();
303 }
304 }
305
306 if (print) {
307 outputStream* stream = _classlist_file;
308 stream->print("@cp %s", ik->name()->as_C_string());
309 for (int i = 0; i < list.length(); i++) {
310 if (list.at(i)) {
311 constantTag cp_tag = cp->tag_at(i).value();
312 assert(cp_tag.value() == JVM_CONSTANT_Class ||
313 cp_tag.value() == JVM_CONSTANT_Fieldref ||
314 cp_tag.value() == JVM_CONSTANT_Methodref||
315 cp_tag.value() == JVM_CONSTANT_InterfaceMethodref ||
316 cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity");
317 stream->print(" %d", i);
318 }
319 }
320 stream->cr();
321 }
322 }
|
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 "cds/aotConstantPoolResolver.hpp"
26 #include "cds/cds_globals.hpp"
27 #include "cds/classListParser.hpp"
28 #include "cds/classListWriter.hpp"
29 #include "cds/lambdaFormInvokers.inline.hpp"
30 #include "classfile/classFileStream.hpp"
31 #include "classfile/classLoader.hpp"
32 #include "classfile/classLoaderData.hpp"
33 #include "classfile/classLoaderDataGraph.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/symbolTable.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/constantPool.inline.hpp"
39 #include "oops/instanceKlass.hpp"
40 #include "runtime/javaCalls.hpp"
41 #include "runtime/mutexLocker.hpp"
42
43 fileStream* ClassListWriter::_classlist_file = nullptr;
44
45 void ClassListWriter::init() {
46 // For -XX:DumpLoadedClassList=<file> option
47 if (DumpLoadedClassList != nullptr) {
48 const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
49 _classlist_file = new(mtInternal)
50 fileStream(list_name);
51 _classlist_file->print_cr("# NOTE: Do not modify this file.");
52 _classlist_file->print_cr("#");
53 _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
54 _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
55 _classlist_file->print_cr("#");
56 FREE_C_HEAP_ARRAY(char, list_name);
57 }
58 }
59
60 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {
194 #endif
195 }
196
197 stream->cr();
198 stream->flush();
199 }
200
201 void ClassListWriter::delete_classlist() {
202 if (_classlist_file != nullptr) {
203 delete _classlist_file;
204 }
205 }
206
207 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
208 public:
209 void do_cld(ClassLoaderData* cld) {
210 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
211 if (klass->is_instance_klass()) {
212 InstanceKlass* ik = InstanceKlass::cast(klass);
213 write_resolved_constants_for(ik);
214 write_array_info_for(ik); // FIXME: piggybacking on WriteResolveConstantsCLDClosure is misleading
215 }
216 }
217 }
218 };
219
220 void ClassListWriter::write_array_info_for(InstanceKlass* ik) {
221 ObjArrayKlass* oak = ik->array_klasses();
222 if (oak != nullptr) {
223 while (oak->higher_dimension() != nullptr) {
224 oak = oak->higher_dimension();
225 }
226 ResourceMark rm;
227 outputStream* stream = _classlist_file;
228 stream->print_cr("%s %s %d", ClassListParser::ARRAY_TAG, ik->name()->as_C_string(), oak->dimension());
229 }
230 }
231
232 void ClassListWriter::write_resolved_constants() {
233 if (!is_enabled()) {
234 return;
235 }
236 MutexLocker lock(ClassLoaderDataGraph_lock);
237 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
238
239 WriteResolveConstantsCLDClosure closure;
240 ClassLoaderDataGraph::loaded_cld_do(&closure);
241 }
242
243 void ClassListWriter::write_reflection_data() {
244 if (!is_enabled()) {
245 return;
246 }
247 auto collector = [&] (const InstanceKlass* ik, int id) {
248 write_reflection_data_for(const_cast<InstanceKlass*>(ik));
249 };
250 _id_table->iterate_all(collector);
251 }
252
253 void ClassListWriter::write_reflection_data_for(InstanceKlass* ik) {
254 ResourceMark rm;
255 outputStream* stream = _classlist_file;
256 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) || ik->is_hidden()) {
257 return; // ignore
258 }
259 if (java_lang_Class::has_reflection_data(ik->java_mirror())) {
260 EXCEPTION_MARK;
261 int rd_flags = AOTConstantPoolResolver::class_reflection_data_flags(ik, THREAD);
262 if (!HAS_PENDING_EXCEPTION) {
263 // We can't hold the lock when doing the upcall inside class_reflection_data_flags()
264 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
265 stream->print_cr("%s %s %d", ClassListParser::CLASS_REFLECTION_DATA_TAG, ik->name()->as_C_string(), rd_flags);
266 }
267 }
268 }
269
270 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
271 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
272 ik->is_hidden()) {
273 return;
274 }
275 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
276 return;
277 }
278 if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
279 // This class is regenerated during JDK build process, so the classlist
280 // may not match the version that's in the real jdk image.
281 return;
282 }
283
284 if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
285 return;
286 }
287
288 ResourceMark rm;
289 ConstantPool* cp = ik->constants();
347 }
348 }
349
350 if (print) {
351 outputStream* stream = _classlist_file;
352 stream->print("@cp %s", ik->name()->as_C_string());
353 for (int i = 0; i < list.length(); i++) {
354 if (list.at(i)) {
355 constantTag cp_tag = cp->tag_at(i).value();
356 assert(cp_tag.value() == JVM_CONSTANT_Class ||
357 cp_tag.value() == JVM_CONSTANT_Fieldref ||
358 cp_tag.value() == JVM_CONSTANT_Methodref||
359 cp_tag.value() == JVM_CONSTANT_InterfaceMethodref ||
360 cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity");
361 stream->print(" %d", i);
362 }
363 }
364 stream->cr();
365 }
366 }
367
368 void ClassListWriter::write_loader_negative_lookup_cache_for(oop loader, const char* loader_type) {
369 TempNewSymbol method = SymbolTable::new_symbol("negativeLookupCacheContents");
370 TempNewSymbol signature = SymbolTable::new_symbol("()Ljava/lang/String;");
371
372 EXCEPTION_MARK;
373 HandleMark hm(THREAD);
374
375 JavaValue result(T_OBJECT);
376 JavaCalls::call_virtual(&result,
377 Handle(THREAD, loader),
378 loader->klass(),
379 method,
380 signature,
381 CHECK);
382
383 if (HAS_PENDING_EXCEPTION) {
384 log_warning(cds)("Error during BuiltinClassLoader::negativeLookupCacheContents() call for %s loader", loader_type);
385 CLEAR_PENDING_EXCEPTION;
386 return;
387 } else if (result.get_oop() == nullptr) {
388 return;
389 }
390
391 ResourceMark rm;
392 const char* cache_contents = java_lang_String::as_utf8_string(result.get_oop());
393 log_debug(cds)("%s loader negative cache: %s", loader_type, cache_contents);
394
395 outputStream* stream = _classlist_file;
396 const size_t buffer_size = strlen(ClassListParser::LOADER_NEGATIVE_CACHE_TAG) + 1 /* for space*/
397 + strlen(loader_type) + 1 /* for space */ + strlen(cache_contents) + 1 /* for null character */;
398 char* buffer = NEW_C_HEAP_ARRAY(char, buffer_size, mtInternal);
399 _classlist_file->set_scratch_buffer(buffer, buffer_size);
400 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
401 stream->print("%s %s %s", ClassListParser::LOADER_NEGATIVE_CACHE_TAG, loader_type, cache_contents);
402 stream->cr();
403 _classlist_file->set_scratch_buffer(nullptr, 0);
404 }
405
406 void ClassListWriter::write_loader_negative_lookup_cache() {
407 if (!is_enabled()) {
408 return;
409 }
410
411 write_loader_negative_lookup_cache_for(SystemDictionary::java_platform_loader(), "platform");
412 write_loader_negative_lookup_cache_for(SystemDictionary::java_system_loader(), "app");
413 }
|