1 /*
  2  * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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 
 56         @SuppressWarnings("removal")
 57         SecurityManager security = System.getSecurityManager();
 58         if (security != null) {
 59             security.checkWrite(outputFile);
 60             Util.checkControlAccess();
 61         }
 62 
 63         dumpHeap0(outputFile, live);
 64     }
 65 
 66     private native void dumpHeap0(String outputFile, boolean live) throws IOException;
 67 
 68     @Override
 69     public List<VMOption> getDiagnosticOptions() {
 70         List<Flag> allFlags = Flag.getAllFlags();
 71         List<VMOption> result = new ArrayList<>();
 72         for (Flag flag : allFlags) {
 73             if (flag.isWriteable() && flag.isExternal()) {
 74                 result.add(flag.getVMOption());
 75             }
 76         }
 77         return result;
 78     }
 79 
 80     @Override
 81     public VMOption getVMOption(String name) {
 82         if (name == null) {
 83             throw new NullPointerException("name cannot be null");
 84         }
 85 
 86         Flag f = Flag.getFlag(name);
 87         if (f == null) {
 88             throw new IllegalArgumentException("VM option \"" +
 89                 name + "\" does not exist");
 90         }
 91         return f.getVMOption();
 92     }
 93 
 94     @Override
 95     public void setVMOption(String name, String value) {
 96         if (name == null) {
 97             throw new NullPointerException("name cannot be null");
 98         }
 99         if (value == null) {
100             throw new NullPointerException("value cannot be null");
101         }
102 
103         Util.checkControlAccess();
104         Flag flag = Flag.getFlag(name);
105         if (flag == null) {
106             throw new IllegalArgumentException("VM option \"" +
107                 name + "\" does not exist");
108         }
109         if (!flag.isWriteable()){
110             throw new IllegalArgumentException("VM Option \"" +
111                 name + "\" is not writeable");
112         }
113 
114         // Check the type of the value
115         Object v = flag.getValue();
116         if (v instanceof Long) {
117             try {
118                 long l = Long.parseLong(value);
119                 Flag.setLongValue(name, l);
120             } catch (NumberFormatException e) {
121                 throw new IllegalArgumentException("Invalid value:" +
122                         " VM Option \"" + name + "\"" +
123                         " expects numeric value", e);
124             }
125         } else if (v instanceof Double) {
126             try {
127                 double d = Double.parseDouble(value);
128                 Flag.setDoubleValue(name, d);
129             } catch (NumberFormatException e) {
130                 throw new IllegalArgumentException("Invalid value:" +
131                         " VM Option \"" + name + "\"" +
132                         " expects numeric value", e);
133             }
134         } else if (v instanceof Boolean) {
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 }
--- EOF ---