Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/JMap.java
41161 views
1
/*
2
* Copyright (c) 2004, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
package sun.jvm.hotspot.tools;
26
27
import java.io.*;
28
import sun.jvm.hotspot.debugger.JVMDebugger;
29
import sun.jvm.hotspot.utilities.*;
30
31
public class JMap extends Tool {
32
public JMap(int m) {
33
mode = m;
34
}
35
36
public JMap() {
37
this(MODE_PMAP);
38
}
39
40
public JMap(JVMDebugger d) {
41
super(d);
42
}
43
44
protected boolean needsJavaPrefix() {
45
return false;
46
}
47
48
public String getName() {
49
return "jmap";
50
}
51
52
protected String getCommandFlags() {
53
return "-heap|-heap:format=b[,gz=<1-9>][,file=<dumpfile>]|-heap:format=x[,file=<dumpfile>]|{-histo|-clstats|-finalizerinfo";
54
}
55
56
protected void printFlagsUsage() {
57
System.out.println(" <no option>\tTo print same info as Solaris pmap.");
58
System.out.println(" -heap\tTo print java heap summary.");
59
System.out.println(" -heap:format=b[,gz=<1-9>][,file=<dumpfile>] \tTo dump java heap in hprof binary format.");
60
System.out.println(" \tIf gz specified, the heap dump is written in gzipped format");
61
System.out.println(" \tusing the given compression level.");
62
System.err.println(" \t1 (recommended) is the fastest, 9 the strongest compression.");
63
System.out.println(" -heap:format=x[,file=<dumpfile>] \tTo dump java heap in GXL format.");
64
System.out.println(" \tPlease be aware that \"gz\" option is not valid for heap dump in GXL format.");
65
System.out.println(" -histo\tTo print histogram of java object heap.");
66
System.out.println(" -clstats\tTo print class loader statistics.");
67
System.out.println(" -finalizerinfo\tTo print information on objects awaiting finalization.");
68
super.printFlagsUsage();
69
}
70
71
public static final int MODE_HEAP_SUMMARY = 0;
72
public static final int MODE_HISTOGRAM = 1;
73
public static final int MODE_CLSTATS = 2;
74
public static final int MODE_PMAP = 3;
75
public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
76
public static final int MODE_HEAP_GRAPH_GXL = 5;
77
public static final int MODE_FINALIZERINFO = 6;
78
79
private static String dumpfile = "heap.bin";
80
private static int gzLevel = 0;
81
82
public void run() {
83
Tool tool = null;
84
switch (mode) {
85
86
case MODE_HEAP_SUMMARY:
87
tool = new HeapSummary();
88
break;
89
90
case MODE_HISTOGRAM:
91
tool = new ObjectHistogram();
92
break;
93
94
case MODE_CLSTATS:
95
tool = new ClassLoaderStats();
96
break;
97
98
case MODE_PMAP:
99
tool = new PMap();
100
break;
101
102
case MODE_HEAP_GRAPH_HPROF_BIN:
103
writeHeapHprofBin(dumpfile, gzLevel);
104
return;
105
106
case MODE_HEAP_GRAPH_GXL:
107
writeHeapGXL(dumpfile);
108
return;
109
110
case MODE_FINALIZERINFO:
111
tool = new FinalizerInfo();
112
break;
113
114
default:
115
usage();
116
break;
117
}
118
119
tool.setAgent(getAgent());
120
tool.setDebugeeType(getDebugeeType());
121
tool.run();
122
}
123
124
public static void main(String[] args) {
125
int mode = MODE_PMAP;
126
if (args.length > 1 ) {
127
String modeFlag = args[0];
128
boolean copyArgs = true;
129
if (modeFlag.equals("-heap")) {
130
mode = MODE_HEAP_SUMMARY;
131
} else if (modeFlag.equals("-histo")) {
132
mode = MODE_HISTOGRAM;
133
} else if (modeFlag.equals("-clstats")) {
134
mode = MODE_CLSTATS;
135
} else if (modeFlag.equals("-finalizerinfo")) {
136
mode = MODE_FINALIZERINFO;
137
} else {
138
int index = modeFlag.indexOf("-heap:");
139
if (index != -1) {
140
String[] options = modeFlag.substring(6).split(",");
141
for (String option : options) {
142
String[] keyValue = option.split("=");
143
if (keyValue[0].equals("format")) {
144
if (keyValue[1].equals("b")) {
145
mode = MODE_HEAP_GRAPH_HPROF_BIN;
146
} else if (keyValue[1].equals("x")) {
147
mode = MODE_HEAP_GRAPH_GXL;
148
} else {
149
System.err.println("unknown heap format:" + keyValue[0]);
150
151
// Exit with error status
152
System.exit(1);
153
}
154
} else if (keyValue[0].equals("file")) {
155
if ((keyValue[1] == null) || keyValue[1].equals("")) {
156
System.err.println("File name must be set.");
157
System.exit(1);
158
}
159
dumpfile = keyValue[1];
160
} else if (keyValue[0].equals("gz")) {
161
if (mode == MODE_HEAP_GRAPH_GXL) {
162
System.err.println("\"gz\" option is not compatible with heap dump in GXL format");
163
System.exit(1);
164
}
165
if (keyValue.length == 1) {
166
System.err.println("Argument is expected for \"gz\"");
167
System.exit(1);
168
}
169
String level = keyValue[1];
170
try {
171
gzLevel = Integer.parseInt(level);
172
} catch (NumberFormatException e) {
173
System.err.println("\"gz\" option value not an integer ("+level+")");
174
System.exit(1);
175
}
176
if (gzLevel < 1 || gzLevel > 9) {
177
System.err.println("compression level out of range (1-9): " + level);
178
System.exit(1);
179
}
180
} else {
181
System.err.println("unknown option:" + keyValue[0]);
182
183
// Exit with error status
184
System.exit(1);
185
}
186
}
187
} else {
188
copyArgs = false;
189
}
190
}
191
192
if (copyArgs) {
193
String[] newArgs = new String[args.length - 1];
194
for (int i = 0; i < newArgs.length; i++) {
195
newArgs[i] = args[i + 1];
196
}
197
args = newArgs;
198
}
199
}
200
201
JMap jmap = new JMap(mode);
202
jmap.execute(args);
203
}
204
205
public boolean writeHeapHprofBin(String fileName, int gzLevel) {
206
try {
207
HeapGraphWriter hgw;
208
if (gzLevel == 0) {
209
hgw = new HeapHprofBinWriter();
210
} else if (gzLevel >=1 && gzLevel <= 9) {
211
hgw = new HeapHprofBinWriter(gzLevel);
212
} else {
213
System.err.println("Illegal compression level: " + gzLevel);
214
return false;
215
}
216
hgw.write(fileName);
217
System.out.println("heap written to " + fileName);
218
return true;
219
} catch (IOException exp) {
220
throw new RuntimeException(exp);
221
}
222
}
223
224
public boolean writeHeapHprofBin() {
225
return writeHeapHprofBin("heap.bin", -1);
226
}
227
228
private boolean writeHeapGXL(String fileName) {
229
try {
230
HeapGraphWriter hgw = new HeapGXLWriter();
231
hgw.write(fileName);
232
System.out.println("heap written to " + fileName);
233
return true;
234
} catch (IOException exp) {
235
throw new RuntimeException(exp);
236
}
237
}
238
239
public boolean writeHeapGXL() {
240
return writeHeapGXL("heap.xml");
241
}
242
243
private int mode;
244
}
245
246