1 /*
2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
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 #ifndef SHARE_CDS_CLASSLISTWRITER_HPP
26 #define SHARE_CDS_CLASSLISTWRITER_HPP
27 
28 #include "runtime/mutexLocker.hpp"
29 #include "runtime/thread.hpp"
30 #include "utilities/ostream.hpp"
31 
32 class ClassListWriter {
33   friend const char* make_log_name(const char* log_name, const char* force_directory);
34 
35   static fileStream* _classlist_file;
36   MutexLocker _locker;
37 public:
38 #if INCLUDE_CDS
ClassListWriter()39   ClassListWriter() : _locker(Thread::current(), ClassListFile_lock, Mutex::_no_safepoint_check_flag) {}
40 #else
41   ClassListWriter() : _locker(Thread::current(), NULL, Mutex::_no_safepoint_check_flag) {}
42 #endif
43 
stream()44   outputStream* stream() {
45     return _classlist_file;
46   }
47 
is_enabled()48   static bool is_enabled() {
49 #if INCLUDE_CDS
50     return _classlist_file != NULL && _classlist_file->is_open();
51 #else
52     return false;
53 #endif
54   }
55 
init()56   static void init() {
57 #if INCLUDE_CDS
58   // For -XX:DumpLoadedClassList=<file> option
59   if (DumpLoadedClassList != NULL) {
60     const char* list_name = make_log_name(DumpLoadedClassList, NULL);
61     _classlist_file = new(ResourceObj::C_HEAP, mtInternal)
62                          fileStream(list_name);
63     _classlist_file->print_cr("# NOTE: Do not modify this file.");
64     _classlist_file->print_cr("#");
65     _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
66     _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
67     _classlist_file->print_cr("#");
68     FREE_C_HEAP_ARRAY(char, list_name);
69   }
70 #endif
71   }
72 
delete_classlist()73   static void delete_classlist() {
74 #if INCLUDE_CDS
75     if (_classlist_file != NULL) {
76         delete _classlist_file;
77     }
78 #endif
79   }
80 };
81 
82 #endif // SHARE_CDS_CLASSLISTWRITER_HPP
83