1 /*
2 * Copyright (c) 2025, 2026, 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_AOTMAPLOGGER_HPP
26 #define SHARE_CDS_AOTMAPLOGGER_HPP
27
28 #include "cds/archiveBuilder.hpp"
29 #include "memory/allocation.hpp"
30 #include "memory/allStatic.hpp"
31 #include "memory/metaspaceClosureType.hpp"
32 #include "oops/oopsHierarchy.hpp"
33 #include "utilities/globalDefinitions.hpp"
34 #include "utilities/growableArray.hpp"
35
36 class AOTMappedHeapInfo;
37 class AOTStreamedHeapInfo;
38 class CompileTrainingData;
39 class DumpRegion;
40 class FileMapInfo;
41 class GrowableArrayBase;
42 class KlassTrainingData;
43 class MethodCounters;
44 class MethodTrainingData;
45 class ModuleEntry;
46 class outputStream;
47 class PackageEntry;
48
49 // Write detailed info to a mapfile to analyze contents of the AOT cache/CDS archive.
50 // -Xlog:aot+map* can be used both when creating an AOT cache, or when using an AOT cache.
51 //
52 // Creating cache:
53 // java -XX:AOTCacheOutput=app.aot -Xlog:aot+map*=trace -cp app.jar App
54 //
55 // Using cache:
56 // java -XX:AOTCache=app.aot -Xlog:aot+map*=trace -cp app.jar App
57 //
58 // You can also print the map of a cache without executing the application by using the
59 // --version flag:
60 // java -XX:AOTCache=app.aot -Xlog:aot+map*=trace --version
61 //
62 // Because the output can be large, it's best to save it to a file
63 // java -XX:AOTCache=app.aot -Xlog:aot+map*=trace:file=aot.map:none:filesize=0 --version
64 class AOTMapLogger : AllStatic {
65 struct ArchivedObjInfo {
66 address _src_addr;
67 address _buffered_addr;
68 address _requested_addr;
69 int _bytes;
70 MetaspaceClosureType _type;
71 };
72
73 public:
74 // FakeOop and subtypes
75 class FakeOop;
76 class FakeMirror;
77 class FakeRefArray;
78 class FakeFlatArray;
79 class FakeString;
80 class FakeTypeArray;
81
82 #if INCLUDE_CDS_JAVA_HEAP
83 struct OopData {
84 address _buffered_addr;
85 address _requested_addr;
86 intptr_t _target_location;
87 uint32_t _narrow_location;
88 oopDesc* _raw_oop;
89 Klass* _klass;
90 size_t _size;
91 bool _is_root_segment;
92 };
93
94 class OopDataIterator : public CHeapObj<mtClassShared> {
95 protected:
96 OopData null_data() {
97 return { nullptr,
98 nullptr,
99 0,
100 0,
101 nullptr,
102 nullptr,
103 0,
104 false };
105 }
106
107 public:
108 virtual bool has_next() = 0;
109 virtual OopData next() = 0;
110 virtual OopData obj_at(narrowOop* p) = 0;
111 virtual OopData obj_at(oop* p) = 0;
112 virtual GrowableArrayCHeap<OopData, mtClass>* roots() = 0;
113 virtual ~OopDataIterator() {}
114 };
115 #endif
116
117 private:
118 class RequestedMetadataAddr;
119 class RuntimeGatherArchivedMetaspaceObjs;
120
121 static bool _is_logging_at_bootstrap;
122 static bool _is_runtime_logging;
123 static GrowableArrayCHeap<FakeOop, mtClass>* _roots;
124
125 static intx _buffer_to_requested_delta;
126 static intx _requested_to_mapped_metadata_delta;
127
128 static void runtime_log(FileMapInfo* mapinfo, GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs);
129 static void runtime_log_metaspace_regions(FileMapInfo* mapinfo, GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs);
130 static void dumptime_log_metaspace_region(const char* name, DumpRegion* region,
131 const ArchiveBuilder::SourceObjList* src_objs);
132
133 // Common code for dumptime/runtime
134 static void log_file_header(FileMapInfo* mapinfo);
135 static void log_region_range(const char* name, address base, address top, address requested_base);
136 static void log_metaspace_objects_impl(address region_base, address region_end,
137 GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs, int start_idx, int end_idx);
138 static void log_as_hex(address base, address top, address requested_base, bool is_heap = false);
139
140 // Metaspace object: type-specific logging
141 static void log_constant_pool(ConstantPool* cp, address requested_addr, const char* type_name, int bytes, Thread* current);
142 static void log_constant_pool_cache(ConstantPoolCache* cpc, address requested_addr,
143 const char* type_name, int bytes, Thread* current);
144 static void log_const_method(ConstMethod* cm, address requested_addr, const char* type_name, int bytes, Thread* current);
145 static void log_method_counters(MethodCounters* mc, address requested_addr, const char* type_name, int bytes,
146 Thread* current);
147 static void log_method_data(MethodData* md, address requested_addr, const char* type_name, int bytes,
148 Thread* current);
149 static void log_klass(Klass* k, address requested_addr, const char* type_name, int bytes, Thread* current);
150 static void log_method(Method* m, address requested_addr, const char* type_name, int bytes, Thread* current);
151 static void log_module_entry(ModuleEntry* mod, address requested_addr, const char* type_name, int bytes, Thread* current);
152 static void log_package_entry(PackageEntry* pkg, address requested_addr, const char* type_name, int bytes, Thread* current);
153 static void log_growable_array(GrowableArrayBase* arr, address requested_addr, const char* type_name, int bytes, Thread* current);
154 static void log_symbol(Symbol* s, address requested_addr, const char* type_name, int bytes, Thread* current);
155 static void log_klass_training_data(KlassTrainingData* ktd, address requested_addr, const char* type_name, int bytes, Thread* current);
156 static void log_method_training_data(MethodTrainingData* mtd, address requested_addr, const char* type_name, int bytes, Thread* current);
157 static void log_compile_training_data(CompileTrainingData* ctd, address requested_addr, const char* type_name, int bytes, Thread* current);
158
159
160 #if INCLUDE_CDS_JAVA_HEAP
161 static void dumptime_log_mapped_heap_region(AOTMappedHeapInfo* mapped_heap_info);
162 static void dumptime_log_streamed_heap_region(AOTStreamedHeapInfo* streamed_heap_info);
163 static void runtime_log_heap_region(FileMapInfo* mapinfo);
164
165 static void print_oop_info_cr(outputStream* st, FakeOop fake_oop, bool print_location = true);
166 static void print_oop_details(FakeOop fake_oop, outputStream* st);
167 static void log_mapped_oops(address buf_start, address buf_end);
168 static void log_archived_objects(OopDataIterator* iter);
169 class ArchivedFieldPrinter; // to be replaced by ArchivedFieldPrinter2
170 #endif
171
172 public:
173 static void ergo_initialize();
174 static bool is_logging_at_bootstrap() { return _is_logging_at_bootstrap; }
175
176 static void dumptime_log(ArchiveBuilder* builder, FileMapInfo* mapinfo,
177 AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeapInfo* streamed_heap_info,
178 char* bitmap, size_t bitmap_size_in_bytes);
179 static void runtime_log(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo);
180 };
181
182 #endif // SHARE_CDS_AOTMAPLOGGER_HPP