Skip to content

Commit fc48301

Browse files
committed
[GR-38951] Introduce -XX:+DumpHeapAndExit option.
PullRequest: graal/11886
2 parents 6196532 + dc4c6c8 commit fc48301

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/JavaMainWrapper.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.svm.core;
2626

27+
import java.io.File;
2728
import java.lang.invoke.MethodHandle;
2829
import java.lang.invoke.MethodHandles;
2930
import java.lang.reflect.Method;
@@ -45,6 +46,7 @@
4546
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
4647
import org.graalvm.nativeimage.c.type.CTypeConversion;
4748
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
49+
import org.graalvm.nativeimage.impl.HeapDumpSupport;
4850
import org.graalvm.word.Pointer;
4951
import org.graalvm.word.UnsignedWord;
5052
import org.graalvm.word.WordFactory;
@@ -134,6 +136,18 @@ private static int runCore() {
134136
*/
135137
private static int runCore0() {
136138
try {
139+
if (SubstrateOptions.DumpHeapAndExit.getValue()) {
140+
if (VMInspectionOptions.AllowVMInspection.getValue() && ImageSingletons.contains(HeapDumpSupport.class)) {
141+
String absoluteHeapDumpPath = new File(SubstrateOptions.Name.getValue() + ".hprof").getAbsolutePath();
142+
VMRuntime.dumpHeap(absoluteHeapDumpPath, true);
143+
System.out.println("Heap dump created at '" + absoluteHeapDumpPath + "'.");
144+
return 0;
145+
} else {
146+
System.err.println("Unable to dump heap. Heap dumping is only supported for native images built with `-H:+AllowVMInspection` and GraalVM Enterprise Edition.");
147+
return 1;
148+
}
149+
}
150+
137151
if (SubstrateOptions.ParseRuntimeOptions.getValue()) {
138152
/*
139153
* When options are not parsed yet, it is also too early to run the startup hooks

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,9 @@ public Boolean getValue(OptionValues values) {
722722
}
723723
};
724724

725+
@Option(help = "Create a heap dump and exit.")//
726+
public static final RuntimeOptionKey<Boolean> DumpHeapAndExit = new ImmutableRuntimeOptionKey<>(false);
727+
725728
@Option(help = "Enable Java Flight Recorder.")//
726729
public static final RuntimeOptionKey<Boolean> FlightRecorder = new ImmutableRuntimeOptionKey<>(false);
727730

0 commit comments

Comments
 (0)