src/share/classes/sun/management/HotSpotDiagnostic.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2008, 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 
  26 package sun.management;
  27 
  28 import java.io.IOException;
  29 import java.util.ArrayList;


  30 import java.util.List;
  31 import javax.management.ObjectName;
  32 


  33 import com.sun.management.HotSpotDiagnosticMXBean;
  34 import com.sun.management.VMOption;
  35 
  36 /**
  37  * Implementation of the diagnostic MBean for Hotspot VM.
  38  */
  39 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
  40     public HotSpotDiagnostic() {




  41     }
  42 
  43     public native void dumpHeap(String outputFile, boolean live) throws IOException;
  44 
  45     public List<VMOption> getDiagnosticOptions() {
  46         List<Flag> allFlags = Flag.getAllFlags();
  47         List<VMOption> result = new ArrayList<VMOption>();
  48         for (Flag flag : allFlags) {
  49             if (flag.isWriteable() && flag.isExternal()) {
  50                 result.add(flag.getVMOption());
  51             }
  52         }
  53         return result;
  54     }
  55 
  56     public VMOption getVMOption(String name) {
  57         if (name == null) {
  58             throw new NullPointerException("name cannot be null");
  59         }
  60 


  99                 iae.initCause(e);
 100                 throw iae;
 101             }
 102         } else if (v instanceof Boolean) {
 103             if (!value.equalsIgnoreCase("true") &&
 104                 !value.equalsIgnoreCase("false")) {
 105                 throw new IllegalArgumentException("Invalid value:" +
 106                     " VM Option \"" + name + "\"" +
 107                     " expects \"true\" or \"false\".");
 108             }
 109             Flag.setBooleanValue(name, Boolean.parseBoolean(value));
 110         } else if (v instanceof String) {
 111             Flag.setStringValue(name, value);
 112         } else {
 113             throw new IllegalArgumentException("VM Option \"" +
 114                 name + "\" is of an unsupported type: " +
 115                 v.getClass().getName());
 116         }
 117     }
 118 












































 119     public ObjectName getObjectName() {
 120         return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
 121     }






 122 }
   1 /*
   2  * Copyright (c) 2005, 2011, 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 
  26 package sun.management;
  27 
  28 import java.io.IOException;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import java.util.Collections;
  32 import java.util.List;
  33 import javax.management.ObjectName;
  34 
  35 import com.sun.management.DiagnosticCommandInfo;
  36 import com.sun.management.DiagnosticCommandArgumentInfo;
  37 import com.sun.management.HotSpotDiagnosticMXBean;
  38 import com.sun.management.VMOption;
  39 
  40 /**
  41  * Implementation of the diagnostic MBean for Hotspot VM.
  42  */
  43 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean {
  44 
  45     private final VMManagement jvm;
  46 
  47     public HotSpotDiagnostic(VMManagement jvm) {
  48         this.jvm = jvm;
  49     }
  50 
  51     public native void dumpHeap(String outputFile, boolean live) throws IOException;
  52 
  53     public List<VMOption> getDiagnosticOptions() {
  54         List<Flag> allFlags = Flag.getAllFlags();
  55         List<VMOption> result = new ArrayList<VMOption>();
  56         for (Flag flag : allFlags) {
  57             if (flag.isWriteable() && flag.isExternal()) {
  58                 result.add(flag.getVMOption());
  59             }
  60         }
  61         return result;
  62     }
  63 
  64     public VMOption getVMOption(String name) {
  65         if (name == null) {
  66             throw new NullPointerException("name cannot be null");
  67         }
  68 


 107                 iae.initCause(e);
 108                 throw iae;
 109             }
 110         } else if (v instanceof Boolean) {
 111             if (!value.equalsIgnoreCase("true") &&
 112                 !value.equalsIgnoreCase("false")) {
 113                 throw new IllegalArgumentException("Invalid value:" +
 114                     " VM Option \"" + name + "\"" +
 115                     " expects \"true\" or \"false\".");
 116             }
 117             Flag.setBooleanValue(name, Boolean.parseBoolean(value));
 118         } else if (v instanceof String) {
 119             Flag.setStringValue(name, value);
 120         } else {
 121             throw new IllegalArgumentException("VM Option \"" +
 122                 name + "\" is of an unsupported type: " +
 123                 v.getClass().getName());
 124         }
 125     }
 126 
 127     public List<String> getDiagnosticCommands() {
 128         String[] commands = getDiagnosticCommands0();
 129         return commands == null ? Collections.<String>emptyList() :
 130             Arrays.asList(commands);
 131     }
 132 
 133     public DiagnosticCommandInfo getDiagnosticCommandInfo(String command) {
 134         String[] array = new String[] { command };
 135         return getDiagnosticCommandInfo0(array)[0];
 136     }
 137 
 138     public List<DiagnosticCommandInfo> getDiagnosticCommandInfo() {
 139         String[] commands = getDiagnosticCommands0();
 140         return Arrays.asList(getDiagnosticCommandInfo0(commands));
 141     }
 142 
 143     public List<DiagnosticCommandInfo> getDiagnosticCommandInfo(
 144         List<String> commands) {
 145         if (commands == null) {
 146             throw new NullPointerException();
 147         }
 148         return Arrays.asList(getDiagnosticCommandInfo0(
 149             commands.toArray(new String[commands.size()])));
 150     }
 151 
 152     public String execute(String command) {
 153         Util.checkDiagnosticCommandAccess();
 154         return executeDiagnosticCommand0(command);
 155     }
 156 
 157     public String execute(String cmd, String... arguments) {
 158         if(cmd == null) {
 159             throw new IllegalArgumentException("Missing command name");
 160         }
 161         StringBuilder sb = new StringBuilder();
 162         sb.append(cmd);
 163         sb.append(" ");
 164         for(String arg : arguments) {
 165             sb.append(arg);
 166             sb.append(" ");
 167         }
 168         return execute(sb.toString());
 169     }
 170 
 171     public ObjectName getObjectName() {
 172         return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic");
 173     }
 174 
 175     private native String[] getDiagnosticCommands0();
 176     private native DiagnosticCommandInfo[] getDiagnosticCommandInfo0(
 177         String[] commands) throws IllegalArgumentException;
 178     private native String executeDiagnosticCommand0(String command)
 179         throws IllegalArgumentException;
 180 }