Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/HotSpotDiagnostic.java
38827 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.io.IOException;28import java.util.ArrayList;29import java.util.List;30import javax.management.ObjectName;3132import com.sun.management.HotSpotDiagnosticMXBean;33import com.sun.management.VMOption;34import java.security.AccessController;35import java.security.PrivilegedAction;3637/**38* Implementation of the diagnostic MBean for Hotspot VM.39*/40public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {41public HotSpotDiagnostic() {42}4344@Override45public void dumpHeap(String outputFile, boolean live) throws IOException {4647String propertyName = "jdk.management.heapdump.allowAnyFileSuffix";48PrivilegedAction<Boolean> pa = () -> Boolean.parseBoolean(System.getProperty(propertyName, "false"));49boolean allowAnyFileSuffix = AccessController.doPrivileged(pa);50if (!allowAnyFileSuffix && !outputFile.endsWith(".hprof")) {51throw new IllegalArgumentException("heapdump file must have .hprof extention");52}5354SecurityManager security = System.getSecurityManager();55if (security != null) {56security.checkWrite(outputFile);57Util.checkControlAccess();58}5960dumpHeap0(outputFile, live);61}6263private native void dumpHeap0(String outputFile, boolean live) throws IOException;6465@Override66public List<VMOption> getDiagnosticOptions() {67List<Flag> allFlags = Flag.getAllFlags();68List<VMOption> result = new ArrayList<>();69for (Flag flag : allFlags) {70if (flag.isWriteable() && flag.isExternal()) {71result.add(flag.getVMOption());72}73}74return result;75}7677@Override78public VMOption getVMOption(String name) {79if (name == null) {80throw new NullPointerException("name cannot be null");81}8283Flag f = Flag.getFlag(name);84if (f == null) {85throw new IllegalArgumentException("VM option \"" +86name + "\" does not exist");87}88return f.getVMOption();89}9091@Override92public void setVMOption(String name, String value) {93if (name == null) {94throw new NullPointerException("name cannot be null");95}96if (value == null) {97throw new NullPointerException("value cannot be null");98}99100Util.checkControlAccess();101Flag flag = Flag.getFlag(name);102if (flag == null) {103throw new IllegalArgumentException("VM option \"" +104name + "\" does not exist");105}106if (!flag.isWriteable()){107throw new IllegalArgumentException("VM Option \"" +108name + "\" is not writeable");109}110111// Check the type of the value112Object v = flag.getValue();113if (v instanceof Long) {114try {115long l = Long.parseLong(value);116Flag.setLongValue(name, l);117} catch (NumberFormatException e) {118throw new IllegalArgumentException("Invalid value:" +119" VM Option \"" + name + "\"" +120" expects numeric value", e);121}122} else if (v instanceof Double) {123try {124double d = Double.parseDouble(value);125Flag.setDoubleValue(name, d);126} catch (NumberFormatException e) {127throw new IllegalArgumentException("Invalid value:" +128" VM Option \"" + name + "\"" +129" expects numeric value", e);130}131} else if (v instanceof Boolean) {132if (!value.equalsIgnoreCase("true") &&133!value.equalsIgnoreCase("false")) {134throw new IllegalArgumentException("Invalid value:" +135" VM Option \"" + name + "\"" +136" expects \"true\" or \"false\".");137}138Flag.setBooleanValue(name, Boolean.parseBoolean(value));139} else if (v instanceof String) {140Flag.setStringValue(name, value);141} else {142throw new IllegalArgumentException("VM Option \"" +143name + "\" is of an unsupported type: " +144v.getClass().getName());145}146}147148@Override149public ObjectName getObjectName() {150return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");151}152}153154155