Skip to content

Commit

Permalink
Create new index page for heap dump creation (#8929)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
  • Loading branch information
daniel-beck and daniel-beck committed Feb 8, 2024
1 parent b91a57c commit b8236f8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
27 changes: 19 additions & 8 deletions core/src/main/java/hudson/util/RemotingDiagnostics.java
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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");
Expand All @@ -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();
}
}
}
2 changes: 2 additions & 0 deletions core/src/main/resources/hudson/util/Messages.properties
Expand Up @@ -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}
Expand Down
@@ -0,0 +1,15 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:layout permission="${app.ADMINISTER}">
<st:include page="sidepanel.jelly" it="${it.context}"/>
<l:main-panel>
<l:app-bar title="${%title(it.context.displayName)}"/>
<p class="jenkins-description">
${%blurb}
</p>
<form action="heapdump.hprof" method="post">
<input type="submit" class="jenkins-button" value="${%Download}"/>
</form>
</l:main-panel>
</l:layout>
</j:jelly>
@@ -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}

0 comments on commit b8236f8

Please sign in to comment.