jdk/src/jdk.jcmd/share/classes/sun/tools/jmap/JMap.java
changeset 36511 9d0388c6b336
parent 25859 3317bb8137f4
child 38360 fb63be22ffa6
equal deleted inserted replaced
36510:043f1af70518 36511:9d0388c6b336
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.tools.jmap;
    26 package sun.tools.jmap;
    27 
    27 
    28 import java.lang.reflect.Method;
       
    29 import java.io.File;
    28 import java.io.File;
    30 import java.io.IOException;
    29 import java.io.IOException;
    31 import java.io.InputStream;
    30 import java.io.InputStream;
    32 
    31 
    33 import com.sun.tools.attach.VirtualMachine;
    32 import com.sun.tools.attach.VirtualMachine;
    34 import com.sun.tools.attach.AttachNotSupportedException;
    33 import com.sun.tools.attach.AttachNotSupportedException;
    35 import sun.tools.attach.HotSpotVirtualMachine;
    34 import sun.tools.attach.HotSpotVirtualMachine;
       
    35 import jdk.internal.vm.agent.spi.ToolProvider;
       
    36 import jdk.internal.vm.agent.spi.ToolProviderFinder;
    36 
    37 
    37 /*
    38 /*
    38  * This class is the main class for the JMap utility. It parses its arguments
    39  * This class is the main class for the JMap utility. It parses its arguments
    39  * and decides if the command should be satisfied using the VM attach mechanism
    40  * and decides if the command should be satisfied using the VM attach mechanism
    40  * or an SA tool. At this time the only option that uses the VM attach mechanism
    41  * or an SA tool. At this time the only option that uses the VM attach mechanism
   147     }
   148     }
   148 
   149 
   149     // Invoke SA tool  with the given arguments
   150     // Invoke SA tool  with the given arguments
   150     private static void runTool(String option, String args[]) throws Exception {
   151     private static void runTool(String option, String args[]) throws Exception {
   151         String[][] tools = {
   152         String[][] tools = {
   152             { "-pmap",          "sun.jvm.hotspot.tools.PMap"             },
   153             { "-pmap",          "pmap"             },
   153             { "-heap",          "sun.jvm.hotspot.tools.HeapSummary"      },
   154             { "-heap",          "heapSummary"      },
   154             { "-heap:format=b", "sun.jvm.hotspot.tools.HeapDumper"       },
   155             { "-heap:format=b", "heapDumper"       },
   155             { "-histo",         "sun.jvm.hotspot.tools.ObjectHistogram"  },
   156             { "-histo",         "objectHistogram"  },
   156             { "-clstats",       "sun.jvm.hotspot.tools.ClassLoaderStats" },
   157             { "-clstats",       "classLoaderStats" },
   157             { "-finalizerinfo", "sun.jvm.hotspot.tools.FinalizerInfo"    },
   158             { "-finalizerinfo", "finalizerInfo"    },
   158         };
   159         };
   159 
   160 
   160         String tool = null;
   161         String name = null;
   161 
   162 
   162         // -dump option needs to be handled in a special way
   163         // -dump option needs to be handled in a special way
   163         if (option.startsWith(DUMP_OPTION_PREFIX)) {
   164         if (option.startsWith(DUMP_OPTION_PREFIX)) {
   164             // first check that the option can be parsed
   165             // first check that the option can be parsed
   165             String fn = parseDumpOptions(option);
   166             String fn = parseDumpOptions(option);
   166             if (fn == null) {
   167             if (fn == null) {
   167                 usage(1);
   168                 usage(1);
   168             }
   169             }
   169 
   170 
   170             // tool for heap dumping
   171             // tool for heap dumping
   171             tool = "sun.jvm.hotspot.tools.HeapDumper";
   172             name = "heapDumper";
   172 
   173 
   173             // HeapDumper -f <file>
   174             // HeapDumper -f <file>
   174             args = prepend(fn, args);
   175             args = prepend(fn, args);
   175             args = prepend("-f", args);
   176             args = prepend("-f", args);
   176         } else {
   177         } else {
   177             int i=0;
   178             int i=0;
   178             while (i < tools.length) {
   179             while (i < tools.length) {
   179                 if (option.equals(tools[i][0])) {
   180                 if (option.equals(tools[i][0])) {
   180                     tool = tools[i][1];
   181                     name = tools[i][1];
   181                     break;
   182                     break;
   182                 }
   183                 }
   183                 i++;
   184                 i++;
   184             }
   185             }
   185         }
   186         }
       
   187         if (name == null) {
       
   188             usage(1);   // no mapping to tool
       
   189         }
       
   190 
       
   191         // Tool not available on this platform.
       
   192         ToolProvider tool = ToolProviderFinder.find(name);
   186         if (tool == null) {
   193         if (tool == null) {
   187             usage(1);   // no mapping to tool
       
   188         }
       
   189 
       
   190         // Tool not available on this  platform.
       
   191         Class<?> c = loadClass(tool);
       
   192         if (c == null) {
       
   193             usage(1);
   194             usage(1);
   194         }
   195         }
   195 
   196 
   196         // invoke the main method with the arguments
   197         // invoke the main method with the arguments
   197         Class<?>[] argTypes = { String[].class } ;
   198         tool.run(args);
   198         Method m = c.getDeclaredMethod("main", argTypes);
       
   199 
       
   200         Object[] invokeArgs = { args };
       
   201         m.invoke(null, invokeArgs);
       
   202     }
       
   203 
       
   204     // loads the given class using the system class loader
       
   205     private static Class<?> loadClass(String name) {
       
   206         //
       
   207         // We specify the system clas loader so as to cater for development
       
   208         // environments where this class is on the boot class path but sa-jdi.jar
       
   209         // is on the system class path. Once the JDK is deployed then both
       
   210         // tools.jar and sa-jdi.jar are on the system class path.
       
   211         //
       
   212         try {
       
   213             return Class.forName(name, true,
       
   214                                  ClassLoader.getSystemClassLoader());
       
   215         } catch (Exception x)  { }
       
   216         return null;
       
   217     }
   199     }
   218 
   200 
   219     private static final String LIVE_OBJECTS_OPTION = "-live";
   201     private static final String LIVE_OBJECTS_OPTION = "-live";
   220     private static final String ALL_OBJECTS_OPTION = "-all";
   202     private static final String ALL_OBJECTS_OPTION = "-all";
   221     private static void histo(String pid, boolean live) throws IOException {
   203     private static void histo(String pid, boolean live) throws IOException {
   338         return newargs;
   320         return newargs;
   339     }
   321     }
   340 
   322 
   341     // returns true if SA is available
   323     // returns true if SA is available
   342     private static boolean haveSA() {
   324     private static boolean haveSA() {
   343         Class<?> c = loadClass("sun.jvm.hotspot.tools.HeapSummary");
   325         return ToolProviderFinder.find("heapSummary") != null;
   344         return (c != null);
       
   345     }
   326     }
   346 
   327 
   347     // print usage message
   328     // print usage message
   348     private static void usage(int exit) {
   329     private static void usage(int exit) {
   349         System.err.println("Usage:");
   330         System.err.println("Usage:");