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