8156033: jhsdb jmap cannot set heapdump name
authorysuenaga
Sat, 07 May 2016 10:29:16 +0900
changeset 38264 a6a9765aafd5
parent 38262 8e814475a28a
child 38265 087fd62badde
8156033: jhsdb jmap cannot set heapdump name Reviewed-by: dsamersoff
hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncher.java
hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/JMap.java
--- a/hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncher.java	Tue May 10 03:37:36 2016 +0000
+++ b/hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncher.java	Sat May 07 10:29:16 2016 +0900
@@ -73,6 +73,7 @@
         System.out.println("    <no option>\tto print same info as Solaris pmap");
         System.out.println("    --heap\tto print java heap summary");
         System.out.println("    --binaryheap\tto dump java heap in hprof binary format");
+        System.out.println("    --dumpfile\tname of the dump file");
         System.out.println("    --histo\tto print histogram of java object heap");
         System.out.println("    --clstats\tto print class loader statistics");
         System.out.println("    --finalizerinfo\tto print information on objects awaiting finalization");
@@ -241,13 +242,15 @@
     private static void runJMAP(String[] oldArgs) {
         SAGetopt sg = new SAGetopt(oldArgs);
         String[] longOpts = {"exe=", "core=", "pid=",
-              "heap", "binaryheap", "histo", "clstats", "finalizerinfo"};
+              "heap", "binaryheap", "dumpfile=", "histo", "clstats", "finalizerinfo"};
 
         ArrayList<String> newArgs = new ArrayList();
         String pid = null;
         String exe = null;
         String core = null;
         String s = null;
+        String dumpfile = null;
+        boolean requestHeapdump = false;
 
         while((s = sg.next(null, longOpts)) != null) {
             if (s.equals("exe")) {
@@ -267,7 +270,11 @@
                 continue;
             }
             if (s.equals("binaryheap")) {
-                newArgs.add("-heap:format=b");
+                requestHeapdump = true;
+                continue;
+            }
+            if (s.equals("dumpfile")) {
+                dumpfile = sg.getOptarg();
                 continue;
             }
             if (s.equals("histo")) {
@@ -284,6 +291,17 @@
             }
         }
 
+        if (!requestHeapdump && (dumpfile != null)) {
+            throw new IllegalArgumentException("Unexpected argument dumpfile");
+        }
+        if (requestHeapdump) {
+            if (dumpfile == null) {
+                newArgs.add("-heap:format=b");
+            } else {
+                newArgs.add("-heap:format=b,file=" + dumpfile);
+            }
+        }
+
         buildAttachArgs(newArgs, pid, exe, core, false);
         JMap.main(newArgs.toArray(new String[newArgs.size()]));
     }
--- a/hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/JMap.java	Tue May 10 03:37:36 2016 +0000
+++ b/hotspot/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/JMap.java	Sat May 07 10:29:16 2016 +0900
@@ -71,6 +71,8 @@
     public static final int MODE_HEAP_GRAPH_GXL = 5;
     public static final int MODE_FINALIZERINFO = 6;
 
+    private static String dumpfile = "heap.bin";
+
     public void run() {
         Tool tool = null;
         switch (mode) {
@@ -92,11 +94,11 @@
             break;
 
         case MODE_HEAP_GRAPH_HPROF_BIN:
-            writeHeapHprofBin();
+            writeHeapHprofBin(dumpfile);
             return;
 
         case MODE_HEAP_GRAPH_GXL:
-            writeHeapGXL();
+            writeHeapGXL(dumpfile);
             return;
 
         case MODE_FINALIZERINFO:
@@ -127,18 +129,34 @@
             } else if (modeFlag.equals("-finalizerinfo")) {
                 mode = MODE_FINALIZERINFO;
             } else {
-                int index = modeFlag.indexOf("-heap:format=");
+                int index = modeFlag.indexOf("-heap:");
                 if (index != -1) {
-                    String format = modeFlag.substring(1 + modeFlag.indexOf('='));
-                    if (format.equals("b")) {
-                        mode = MODE_HEAP_GRAPH_HPROF_BIN;
-                    } else if (format.equals("x")) {
-                        mode = MODE_HEAP_GRAPH_GXL;
-                    } else {
-                        System.err.println("unknown heap format:" + format);
+                    String[] options = modeFlag.substring(6).split(",");
+                    for (String option : options) {
+                        String[] keyValue = option.split("=");
+                        if (keyValue[0].equals("format")) {
+                            if (keyValue[1].equals("b")) {
+                                mode = MODE_HEAP_GRAPH_HPROF_BIN;
+                            } else if (keyValue[1].equals("x")) {
+                                mode = MODE_HEAP_GRAPH_GXL;
+                            } else {
+                                System.err.println("unknown heap format:" + keyValue[0]);
 
-                        // Exit with error status
-                        System.exit(1);
+                                // Exit with error status
+                                System.exit(1);
+                            }
+                        } else if (keyValue[0].equals("file")) {
+                            if ((keyValue[1] == null) || keyValue[1].equals("")) {
+                                System.err.println("File name must be set.");
+                                System.exit(1);
+                            }
+                            dumpfile = keyValue[1];
+                        } else {
+                            System.err.println("unknown option:" + keyValue[0]);
+
+                            // Exit with error status
+                            System.exit(1);
+                        }
                     }
                 } else {
                     copyArgs = false;