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   FakeObjArray;
 78   class   FakeString;
 79   class   FakeTypeArray;
 80 
 81 #if INCLUDE_CDS_JAVA_HEAP
 82   struct OopData {
 83     address _buffered_addr;
 84     address _requested_addr;
 85     intptr_t _target_location;
 86     uint32_t _narrow_location;
 87     oopDesc* _raw_oop;
 88     Klass* _klass;
 89     size_t _size;
 90     bool _is_root_segment;
 91   };
 92 
 93   class OopDataIterator : public CHeapObj<mtClassShared> {
 94   protected:
 95     OopData null_data() {
 96       return { nullptr,
 97                nullptr,
 98                0,
 99                0,
100                nullptr,
101                nullptr,
102                0,
103                false };
104     }
105 
106   public:
107     virtual bool has_next() = 0;
108     virtual OopData next() = 0;
109     virtual OopData obj_at(narrowOop* p) = 0;
110     virtual OopData obj_at(oop* p) = 0;
111     virtual GrowableArrayCHeap<OopData, mtClass>* roots() = 0;
112     virtual ~OopDataIterator() {}
113   };
114 #endif
115 
116 private:
117   class RequestedMetadataAddr;
118   class RuntimeGatherArchivedMetaspaceObjs;
119 
120   static bool _is_logging_at_bootstrap;
121   static bool _is_runtime_logging;
122   static GrowableArrayCHeap<FakeOop, mtClass>* _roots;
123 
124   static intx _buffer_to_requested_delta;
125   static intx _requested_to_mapped_metadata_delta;
126 
127   static void runtime_log(FileMapInfo* mapinfo, GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs);
128   static void runtime_log_metaspace_regions(FileMapInfo* mapinfo, GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs);
129   static void dumptime_log_metaspace_region(const char* name, DumpRegion* region,
130                                             const ArchiveBuilder::SourceObjList* src_objs);
131 
132   // Common code for dumptime/runtime
133   static void log_file_header(FileMapInfo* mapinfo);
134   static void log_region_range(const char* name, address base, address top, address requested_base);
135   static void log_metaspace_objects_impl(address region_base, address region_end,
136                                          GrowableArrayCHeap<ArchivedObjInfo, mtClass>* objs, int start_idx, int end_idx);
137   static void log_as_hex(address base, address top, address requested_base, bool is_heap = false);
138 
139   // Metaspace object: type-specific logging
140   static void log_constant_pool(ConstantPool* cp, address requested_addr, const char* type_name, int bytes, Thread* current);
141   static void log_constant_pool_cache(ConstantPoolCache* cpc, address requested_addr,
142                                       const char* type_name, int bytes, Thread* current);
143   static void log_const_method(ConstMethod* cm, address requested_addr, const char* type_name, int bytes, Thread* current);
144   static void log_method_counters(MethodCounters* mc, address requested_addr, const char* type_name, int bytes,
145   Thread* current);
146   static void log_method_data(MethodData* md, address requested_addr, const char* type_name, int bytes,
147   Thread* current);
148   static void log_klass(Klass* k, address requested_addr, const char* type_name, int bytes, Thread* current);
149   static void log_method(Method* m, address requested_addr, const char* type_name, int bytes, Thread* current);
150   static void log_module_entry(ModuleEntry* mod, address requested_addr, const char* type_name, int bytes, Thread* current);
151   static void log_package_entry(PackageEntry* pkg, address requested_addr, const char* type_name, int bytes, Thread* current);
152   static void log_growable_array(GrowableArrayBase* arr, address requested_addr, const char* type_name, int bytes, Thread* current);
153   static void log_symbol(Symbol* s, address requested_addr, const char* type_name, int bytes, Thread* current);
154   static void log_klass_training_data(KlassTrainingData* ktd, address requested_addr, const char* type_name, int bytes, Thread* current);
155   static void log_method_training_data(MethodTrainingData* mtd, address requested_addr, const char* type_name, int bytes, Thread* current);
156   static void log_compile_training_data(CompileTrainingData* ctd, address requested_addr, const char* type_name, int bytes, Thread* current);
157 
158 
159 #if INCLUDE_CDS_JAVA_HEAP
160   static void dumptime_log_mapped_heap_region(AOTMappedHeapInfo* mapped_heap_info);
161   static void dumptime_log_streamed_heap_region(AOTStreamedHeapInfo* streamed_heap_info);
162   static void runtime_log_heap_region(FileMapInfo* mapinfo);
163 
164   static void print_oop_info_cr(outputStream* st, FakeOop fake_oop, bool print_location = true);
165   static void print_oop_details(FakeOop fake_oop, outputStream* st);
166   static void log_mapped_oops(address buf_start, address buf_end);
167   static void log_archived_objects(OopDataIterator* iter);
168   class ArchivedFieldPrinter; // to be replaced by ArchivedFieldPrinter2
169 #endif
170 
171 public:
172   static void ergo_initialize();
173   static bool is_logging_at_bootstrap() { return _is_logging_at_bootstrap; }
174 
175   static void dumptime_log(ArchiveBuilder* builder, FileMapInfo* mapinfo,
176                            AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeapInfo* streamed_heap_info,
177                            char* bitmap, size_t bitmap_size_in_bytes);
178   static void runtime_log(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo);
179 };
180 
181 #endif // SHARE_CDS_AOTMAPLOGGER_HPP