Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jinfo/JInfo.java
38918 views
1
/*
2
* Copyright (c) 2006, 2013, 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.tools.jinfo;
27
28
import java.lang.reflect.Method;
29
import java.io.IOException;
30
import java.io.InputStream;
31
32
import com.sun.tools.attach.VirtualMachine;
33
import sun.tools.attach.HotSpotVirtualMachine;
34
35
/*
36
* This class is the main class for the JInfo utility. It parses its arguments
37
* and decides if the command should be satisfied using the VM attach mechanism
38
* or an SA tool. At this time the only option that uses the VM attach
39
* mechanism is the -flag option to set or print a command line option of a
40
* running application. All other options are mapped to SA tools.
41
*/
42
public class JInfo {
43
44
public static void main(String[] args) throws Exception {
45
if (args.length == 0) {
46
usage(1); // no arguments
47
}
48
49
boolean useSA = true;
50
String arg1 = args[0];
51
if (arg1.startsWith("-")) {
52
if (arg1.equals("-flags") ||
53
arg1.equals("-sysprops")) {
54
// SA JInfo needs <pid> or <server> or
55
// (<executable> and <code file>). So, total
56
// argument count including option has to 2 or 3.
57
if (args.length != 2 && args.length != 3) {
58
usage(1);
59
}
60
} else if (arg1.equals("-flag")) {
61
// do not use SA, use attach-on-demand
62
useSA = false;
63
} else {
64
// unknown option or -h or -help, print help
65
int exit;
66
if (arg1.equals("-help") || arg1.equals("-h")) {
67
exit = 0;
68
} else {
69
exit = 1;
70
}
71
usage(exit);
72
}
73
}
74
75
if (useSA) {
76
runTool(args);
77
} else {
78
if (args.length == 3) {
79
String pid = args[2];
80
String option = args[1];
81
flag(pid, option);
82
} else {
83
int exit;
84
if (arg1.equals("-help") || arg1.equals("-h")) {
85
exit = 0;
86
} else {
87
exit = 1;
88
}
89
usage(exit);
90
}
91
}
92
}
93
94
// Invoke SA tool with the given arguments
95
private static void runTool(String args[]) throws Exception {
96
String tool = "sun.jvm.hotspot.tools.JInfo";
97
// Tool not available on this platform.
98
Class<?> c = loadClass(tool);
99
if (c == null) {
100
usage(1);
101
}
102
103
// invoke the main method with the arguments
104
Class[] argTypes = { String[].class } ;
105
Method m = c.getDeclaredMethod("main", argTypes);
106
107
Object[] invokeArgs = { args };
108
m.invoke(null, invokeArgs);
109
}
110
111
// loads the given class using the system class loader
112
private static Class<?> loadClass(String name) {
113
//
114
// We specify the system clas loader so as to cater for development
115
// environments where this class is on the boot class path but sa-jdi.jar
116
// is on the system class path. Once the JDK is deployed then both
117
// tools.jar and sa-jdi.jar are on the system class path.
118
//
119
try {
120
return Class.forName(name, true,
121
ClassLoader.getSystemClassLoader());
122
} catch (Exception x) { }
123
return null;
124
}
125
126
private static void flag(String pid, String option) throws IOException {
127
VirtualMachine vm = attach(pid);
128
String flag;
129
InputStream in;
130
int index = option.indexOf('=');
131
if (index != -1) {
132
flag = option.substring(0, index);
133
String value = option.substring(index + 1);
134
in = ((HotSpotVirtualMachine)vm).setFlag(flag, value);
135
} else {
136
char c = option.charAt(0);
137
switch (c) {
138
case '+':
139
flag = option.substring(1);
140
in = ((HotSpotVirtualMachine)vm).setFlag(flag, "1");
141
break;
142
case '-':
143
flag = option.substring(1);
144
in = ((HotSpotVirtualMachine)vm).setFlag(flag, "0");
145
break;
146
default:
147
flag = option;
148
in = ((HotSpotVirtualMachine)vm).printFlag(flag);
149
break;
150
}
151
}
152
153
drain(vm, in);
154
}
155
156
// Attach to <pid>, exiting if we fail to attach
157
private static VirtualMachine attach(String pid) {
158
try {
159
return VirtualMachine.attach(pid);
160
} catch (Exception x) {
161
String msg = x.getMessage();
162
if (msg != null) {
163
System.err.println(pid + ": " + msg);
164
} else {
165
x.printStackTrace();
166
}
167
System.exit(1);
168
return null; // keep compiler happy
169
}
170
}
171
172
// Read the stream from the target VM until EOF, then detach
173
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
174
// read to EOF and just print output
175
byte b[] = new byte[256];
176
int n;
177
do {
178
n = in.read(b);
179
if (n > 0) {
180
String s = new String(b, 0, n, "UTF-8");
181
System.out.print(s);
182
}
183
} while (n > 0);
184
in.close();
185
vm.detach();
186
}
187
188
189
// print usage message
190
private static void usage(int exit) {
191
192
Class<?> c = loadClass("sun.jvm.hotspot.tools.JInfo");
193
boolean usageSA = (c != null);
194
195
System.err.println("Usage:");
196
if (usageSA) {
197
System.err.println(" jinfo [option] <pid>");
198
System.err.println(" (to connect to running process)");
199
System.err.println(" jinfo [option] <executable <core>");
200
System.err.println(" (to connect to a core file)");
201
System.err.println(" jinfo [option] [server_id@]<remote server IP or hostname>");
202
System.err.println(" (to connect to remote debug server)");
203
System.err.println("");
204
System.err.println("where <option> is one of:");
205
System.err.println(" -flag <name> to print the value of the named VM flag");
206
System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag");
207
System.err.println(" -flag <name>=<value> to set the named VM flag to the given value");
208
System.err.println(" -flags to print VM flags");
209
System.err.println(" -sysprops to print Java system properties");
210
System.err.println(" <no option> to print both of the above");
211
System.err.println(" -h | -help to print this help message");
212
} else {
213
System.err.println(" jinfo <option> <pid>");
214
System.err.println(" (to connect to a running process)");
215
System.err.println("");
216
System.err.println("where <option> is one of:");
217
System.err.println(" -flag <name> to print the value of the named VM flag");
218
System.err.println(" -flag [+|-]<name> to enable or disable the named VM flag");
219
System.err.println(" -flag <name>=<value> to set the named VM flag to the given value");
220
System.err.println(" -h | -help to print this help message");
221
}
222
223
System.exit(exit);
224
}
225
}
226
227