< prev index next >

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

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package com.sun.management.internal;
 26 
 27 import java.io.IOException;







 28 import java.util.ArrayList;
 29 import java.util.List;
 30 import javax.management.ObjectName;
 31 
 32 import com.sun.management.HotSpotDiagnosticMXBean;

 33 import com.sun.management.VMOption;
 34 import java.security.AccessController;
 35 import java.security.PrivilegedAction;
 36 import sun.management.Util;
 37 
 38 /**
 39  * Implementation of the diagnostic MBean for Hotspot VM.
 40  */
 41 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
 42     public HotSpotDiagnostic() {
 43     }
 44 
 45     @Override
 46     public void dumpHeap(String outputFile, boolean live) throws IOException {
 47 
 48         String propertyName = "jdk.management.heapdump.allowAnyFileSuffix";
 49         PrivilegedAction<Boolean> pa = () -> Boolean.parseBoolean(System.getProperty(propertyName, "false"));
 50         @SuppressWarnings("removal")
 51         boolean allowAnyFileSuffix = AccessController.doPrivileged(pa);
 52         if (!allowAnyFileSuffix && !outputFile.endsWith(".hprof")) {
 53             throw new IllegalArgumentException("heapdump file must have .hprof extention");
 54         }
 55 

135             if (!value.equalsIgnoreCase("true") &&
136                 !value.equalsIgnoreCase("false")) {
137                 throw new IllegalArgumentException("Invalid value:" +
138                     " VM Option \"" + name + "\"" +
139                     " expects \"true\" or \"false\".");
140             }
141             Flag.setBooleanValue(name, Boolean.parseBoolean(value));
142         } else if (v instanceof String) {
143             Flag.setStringValue(name, value);
144         } else {
145             throw new IllegalArgumentException("VM Option \"" +
146                 name + "\" is of an unsupported type: " +
147                 v.getClass().getName());
148         }
149     }
150 
151     @Override
152     public ObjectName getObjectName() {
153         return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
154     }






































155 }

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package com.sun.management.internal;
 26 
 27 import java.io.IOException;
 28 import java.io.OutputStream;
 29 import java.nio.file.Files;
 30 import java.nio.file.Path;
 31 import java.security.AccessController;
 32 import java.security.PrivilegedAction;
 33 import java.security.PrivilegedActionException;
 34 import java.security.PrivilegedExceptionAction;
 35 import java.util.ArrayList;
 36 import java.util.List;
 37 import javax.management.ObjectName;

 38 import com.sun.management.HotSpotDiagnosticMXBean;
 39 import com.sun.management.HotSpotDiagnosticMXBean.ThreadDumpFormat;
 40 import com.sun.management.VMOption;
 41 import jdk.internal.vm.ThreadDumper;

 42 import sun.management.Util;
 43 
 44 /**
 45  * Implementation of the diagnostic MBean for Hotspot VM.
 46  */
 47 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
 48     public HotSpotDiagnostic() {
 49     }
 50 
 51     @Override
 52     public void dumpHeap(String outputFile, boolean live) throws IOException {
 53 
 54         String propertyName = "jdk.management.heapdump.allowAnyFileSuffix";
 55         PrivilegedAction<Boolean> pa = () -> Boolean.parseBoolean(System.getProperty(propertyName, "false"));
 56         @SuppressWarnings("removal")
 57         boolean allowAnyFileSuffix = AccessController.doPrivileged(pa);
 58         if (!allowAnyFileSuffix && !outputFile.endsWith(".hprof")) {
 59             throw new IllegalArgumentException("heapdump file must have .hprof extention");
 60         }
 61 

141             if (!value.equalsIgnoreCase("true") &&
142                 !value.equalsIgnoreCase("false")) {
143                 throw new IllegalArgumentException("Invalid value:" +
144                     " VM Option \"" + name + "\"" +
145                     " expects \"true\" or \"false\".");
146             }
147             Flag.setBooleanValue(name, Boolean.parseBoolean(value));
148         } else if (v instanceof String) {
149             Flag.setStringValue(name, value);
150         } else {
151             throw new IllegalArgumentException("VM Option \"" +
152                 name + "\" is of an unsupported type: " +
153                 v.getClass().getName());
154         }
155     }
156 
157     @Override
158     public ObjectName getObjectName() {
159         return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
160     }
161 
162     @Override
163     @SuppressWarnings("removal")
164     public void dumpThreads(String outputFile, ThreadDumpFormat format) throws IOException {
165         Path file = Path.of(outputFile);
166         if (!file.isAbsolute())
167             throw new IllegalArgumentException("'outputFile' not absolute path");
168 
169         // need ManagementPermission("control")
170         @SuppressWarnings("removal")
171         SecurityManager sm = System.getSecurityManager();
172         if (sm != null)
173             Util.checkControlAccess();
174 
175         try (OutputStream out = Files.newOutputStream(file)) {
176             PrivilegedExceptionAction<Void> pa = () -> {
177                 dumpThreads(out, format);
178                 return null;
179             };
180             try {
181                 AccessController.doPrivileged(pa);
182             } catch (PrivilegedActionException pae) {
183                 Throwable cause = pae.getCause();
184                 if (cause instanceof IOException ioe)
185                     throw ioe;
186                 if (cause instanceof RuntimeException e)
187                     throw e;
188                 throw new RuntimeException(cause);
189             }
190         }
191     }
192 
193     private void dumpThreads(OutputStream out, ThreadDumpFormat format) throws IOException {
194         switch (format) {
195             case TEXT_PLAIN -> ThreadDumper.dumpThreads(out);
196             case JSON       -> ThreadDumper.dumpThreadsToJson(out);
197         }
198     }
199 }
< prev index next >