From b8236f82901467023e1fd5ea646a93afc953b1ff Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Thu, 8 Feb 2024 10:00:18 +0100 Subject: [PATCH] Create new index page for heap dump creation (#8929) Co-authored-by: Daniel Beck --- .../java/hudson/util/RemotingDiagnostics.java | 27 +++++++++++++------ .../resources/hudson/util/Messages.properties | 2 ++ .../RemotingDiagnostics/HeapDump/index.jelly | 15 +++++++++++ .../HeapDump/index.properties | 3 +++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.jelly create mode 100644 core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.properties diff --git a/core/src/main/java/hudson/util/RemotingDiagnostics.java b/core/src/main/java/hudson/util/RemotingDiagnostics.java index 993fe7849a44..1756d2933b18 100644 --- a/core/src/main/java/hudson/util/RemotingDiagnostics.java +++ b/core/src/main/java/hudson/util/RemotingDiagnostics.java @@ -31,6 +31,7 @@ import hudson.FilePath; import hudson.Functions; import hudson.Util; +import hudson.model.ModelObject; import hudson.model.User; import hudson.remoting.AsyncFutureImpl; import hudson.remoting.DelegatingCallable; @@ -56,9 +57,12 @@ import jenkins.util.ScriptListener; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.customizers.ImportCustomizer; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; import org.kohsuke.stapler.WebMethod; +import org.kohsuke.stapler.interceptor.RequirePOST; /** * Various remoting operations related to diagnostics. @@ -196,7 +200,7 @@ public FilePath call() throws IOException { * Heap dump, exposable to URL via Stapler. * */ - public static class HeapDump { + public static class HeapDump implements ModelObject { private final AccessControlled owner; private final VirtualChannel channel; @@ -205,14 +209,8 @@ public HeapDump(AccessControlled owner, VirtualChannel channel) { this.channel = channel; } - /** - * Obtains the heap dump. - */ - public void doIndex(StaplerResponse rsp) throws IOException { - rsp.sendRedirect("heapdump.hprof"); - } - @WebMethod(name = "heapdump.hprof") + @RequirePOST public void doHeapDump(StaplerRequest req, StaplerResponse rsp) throws IOException, InterruptedException { owner.checkPermission(Jenkins.ADMINISTER); rsp.setContentType("application/octet-stream"); @@ -225,8 +223,21 @@ public void doHeapDump(StaplerRequest req, StaplerResponse rsp) throws IOExcepti } } + @Restricted(DoNotUse.class) + public AccessControlled getContext() { + if (owner instanceof ModelObject) { + return owner; + } + return Jenkins.get(); + } + public FilePath obtain() throws IOException, InterruptedException { return RemotingDiagnostics.getHeapDump(channel); } + + @Override + public String getDisplayName() { + return Messages.HeapDump_DisplayName(); + } } } diff --git a/core/src/main/resources/hudson/util/Messages.properties b/core/src/main/resources/hudson/util/Messages.properties index a44fcfd5b355..c5eceafd2347 100644 --- a/core/src/main/resources/hudson/util/Messages.properties +++ b/core/src/main/resources/hudson/util/Messages.properties @@ -30,6 +30,8 @@ FormValidation.ValidateRequired=Required FormValidation.Error.Details=(show details) HttpResponses.Saved=Saved +HeapDump.DisplayName = Heap Dump + Retrier.Attempt=Attempt #{0} to do the action {1} Retrier.AttemptFailed=The attempt #{0} to do the action {1} failed Retrier.Sleeping=Sleeping for {0} milliseconds before a new attempt for the action {1} diff --git a/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.jelly b/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.jelly new file mode 100644 index 000000000000..7659f46a9133 --- /dev/null +++ b/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.jelly @@ -0,0 +1,15 @@ + + + + + + +

+ ${%blurb} +

+
+ +
+
+
+
diff --git a/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.properties b/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.properties new file mode 100644 index 000000000000..e6b547a1b841 --- /dev/null +++ b/core/src/main/resources/hudson/util/RemotingDiagnostics/HeapDump/index.properties @@ -0,0 +1,3 @@ +blurb = Download a heap dump to help diagnose problems. \ + Heap dump creation is only supported on Java runtimes based on OpenJDK/HotSpot. +title = Download Heap Dump for {0}