|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.tools.jmap; |
|
|
|
import java.lang.reflect.Method; |
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
|
|
import com.sun.tools.attach.VirtualMachine; |
|
import com.sun.tools.attach.AttachNotSupportedException; |
|
import sun.tools.attach.HotSpotVirtualMachine; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class JMap { |
|
|
|
|
|
private static String HISTO_OPTION = "-histo"; |
|
private static String LIVE_HISTO_OPTION = "-histo:live"; |
|
private static String DUMP_OPTION_PREFIX = "-dump:"; |
|
|
|
|
|
private static String SA_TOOL_OPTIONS = |
|
"-heap|-heap:format=b|-clstats|-finalizerinfo"; |
|
|
|
|
|
private static String FORCE_SA_OPTION = "-F"; |
|
|
|
|
|
private static String DEFAULT_OPTION = "-pmap"; |
|
|
|
public static void main(String[] args) throws Exception { |
|
if (args.length == 0) { |
|
usage(1); |
|
} |
|
|
|
|
|
boolean useSA = false; |
|
|
|
|
|
String option = null; |
|
|
|
// First iterate over the options (arguments starting with -). There should be |
|
|
|
int optionCount = 0; |
|
while (optionCount < args.length) { |
|
String arg = args[optionCount]; |
|
if (!arg.startsWith("-")) { |
|
break; |
|
} |
|
if (arg.equals("-help") || arg.equals("-h")) { |
|
usage(0); |
|
} else if (arg.equals(FORCE_SA_OPTION)) { |
|
useSA = true; |
|
} else { |
|
if (option != null) { |
|
usage(1); |
|
} |
|
option = arg; |
|
} |
|
optionCount++; |
|
} |
|
|
|
|
|
if (option == null) { |
|
option = DEFAULT_OPTION; |
|
} |
|
if (option.matches(SA_TOOL_OPTIONS)) { |
|
useSA = true; |
|
} |
|
|
|
// Next we check the parameter count. For the SA tools there are |
|
// one or two parameters. For the built-in -dump option there is |
|
|
|
int paramCount = args.length - optionCount; |
|
if (paramCount == 0 || paramCount > 2) { |
|
usage(1); |
|
} |
|
|
|
if (optionCount == 0 || paramCount != 1) { |
|
useSA = true; |
|
} else { |
|
// the parameter for the -dump option is a process-id. |
|
// If it doesn't parse to a number then it must be SA |
|
|
|
if (!args[optionCount].matches("[0-9]+")) { |
|
useSA = true; |
|
} |
|
} |
|
|
|
|
|
// at this point we know if we are executing an SA tool or a built-in |
|
// option. |
|
|
|
if (useSA) { |
|
|
|
String params[] = new String[paramCount]; |
|
for (int i=optionCount; i<args.length; i++ ){ |
|
params[i-optionCount] = args[i]; |
|
} |
|
runTool(option, params); |
|
|
|
} else { |
|
String pid = args[1]; |
|
// Here we handle the built-in options |
|
// As more options are added we should create an abstract tool class and |
|
|
|
if (option.equals(HISTO_OPTION)) { |
|
histo(pid, false); |
|
} else if (option.equals(LIVE_HISTO_OPTION)) { |
|
histo(pid, true); |
|
} else if (option.startsWith(DUMP_OPTION_PREFIX)) { |
|
dump(pid, option); |
|
} else { |
|
usage(1); |
|
} |
|
} |
|
} |
|
|
|
|
|
private static void runTool(String option, String args[]) throws Exception { |
|
String[][] tools = { |
|
{ "-pmap", "sun.jvm.hotspot.tools.PMap" }, |
|
{ "-heap", "sun.jvm.hotspot.tools.HeapSummary" }, |
|
{ "-heap:format=b", "sun.jvm.hotspot.tools.HeapDumper" }, |
|
{ "-histo", "sun.jvm.hotspot.tools.ObjectHistogram" }, |
|
{ "-clstats", "sun.jvm.hotspot.tools.ClassLoaderStats" }, |
|
{ "-finalizerinfo", "sun.jvm.hotspot.tools.FinalizerInfo" }, |
|
}; |
|
|
|
String tool = null; |
|
|
|
|
|
if (option.startsWith(DUMP_OPTION_PREFIX)) { |
|
|
|
String fn = parseDumpOptions(option); |
|
if (fn == null) { |
|
usage(1); |
|
} |
|
|
|
|
|
tool = "sun.jvm.hotspot.tools.HeapDumper"; |
|
|
|
|
|
args = prepend(fn, args); |
|
args = prepend("-f", args); |
|
} else { |
|
int i=0; |
|
while (i < tools.length) { |
|
if (option.equals(tools[i][0])) { |
|
tool = tools[i][1]; |
|
break; |
|
} |
|
i++; |
|
} |
|
} |
|
if (tool == null) { |
|
usage(1); |
|
} |
|
|
|
|
|
Class<?> c = loadClass(tool); |
|
if (c == null) { |
|
usage(1); |
|
} |
|
|
|
|
|
Class[] argTypes = { String[].class } ; |
|
Method m = c.getDeclaredMethod("main", argTypes); |
|
|
|
Object[] invokeArgs = { args }; |
|
m.invoke(null, invokeArgs); |
|
} |
|
|
|
|
|
private static Class<?> loadClass(String name) { |
|
// |
|
// We specify the system clas loader so as to cater for development |
|
// environments where this class is on the boot class path but sa-jdi.jar |
|
// is on the system class path. Once the JDK is deployed then both |
|
// tools.jar and sa-jdi.jar are on the system class path. |
|
|
|
try { |
|
return Class.forName(name, true, |
|
ClassLoader.getSystemClassLoader()); |
|
} catch (Exception x) { } |
|
return null; |
|
} |
|
|
|
private static final String LIVE_OBJECTS_OPTION = "-live"; |
|
private static final String ALL_OBJECTS_OPTION = "-all"; |
|
private static void histo(String pid, boolean live) throws IOException { |
|
VirtualMachine vm = attach(pid); |
|
InputStream in = ((HotSpotVirtualMachine)vm). |
|
heapHisto(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION); |
|
drain(vm, in); |
|
} |
|
|
|
private static void dump(String pid, String options) throws IOException { |
|
|
|
String filename = parseDumpOptions(options); |
|
if (filename == null) { |
|
usage(1); |
|
} |
|
|
|
// get the canonical path - important to avoid just passing |
|
// a "heap.bin" and having the dump created in the target VM |
|
// working directory rather than the directory where jmap |
|
|
|
filename = new File(filename).getCanonicalPath(); |
|
|
|
|
|
boolean live = isDumpLiveObjects(options); |
|
|
|
VirtualMachine vm = attach(pid); |
|
System.out.println("Dumping heap to " + filename + " ..."); |
|
InputStream in = ((HotSpotVirtualMachine)vm). |
|
dumpHeap((Object)filename, |
|
(live ? LIVE_OBJECTS_OPTION : ALL_OBJECTS_OPTION)); |
|
drain(vm, in); |
|
} |
|
|
|
// Parse the options to the -dump option. Valid options are format=b and |
|
// file=<file>. Returns <file> if provided. Returns null if <file> not |
|
|
|
private static String parseDumpOptions(String arg) { |
|
assert arg.startsWith(DUMP_OPTION_PREFIX); |
|
|
|
String filename = null; |
|
|
|
|
|
String options[] = arg.substring(DUMP_OPTION_PREFIX.length()).split(","); |
|
|
|
for (int i=0; i<options.length; i++) { |
|
String option = options[i]; |
|
|
|
if (option.equals("format=b")) { |
|
// ignore format (not needed at this time) |
|
} else if (option.equals("live")) { |
|
// a valid suboption |
|
} else { |
|
|
|
|
|
if (option.startsWith("file=")) { |
|
filename = option.substring(5); |
|
if (filename.length() == 0) { |
|
return null; |
|
} |
|
} else { |
|
return null; |
|
} |
|
} |
|
} |
|
return filename; |
|
} |
|
|
|
private static boolean isDumpLiveObjects(String arg) { |
|
|
|
String options[] = arg.substring(DUMP_OPTION_PREFIX.length()).split(","); |
|
for (String suboption : options) { |
|
if (suboption.equals("live")) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
|
|
private static VirtualMachine attach(String pid) { |
|
try { |
|
return VirtualMachine.attach(pid); |
|
} catch (Exception x) { |
|
String msg = x.getMessage(); |
|
if (msg != null) { |
|
System.err.println(pid + ": " + msg); |
|
} else { |
|
x.printStackTrace(); |
|
} |
|
if ((x instanceof AttachNotSupportedException) && haveSA()) { |
|
System.err.println("The -F option can be used when the " + |
|
"target process is not responding"); |
|
} |
|
System.exit(1); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
private static void drain(VirtualMachine vm, InputStream in) throws IOException { |
|
|
|
byte b[] = new byte[256]; |
|
int n; |
|
do { |
|
n = in.read(b); |
|
if (n > 0) { |
|
String s = new String(b, 0, n, "UTF-8"); |
|
System.out.print(s); |
|
} |
|
} while (n > 0); |
|
in.close(); |
|
vm.detach(); |
|
} |
|
|
|
|
|
private static String[] prepend(String arg, String args[]) { |
|
String[] newargs = new String[args.length+1]; |
|
newargs[0] = arg; |
|
System.arraycopy(args, 0, newargs, 1, args.length); |
|
return newargs; |
|
} |
|
|
|
|
|
private static boolean haveSA() { |
|
Class<?> c = loadClass("sun.jvm.hotspot.tools.HeapSummary"); |
|
return (c != null); |
|
} |
|
|
|
|
|
private static void usage(int exit) { |
|
System.err.println("Usage:"); |
|
if (haveSA()) { |
|
System.err.println(" jmap [option] <pid>"); |
|
System.err.println(" (to connect to running process)"); |
|
System.err.println(" jmap [option] <executable <core>"); |
|
System.err.println(" (to connect to a core file)"); |
|
System.err.println(" jmap [option] [server_id@]<remote server IP or hostname>"); |
|
System.err.println(" (to connect to remote debug server)"); |
|
System.err.println(""); |
|
System.err.println("where <option> is one of:"); |
|
System.err.println(" <none> to print same info as Solaris pmap"); |
|
System.err.println(" -heap to print java heap summary"); |
|
System.err.println(" -histo[:live] to print histogram of java object heap; if the \"live\""); |
|
System.err.println(" suboption is specified, only count live objects"); |
|
System.err.println(" -clstats to print class loader statistics"); |
|
System.err.println(" -finalizerinfo to print information on objects awaiting finalization"); |
|
System.err.println(" -dump:<dump-options> to dump java heap in hprof binary format"); |
|
System.err.println(" dump-options:"); |
|
System.err.println(" live dump only live objects; if not specified,"); |
|
System.err.println(" all objects in the heap are dumped."); |
|
System.err.println(" format=b binary format"); |
|
System.err.println(" file=<file> dump heap to <file>"); |
|
System.err.println(" Example: jmap -dump:live,format=b,file=heap.bin <pid>"); |
|
System.err.println(" -F force. Use with -dump:<dump-options> <pid> or -histo"); |
|
System.err.println(" to force a heap dump or histogram when <pid> does not"); |
|
System.err.println(" respond. The \"live\" suboption is not supported"); |
|
System.err.println(" in this mode."); |
|
System.err.println(" -h | -help to print this help message"); |
|
System.err.println(" -J<flag> to pass <flag> directly to the runtime system"); |
|
} else { |
|
System.err.println(" jmap -histo <pid>"); |
|
System.err.println(" (to connect to running process and print histogram of java object heap"); |
|
System.err.println(" jmap -dump:<dump-options> <pid>"); |
|
System.err.println(" (to connect to running process and dump java heap)"); |
|
System.err.println(""); |
|
System.err.println(" dump-options:"); |
|
System.err.println(" format=b binary default"); |
|
System.err.println(" file=<file> dump heap to <file>"); |
|
System.err.println(""); |
|
System.err.println(" Example: jmap -dump:format=b,file=heap.bin <pid>"); |
|
} |
|
|
|
System.exit(exit); |
|
} |
|
} |