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