|
1 /* |
|
2 * Copyright (c) 2005, 2015, 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 com.sun.management.internal; |
|
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 import sun.management.Util; |
|
36 |
|
37 /** |
|
38 * Implementation of the diagnostic MBean for Hotspot VM. |
|
39 */ |
|
40 public class HotSpotDiagnostic implements HotSpotDiagnosticMXBean { |
|
41 public HotSpotDiagnostic() { |
|
42 } |
|
43 |
|
44 @Override |
|
45 public void dumpHeap(String outputFile, boolean live) throws IOException { |
|
46 SecurityManager security = System.getSecurityManager(); |
|
47 if (security != null) { |
|
48 security.checkWrite(outputFile); |
|
49 Util.checkControlAccess(); |
|
50 } |
|
51 |
|
52 dumpHeap0(outputFile, live); |
|
53 } |
|
54 |
|
55 private native void dumpHeap0(String outputFile, boolean live) throws IOException; |
|
56 |
|
57 @Override |
|
58 public List<VMOption> getDiagnosticOptions() { |
|
59 List<Flag> allFlags = Flag.getAllFlags(); |
|
60 List<VMOption> result = new ArrayList<>(); |
|
61 for (Flag flag : allFlags) { |
|
62 if (flag.isWriteable() && flag.isExternal()) { |
|
63 result.add(flag.getVMOption()); |
|
64 } |
|
65 } |
|
66 return result; |
|
67 } |
|
68 |
|
69 @Override |
|
70 public VMOption getVMOption(String name) { |
|
71 if (name == null) { |
|
72 throw new NullPointerException("name cannot be null"); |
|
73 } |
|
74 |
|
75 Flag f = Flag.getFlag(name); |
|
76 if (f == null) { |
|
77 throw new IllegalArgumentException("VM option \"" + |
|
78 name + "\" does not exist"); |
|
79 } |
|
80 return f.getVMOption(); |
|
81 } |
|
82 |
|
83 @Override |
|
84 public void setVMOption(String name, String value) { |
|
85 if (name == null) { |
|
86 throw new NullPointerException("name cannot be null"); |
|
87 } |
|
88 if (value == null) { |
|
89 throw new NullPointerException("value cannot be null"); |
|
90 } |
|
91 |
|
92 Util.checkControlAccess(); |
|
93 Flag flag = Flag.getFlag(name); |
|
94 if (flag == null) { |
|
95 throw new IllegalArgumentException("VM option \"" + |
|
96 name + "\" does not exist"); |
|
97 } |
|
98 if (!flag.isWriteable()){ |
|
99 throw new IllegalArgumentException("VM Option \"" + |
|
100 name + "\" is not writeable"); |
|
101 } |
|
102 |
|
103 // Check the type of the value |
|
104 Object v = flag.getValue(); |
|
105 if (v instanceof Long) { |
|
106 try { |
|
107 long l = Long.parseLong(value); |
|
108 Flag.setLongValue(name, l); |
|
109 } catch (NumberFormatException e) { |
|
110 throw new IllegalArgumentException("Invalid value:" + |
|
111 " VM Option \"" + name + "\"" + |
|
112 " expects numeric value", e); |
|
113 } |
|
114 } else if (v instanceof Double) { |
|
115 try { |
|
116 double d = Double.parseDouble(value); |
|
117 Flag.setDoubleValue(name, d); |
|
118 } catch (NumberFormatException e) { |
|
119 throw new IllegalArgumentException("Invalid value:" + |
|
120 " VM Option \"" + name + "\"" + |
|
121 " expects numeric value", e); |
|
122 } |
|
123 } else if (v instanceof Boolean) { |
|
124 if (!value.equalsIgnoreCase("true") && |
|
125 !value.equalsIgnoreCase("false")) { |
|
126 throw new IllegalArgumentException("Invalid value:" + |
|
127 " VM Option \"" + name + "\"" + |
|
128 " expects \"true\" or \"false\"."); |
|
129 } |
|
130 Flag.setBooleanValue(name, Boolean.parseBoolean(value)); |
|
131 } else if (v instanceof String) { |
|
132 Flag.setStringValue(name, value); |
|
133 } else { |
|
134 throw new IllegalArgumentException("VM Option \"" + |
|
135 name + "\" is of an unsupported type: " + |
|
136 v.getClass().getName()); |
|
137 } |
|
138 } |
|
139 |
|
140 @Override |
|
141 public ObjectName getObjectName() { |
|
142 return Util.newObjectName("com.sun.management:type=HotSpotDiagnostic"); |
|
143 } |
|
144 } |