diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java index 3386f8c7d5ee..de2357ae5e6f 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,6 +24,7 @@ */ package com.oracle.svm.core; +import java.io.File; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.reflect.Method; @@ -45,6 +46,7 @@ import org.graalvm.nativeimage.c.type.CCharPointerPointer; import org.graalvm.nativeimage.c.type.CTypeConversion; import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder; +import org.graalvm.nativeimage.impl.HeapDumpSupport; import org.graalvm.word.Pointer; import org.graalvm.word.UnsignedWord; import org.graalvm.word.WordFactory; @@ -134,6 +136,18 @@ private static int runCore() { */ private static int runCore0() { try { + if (SubstrateOptions.DumpHeapAndExit.getValue()) { + if (VMInspectionOptions.AllowVMInspection.getValue() && ImageSingletons.contains(HeapDumpSupport.class)) { + String absoluteHeapDumpPath = new File(SubstrateOptions.Name.getValue() + ".hprof").getAbsolutePath(); + VMRuntime.dumpHeap(absoluteHeapDumpPath, true); + System.out.println("Heap dump created at '" + absoluteHeapDumpPath + "'."); + return 0; + } else { + System.err.println("Unable to dump heap. Heap dumping is only supported for native images built with `-H:+AllowVMInspection` and GraalVM Enterprise Edition."); + return 1; + } + } + if (SubstrateOptions.ParseRuntimeOptions.getValue()) { /* * When options are not parsed yet, it is also too early to run the startup hooks diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java index bf6baeb97668..889e28e0f973 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java @@ -722,6 +722,9 @@ public Boolean getValue(OptionValues values) { } }; + @Option(help = "Create a heap dump and exit.")// + public static final RuntimeOptionKey DumpHeapAndExit = new ImmutableRuntimeOptionKey<>(false); + @Option(help = "Enable Java Flight Recorder.")// public static final RuntimeOptionKey FlightRecorder = new ImmutableRuntimeOptionKey<>(false);