|
26 | 26 |
|
27 | 27 | import static com.oracle.svm.core.util.VMError.shouldNotReachHereUnexpectedInput;
|
28 | 28 |
|
| 29 | +import java.io.BufferedWriter; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileWriter; |
| 32 | +import java.io.IOException; |
29 | 33 | import java.lang.reflect.Array;
|
30 | 34 | import java.lang.reflect.Modifier;
|
| 35 | +import java.nio.file.Path; |
31 | 36 | import java.util.ArrayDeque;
|
32 | 37 | import java.util.Arrays;
|
33 | 38 | import java.util.Collection;
|
|
41 | 46 | import java.util.Set;
|
42 | 47 | import java.util.function.Predicate;
|
43 | 48 |
|
| 49 | +import com.oracle.svm.core.option.HostedOptionValues; |
44 | 50 | import org.graalvm.nativeimage.ImageSingletons;
|
45 | 51 | import org.graalvm.nativeimage.c.function.RelocatedPointer;
|
46 | 52 | import org.graalvm.word.UnsignedWord;
|
@@ -792,6 +798,35 @@ public ObjectInfo addLateToImageHeap(Object object, Object reason) {
|
792 | 798 | return addToImageHeap(object, (HostedClass) type, getSize(object, type), System.identityHashCode(object), reason);
|
793 | 799 | }
|
794 | 800 |
|
| 801 | + /** |
| 802 | + * Dumps metadata for every object in the image heap. |
| 803 | + */ |
| 804 | + public void dumpMetadata() { |
| 805 | + String metadataFileName = SubstrateOptions.ImageHeapMetadataDumpFileName.getValue(); |
| 806 | + if (metadataFileName == null || metadataFileName.isEmpty()) { |
| 807 | + // Do not dump metadata if the file name isn't set |
| 808 | + return; |
| 809 | + } |
| 810 | + |
| 811 | + Path metadataFilePath = SubstrateOptions.getImagePath(HostedOptionValues.singleton()).resolve(metadataFileName); |
| 812 | + File metadataFile = metadataFilePath.toFile(); |
| 813 | + String metadataDir = metadataFile.getParent(); |
| 814 | + if (!new File(metadataDir).exists()) { |
| 815 | + throw VMError.shouldNotReachHere("Image heap metadata directory does not exist: " + metadataDir); |
| 816 | + } |
| 817 | + |
| 818 | + try (FileWriter metadataOut = new FileWriter(metadataFile); |
| 819 | + BufferedWriter metadataBw = new BufferedWriter(metadataOut)) { |
| 820 | + metadataBw.write("class-name,partition,offset-in-heap,size\n"); |
| 821 | + for (ObjectInfo info : getObjects()) { |
| 822 | + String csvLine = info.getClazz().getName() + "," + info.getPartition().getName() + "," + info.getOffset() + "," + info.getSize() + System.lineSeparator(); |
| 823 | + metadataBw.write(csvLine); |
| 824 | + } |
| 825 | + } catch (IOException ex) { |
| 826 | + throw new RuntimeException("Failed to dump image heap metadata to " + metadataFile, ex); |
| 827 | + } |
| 828 | + } |
| 829 | + |
795 | 830 | private long getSize(Object object, HostedType type) {
|
796 | 831 | if (type.isInstanceClass()) {
|
797 | 832 | HostedInstanceClass clazz = (HostedInstanceClass) type;
|
|
0 commit comments