annotate agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java @ 12932:7fe6ef09d242

8025638: jmap returns 0 instead of 1 when it fails. Summary: Re-factored some code handling return values and fails/errors during tool execution. Reviewed-by: sla, kevinw Contributed-by: fredrik.arvidsson@oracle.com
author farvidsson
date Wed, 16 Oct 2013 09:20:23 +0200
parents 38ea2efa32a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
2 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.tools;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 10192
diff changeset
28 import sun.jvm.hotspot.debugger.JVMDebugger;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 public class JMap extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
32 public JMap(int m) {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 mode = m;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 }
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 public JMap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 this(MODE_PMAP);
a61af66fc99e Initial load
duke
parents:
diff changeset
38 }
a61af66fc99e Initial load
duke
parents:
diff changeset
39
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 10192
diff changeset
40 public JMap(JVMDebugger d) {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 10192
diff changeset
41 super(d);
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 10192
diff changeset
42 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 10192
diff changeset
43
0
a61af66fc99e Initial load
duke
parents:
diff changeset
44 protected boolean needsJavaPrefix() {
a61af66fc99e Initial load
duke
parents:
diff changeset
45 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 }
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
49 return "jmap";
a61af66fc99e Initial load
duke
parents:
diff changeset
50 }
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 protected String getCommandFlags() {
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
53 return "-heap|-heap:format=b|-histo|-clstats|-finalizerinfo";
0
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 protected void printFlagsUsage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 System.out.println(" <no option>\tto print same info as Solaris pmap");
a61af66fc99e Initial load
duke
parents:
diff changeset
58 System.out.println(" -heap\tto print java heap summary");
a61af66fc99e Initial load
duke
parents:
diff changeset
59 System.out.println(" -heap:format=b\tto dump java heap in hprof binary format");
a61af66fc99e Initial load
duke
parents:
diff changeset
60 System.out.println(" -histo\tto print histogram of java object heap");
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
61 System.out.println(" -clstats\tto print class loader statistics");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
62 System.out.println(" -finalizerinfo\tto print information on objects awaiting finalization");
a61af66fc99e Initial load
duke
parents:
diff changeset
63 super.printFlagsUsage();
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 public static final int MODE_HEAP_SUMMARY = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 public static final int MODE_HISTOGRAM = 1;
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
68 public static final int MODE_CLSTATS = 2;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
69 public static final int MODE_PMAP = 3;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 public static final int MODE_HEAP_GRAPH_GXL = 5;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 public static final int MODE_FINALIZERINFO = 6;
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 Tool tool = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 switch (mode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 case MODE_HEAP_SUMMARY:
a61af66fc99e Initial load
duke
parents:
diff changeset
79 tool = new HeapSummary();
a61af66fc99e Initial load
duke
parents:
diff changeset
80 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 case MODE_HISTOGRAM:
a61af66fc99e Initial load
duke
parents:
diff changeset
83 tool = new ObjectHistogram();
a61af66fc99e Initial load
duke
parents:
diff changeset
84 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
85
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
86 case MODE_CLSTATS:
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
87 tool = new ClassLoaderStats();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
88 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 case MODE_PMAP:
a61af66fc99e Initial load
duke
parents:
diff changeset
91 tool = new PMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
92 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 case MODE_HEAP_GRAPH_HPROF_BIN:
a61af66fc99e Initial load
duke
parents:
diff changeset
95 writeHeapHprofBin();
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 case MODE_HEAP_GRAPH_GXL:
a61af66fc99e Initial load
duke
parents:
diff changeset
99 writeHeapGXL();
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 case MODE_FINALIZERINFO:
a61af66fc99e Initial load
duke
parents:
diff changeset
103 tool = new FinalizerInfo();
a61af66fc99e Initial load
duke
parents:
diff changeset
104 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
107 usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
108 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 tool.setAgent(getAgent());
a61af66fc99e Initial load
duke
parents:
diff changeset
112 tool.setDebugeeType(getDebugeeType());
a61af66fc99e Initial load
duke
parents:
diff changeset
113 tool.run();
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 int mode = MODE_PMAP;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 if (args.length > 1 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 String modeFlag = args[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
120 boolean copyArgs = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 if (modeFlag.equals("-heap")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 mode = MODE_HEAP_SUMMARY;
a61af66fc99e Initial load
duke
parents:
diff changeset
123 } else if (modeFlag.equals("-histo")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 mode = MODE_HISTOGRAM;
8112
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
125 } else if (modeFlag.equals("-clstats")) {
b685ca4f4fb9 8008536: Add HotSpot support for printing class loader statistics for JMap
ehelin
parents: 1552
diff changeset
126 mode = MODE_CLSTATS;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
127 } else if (modeFlag.equals("-finalizerinfo")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 mode = MODE_FINALIZERINFO;
a61af66fc99e Initial load
duke
parents:
diff changeset
129 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 int index = modeFlag.indexOf("-heap:format=");
a61af66fc99e Initial load
duke
parents:
diff changeset
131 if (index != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 String format = modeFlag.substring(1 + modeFlag.indexOf('='));
a61af66fc99e Initial load
duke
parents:
diff changeset
133 if (format.equals("b")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 mode = MODE_HEAP_GRAPH_HPROF_BIN;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 } else if (format.equals("x")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 mode = MODE_HEAP_GRAPH_GXL;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 System.err.println("unknown heap format:" + format);
12932
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
139
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
140 // Exit with error status
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
141 System.exit(1);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
142 }
a61af66fc99e Initial load
duke
parents:
diff changeset
143 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 copyArgs = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147
a61af66fc99e Initial load
duke
parents:
diff changeset
148 if (copyArgs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
149 String[] newArgs = new String[args.length - 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
150 for (int i = 0; i < newArgs.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 newArgs[i] = args[i + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153 args = newArgs;
a61af66fc99e Initial load
duke
parents:
diff changeset
154 }
a61af66fc99e Initial load
duke
parents:
diff changeset
155 }
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 JMap jmap = new JMap(mode);
12932
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
158 jmap.execute(args);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160
a61af66fc99e Initial load
duke
parents:
diff changeset
161 public boolean writeHeapHprofBin(String fileName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 HeapGraphWriter hgw = new HeapHprofBinWriter();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 hgw.write(fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 System.out.println("heap written to " + fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
166 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
167 } catch (IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 System.err.println(exp.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
169 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 public boolean writeHeapHprofBin() {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 return writeHeapHprofBin("heap.bin");
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176
a61af66fc99e Initial load
duke
parents:
diff changeset
177 private boolean writeHeapGXL(String fileName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 HeapGraphWriter hgw = new HeapGXLWriter();
a61af66fc99e Initial load
duke
parents:
diff changeset
180 hgw.write(fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 System.out.println("heap written to " + fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
182 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 } catch (IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 System.err.println(exp.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
185 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 public boolean writeHeapGXL() {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 return writeHeapGXL("heap.xml");
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 private int mode;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 }