< prev index next >

src/jdk.management/share/classes/com/sun/management/internal/HotSpotDiagnostic.java

Print this page
@@ -23,18 +23,24 @@
   * questions.
   */
  package com.sun.management.internal;
  
  import java.io.IOException;
+ import java.io.OutputStream;
+ import java.nio.file.Files;
+ import java.nio.file.Path;
+ import java.security.AccessController;
+ import java.security.PrivilegedAction;
+ import java.security.PrivilegedActionException;
+ import java.security.PrivilegedExceptionAction;
  import java.util.ArrayList;
  import java.util.List;
  import javax.management.ObjectName;
- 
  import com.sun.management.HotSpotDiagnosticMXBean;
+ import com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat;
  import com.sun.management.VMOption;
- import java.security.AccessController;
- import java.security.PrivilegedAction;
+ import jdk.internal.vm.ThreadDumper;
  import sun.management.Util;
  
  /**
   * Implementation of the diagnostic MBean for Hotspot VM.
   */

@@ -150,6 +156,44 @@
  
      @Override
      public ObjectName getObjectName() {
          return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
      }
+ 
+     @Override
+     @SuppressWarnings("removal")
+     public void dumpThreads(String outputFile, ThreadDumpFormat format) throws IOException {
+         Path file = Path.of(outputFile);
+         if (!file.isAbsolute())
+             throw new IllegalArgumentException("'outputFile' not absolute path");
+ 
+         // need ManagementPermission("control")
+         @SuppressWarnings("removal")
+         SecurityManager sm = System.getSecurityManager();
+         if (sm != null)
+             Util.checkControlAccess();
+ 
+         try (OutputStream out = Files.newOutputStream(file)) {
+             PrivilegedExceptionAction<Void> pa = () -> {
+                 dumpThreads(out, format);
+                 return null;
+             };
+             try {
+                 AccessController.doPrivileged(pa);
+             } catch (PrivilegedActionException pae) {
+                 Throwable cause = pae.getCause();
+                 if (cause instanceof IOException ioe)
+                     throw ioe;
+                 if (cause instanceof RuntimeException e)
+                     throw e;
+                 throw new RuntimeException(cause);
+             }
+         }
+     }
+ 
+     private void dumpThreads(OutputStream out, ThreadDumpFormat format) throws IOException {
+         switch (format) {
+             case TEXT_PLAIN -> ThreadDumper.dumpThreads(out);
+             case JSON       -> ThreadDumper.dumpThreadsToJson(out);
+         }
+     }
  }
< prev index next >