< prev index next >

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SALauncher.java

Print this page




  94 
  95     private static boolean toolHelp(String toolName) {
  96         if (toolName.equals("jstack")) {
  97             return jstackHelp();
  98         }
  99         if (toolName.equals("jinfo")) {
 100             return jinfoHelp();
 101         }
 102         if (toolName.equals("jmap")) {
 103             return jmapHelp();
 104         }
 105         if (toolName.equals("jsnap")) {
 106             return jsnapHelp();
 107         }
 108         if (toolName.equals("hsdb") || toolName.equals("clhsdb")) {
 109             return commonHelp();
 110         }
 111         return launcherHelp();
 112     }
 113 
 114     private static void buildAttachArgs(ArrayList<String> newArgs,
 115                                         String pid, String exe, String core) {
 116         if ((pid == null) && (exe == null)) {
 117             throw new IllegalArgumentException(
 118                                      "You have to set --pid or --exe.");
 119         }
 120 
 121         if (pid != null) { // Attach to live process
 122             if (exe != null) {
 123                 throw new IllegalArgumentException(
 124                                              "Unnecessary argument: --exe");
 125             } else if (core != null) {
 126                 throw new IllegalArgumentException(
 127                                              "Unnecessary argument: --core");
 128             } else if (!pid.matches("^\\d+$")) {
 129                 throw new IllegalArgumentException("Invalid pid: " + pid);
 130             }
 131 
 132             newArgs.add(pid);
 133         } else {
 134             if (exe.length() == 0) {
 135                 throw new IllegalArgumentException("You have to set --exe.");
 136             }
 137 
 138             newArgs.add(exe);
 139 
 140             if ((core == null) || (core.length() == 0)) {
 141                 throw new IllegalArgumentException("You have to set --core.");
 142             }
 143 
 144             newArgs.add(core);
 145         }
 146     }
 147 
 148     private static void runCLHSDB(String[] oldArgs) {
 149         SAGetopt sg = new SAGetopt(oldArgs);
 150         String[] longOpts = {"exe=", "core=", "pid="};
 151 
 152         ArrayList<String> newArgs = new ArrayList();
 153         String pid = null;
 154         String exe = null;
 155         String core = null;
 156         String s = null;
 157 
 158         while((s = sg.next(null, longOpts)) != null) {
 159             if (s.equals("exe")) {
 160                 exe = sg.getOptarg();
 161                 continue;
 162             }
 163             if (s.equals("core")) {
 164                 core = sg.getOptarg();
 165                 continue;
 166             }
 167             if (s.equals("pid")) {
 168                 pid = sg.getOptarg();
 169                 continue;
 170             }
 171         }
 172 
 173         buildAttachArgs(newArgs, pid, exe, core);
 174         CLHSDB.main(newArgs.toArray(new String[newArgs.size()]));
 175     }
 176 
 177     private static void runHSDB(String[] oldArgs) {
 178         SAGetopt sg = new SAGetopt(oldArgs);
 179         String[] longOpts = {"exe=", "core=", "pid="};
 180 
 181         ArrayList<String> newArgs = new ArrayList();
 182         String pid = null;
 183         String exe = null;
 184         String core = null;
 185         String s = null;
 186 
 187         while((s = sg.next(null, longOpts)) != null) {
 188             if (s.equals("exe")) {
 189                 exe = sg.getOptarg();
 190                 continue;
 191             }
 192             if (s.equals("core")) {
 193                 core = sg.getOptarg();
 194                 continue;
 195             }
 196             if (s.equals("pid")) {
 197                 pid = sg.getOptarg();
 198                 continue;
 199             }
 200         }
 201 
 202         buildAttachArgs(newArgs, pid, exe, core);
 203         HSDB.main(newArgs.toArray(new String[newArgs.size()]));
 204     }
 205 
 206     private static void runJSTACK(String[] oldArgs) {
 207         SAGetopt sg = new SAGetopt(oldArgs);
 208         String[] longOpts = {"exe=", "core=", "pid=",
 209                                  "mixed", "locks"};
 210 
 211         ArrayList<String> newArgs = new ArrayList();
 212         String pid = null;
 213         String exe = null;
 214         String core = null;
 215         String s = null;
 216 
 217         while((s = sg.next(null, longOpts)) != null) {
 218             if (s.equals("exe")) {
 219                 exe = sg.getOptarg();
 220                 continue;
 221             }
 222             if (s.equals("core")) {
 223                 core = sg.getOptarg();
 224                 continue;
 225             }
 226             if (s.equals("pid")) {
 227                 pid = sg.getOptarg();
 228                 continue;
 229             }
 230             if (s.equals("mixed")) {
 231                 newArgs.add("-m");
 232                 continue;
 233             }
 234             if (s.equals("locks")) {
 235                 newArgs.add("-l");
 236                 continue;
 237             }
 238         }
 239 
 240         buildAttachArgs(newArgs, pid, exe, core);
 241         JStack.main(newArgs.toArray(new String[newArgs.size()]));
 242     }
 243 
 244     private static void runJMAP(String[] oldArgs) {
 245         SAGetopt sg = new SAGetopt(oldArgs);
 246         String[] longOpts = {"exe=", "core=", "pid=",
 247               "heap", "binaryheap", "histo", "clstats", "finalizerinfo"};
 248 
 249         ArrayList<String> newArgs = new ArrayList();
 250         String pid = null;
 251         String exe = null;
 252         String core = null;
 253         String s = null;
 254 
 255         while((s = sg.next(null, longOpts)) != null) {
 256             if (s.equals("exe")) {
 257                 exe = sg.getOptarg();
 258                 continue;
 259             }
 260             if (s.equals("core")) {


 270                 continue;
 271             }
 272             if (s.equals("binaryheap")) {
 273                 newArgs.add("-heap:format=b");
 274                 continue;
 275             }
 276             if (s.equals("histo")) {
 277                 newArgs.add("-histo");
 278                 continue;
 279             }
 280             if (s.equals("clstats")) {
 281                 newArgs.add("-clstats");
 282                 continue;
 283             }
 284             if (s.equals("finalizerinfo")) {
 285                 newArgs.add("-finalizerinfo");
 286                 continue;
 287             }
 288         }
 289 
 290         buildAttachArgs(newArgs, pid, exe, core);
 291         JMap.main(newArgs.toArray(new String[newArgs.size()]));
 292     }
 293 
 294     private static void runJINFO(String[] oldArgs) {
 295         SAGetopt sg = new SAGetopt(oldArgs);
 296         String[] longOpts = {"exe=", "core=", "pid=",
 297                                      "flags", "sysprops"};
 298 
 299         ArrayList<String> newArgs = new ArrayList();
 300         String exe = null;
 301         String pid = null;
 302         String core = null;
 303         String s = null;
 304 
 305         while((s = sg.next(null, longOpts)) != null) {
 306             if (s.equals("exe")) {
 307                 exe = sg.getOptarg();
 308                 continue;
 309             }
 310             if (s.equals("core")) {
 311                 core = sg.getOptarg();
 312                 continue;
 313             }
 314             if (s.equals("pid")) {
 315                 pid = sg.getOptarg();
 316                 continue;
 317             }
 318             if (s.equals("flags")) {
 319                 newArgs.add("-flags");
 320                 continue;
 321             }
 322             if (s.equals("sysprops")) {
 323                 newArgs.add("-sysprops");
 324                 continue;
 325             }
 326         }
 327 
 328         buildAttachArgs(newArgs, pid, exe, core);
 329         JInfo.main(newArgs.toArray(new String[newArgs.size()]));
 330     }
 331 
 332     private static void runJSNAP(String[] oldArgs) {
 333         SAGetopt sg = new SAGetopt(oldArgs);
 334         String[] longOpts = {"exe=", "core=", "pid="};
 335 
 336         ArrayList<String> newArgs = new ArrayList();
 337         String exe = null;
 338         String pid = null;
 339         String core = null;
 340         String s = null;
 341 
 342         while((s = sg.next(null, longOpts)) != null) {
 343             if (s.equals("exe")) {
 344                 exe = sg.getOptarg();
 345                 continue;
 346             }
 347             if (s.equals("core")) {
 348                 core = sg.getOptarg();
 349                 continue;
 350             }
 351             if (s.equals("pid")) {
 352                 pid = sg.getOptarg();
 353                 continue;
 354             }
 355         }
 356 
 357         buildAttachArgs(newArgs, pid, exe, core);
 358         JSnap.main(newArgs.toArray(new String[newArgs.size()]));
 359     }
 360 
 361     public static void main(String[] args) {
 362         // Provide a help
 363         if (args.length == 0) {
 364             launcherHelp();
 365             return;
 366         }
 367         // No arguments imply help for jstack, jmap, jinfo but launch clhsdb and hsdb
 368         if (args.length == 1 && !args[0].equals("clhsdb") && !args[0].equals("hsdb")) {
 369             toolHelp(args[0]);
 370             return;
 371         }
 372 
 373         for (String arg : args) {
 374             if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")) {
 375                 toolHelp(args[0]);
 376                 return;
 377             }


 395             if (args[0].equals("jstack")) {
 396                 runJSTACK(oldArgs);
 397                 return;
 398             }
 399 
 400             if (args[0].equals("jmap")) {
 401                 runJMAP(oldArgs);
 402                 return;
 403             }
 404 
 405             if (args[0].equals("jinfo")) {
 406                 runJINFO(oldArgs);
 407                 return;
 408             }
 409 
 410             if (args[0].equals("jsnap")) {
 411                 runJSNAP(oldArgs);
 412                 return;
 413             }
 414 
 415             throw new IllegalArgumentException("Unknown tool: " + args[0]);
 416         } catch (Exception e) {
 417             System.err.println(e.getMessage());
 418             toolHelp(args[0]);
 419         }
 420     }
 421 }


  94 
  95     private static boolean toolHelp(String toolName) {
  96         if (toolName.equals("jstack")) {
  97             return jstackHelp();
  98         }
  99         if (toolName.equals("jinfo")) {
 100             return jinfoHelp();
 101         }
 102         if (toolName.equals("jmap")) {
 103             return jmapHelp();
 104         }
 105         if (toolName.equals("jsnap")) {
 106             return jsnapHelp();
 107         }
 108         if (toolName.equals("hsdb") || toolName.equals("clhsdb")) {
 109             return commonHelp();
 110         }
 111         return launcherHelp();
 112     }
 113 
 114     private static void buildAttachArgs(ArrayList<String> newArgs, String pid,
 115                                   String exe, String core, boolean allowEmpty) {
 116         if (!allowEmpty && (pid == null) && (exe == null)) {
 117             throw new SAGetoptException("You have to set --pid or --exe.");

 118         }
 119 
 120         if (pid != null) { // Attach to live process
 121             if (exe != null) {
 122                 throw new SAGetoptException("Unnecessary argument: --exe");

 123             } else if (core != null) {
 124                 throw new SAGetoptException("Unnecessary argument: --core");

 125             } else if (!pid.matches("^\\d+$")) {
 126                 throw new SAGetoptException("Invalid pid: " + pid);
 127             }
 128 
 129             newArgs.add(pid);
 130         } else if (exe != null) {
 131             if (exe.length() == 0) {
 132                 throw new SAGetoptException("You have to set --exe.");
 133             }
 134 
 135             newArgs.add(exe);
 136 
 137             if ((core == null) || (core.length() == 0)) {
 138                 throw new SAGetoptException("You have to set --core.");
 139             }
 140 
 141             newArgs.add(core);
 142         }
 143     }
 144 
 145     private static void runCLHSDB(String[] oldArgs) {
 146         SAGetopt sg = new SAGetopt(oldArgs);
 147         String[] longOpts = {"exe=", "core=", "pid="};
 148 
 149         ArrayList<String> newArgs = new ArrayList();
 150         String pid = null;
 151         String exe = null;
 152         String core = null;
 153         String s = null;
 154 
 155         while((s = sg.next(null, longOpts)) != null) {
 156             if (s.equals("exe")) {
 157                 exe = sg.getOptarg();
 158                 continue;
 159             }
 160             if (s.equals("core")) {
 161                 core = sg.getOptarg();
 162                 continue;
 163             }
 164             if (s.equals("pid")) {
 165                 pid = sg.getOptarg();
 166                 continue;
 167             }
 168         }
 169 
 170         buildAttachArgs(newArgs, pid, exe, core, true);
 171         CLHSDB.main(newArgs.toArray(new String[newArgs.size()]));
 172     }
 173 
 174     private static void runHSDB(String[] oldArgs) {
 175         SAGetopt sg = new SAGetopt(oldArgs);
 176         String[] longOpts = {"exe=", "core=", "pid="};
 177 
 178         ArrayList<String> newArgs = new ArrayList();
 179         String pid = null;
 180         String exe = null;
 181         String core = null;
 182         String s = null;
 183 
 184         while((s = sg.next(null, longOpts)) != null) {
 185             if (s.equals("exe")) {
 186                 exe = sg.getOptarg();
 187                 continue;
 188             }
 189             if (s.equals("core")) {
 190                 core = sg.getOptarg();
 191                 continue;
 192             }
 193             if (s.equals("pid")) {
 194                 pid = sg.getOptarg();
 195                 continue;
 196             }
 197         }
 198 
 199         buildAttachArgs(newArgs, pid, exe, core, true);
 200         HSDB.main(newArgs.toArray(new String[newArgs.size()]));
 201     }
 202 
 203     private static void runJSTACK(String[] oldArgs) {
 204         SAGetopt sg = new SAGetopt(oldArgs);
 205         String[] longOpts = {"exe=", "core=", "pid=",
 206                                  "mixed", "locks"};
 207 
 208         ArrayList<String> newArgs = new ArrayList();
 209         String pid = null;
 210         String exe = null;
 211         String core = null;
 212         String s = null;
 213 
 214         while((s = sg.next(null, longOpts)) != null) {
 215             if (s.equals("exe")) {
 216                 exe = sg.getOptarg();
 217                 continue;
 218             }
 219             if (s.equals("core")) {
 220                 core = sg.getOptarg();
 221                 continue;
 222             }
 223             if (s.equals("pid")) {
 224                 pid = sg.getOptarg();
 225                 continue;
 226             }
 227             if (s.equals("mixed")) {
 228                 newArgs.add("-m");
 229                 continue;
 230             }
 231             if (s.equals("locks")) {
 232                 newArgs.add("-l");
 233                 continue;
 234             }
 235         }
 236 
 237         buildAttachArgs(newArgs, pid, exe, core, false);
 238         JStack.main(newArgs.toArray(new String[newArgs.size()]));
 239     }
 240 
 241     private static void runJMAP(String[] oldArgs) {
 242         SAGetopt sg = new SAGetopt(oldArgs);
 243         String[] longOpts = {"exe=", "core=", "pid=",
 244               "heap", "binaryheap", "histo", "clstats", "finalizerinfo"};
 245 
 246         ArrayList<String> newArgs = new ArrayList();
 247         String pid = null;
 248         String exe = null;
 249         String core = null;
 250         String s = null;
 251 
 252         while((s = sg.next(null, longOpts)) != null) {
 253             if (s.equals("exe")) {
 254                 exe = sg.getOptarg();
 255                 continue;
 256             }
 257             if (s.equals("core")) {


 267                 continue;
 268             }
 269             if (s.equals("binaryheap")) {
 270                 newArgs.add("-heap:format=b");
 271                 continue;
 272             }
 273             if (s.equals("histo")) {
 274                 newArgs.add("-histo");
 275                 continue;
 276             }
 277             if (s.equals("clstats")) {
 278                 newArgs.add("-clstats");
 279                 continue;
 280             }
 281             if (s.equals("finalizerinfo")) {
 282                 newArgs.add("-finalizerinfo");
 283                 continue;
 284             }
 285         }
 286 
 287         buildAttachArgs(newArgs, pid, exe, core, false);
 288         JMap.main(newArgs.toArray(new String[newArgs.size()]));
 289     }
 290 
 291     private static void runJINFO(String[] oldArgs) {
 292         SAGetopt sg = new SAGetopt(oldArgs);
 293         String[] longOpts = {"exe=", "core=", "pid=",
 294                                      "flags", "sysprops"};
 295 
 296         ArrayList<String> newArgs = new ArrayList();
 297         String exe = null;
 298         String pid = null;
 299         String core = null;
 300         String s = null;
 301 
 302         while((s = sg.next(null, longOpts)) != null) {
 303             if (s.equals("exe")) {
 304                 exe = sg.getOptarg();
 305                 continue;
 306             }
 307             if (s.equals("core")) {
 308                 core = sg.getOptarg();
 309                 continue;
 310             }
 311             if (s.equals("pid")) {
 312                 pid = sg.getOptarg();
 313                 continue;
 314             }
 315             if (s.equals("flags")) {
 316                 newArgs.add("-flags");
 317                 continue;
 318             }
 319             if (s.equals("sysprops")) {
 320                 newArgs.add("-sysprops");
 321                 continue;
 322             }
 323         }
 324 
 325         buildAttachArgs(newArgs, pid, exe, core, false);
 326         JInfo.main(newArgs.toArray(new String[newArgs.size()]));
 327     }
 328 
 329     private static void runJSNAP(String[] oldArgs) {
 330         SAGetopt sg = new SAGetopt(oldArgs);
 331         String[] longOpts = {"exe=", "core=", "pid="};
 332 
 333         ArrayList<String> newArgs = new ArrayList();
 334         String exe = null;
 335         String pid = null;
 336         String core = null;
 337         String s = null;
 338 
 339         while((s = sg.next(null, longOpts)) != null) {
 340             if (s.equals("exe")) {
 341                 exe = sg.getOptarg();
 342                 continue;
 343             }
 344             if (s.equals("core")) {
 345                 core = sg.getOptarg();
 346                 continue;
 347             }
 348             if (s.equals("pid")) {
 349                 pid = sg.getOptarg();
 350                 continue;
 351             }
 352         }
 353 
 354         buildAttachArgs(newArgs, pid, exe, core, false);
 355         JSnap.main(newArgs.toArray(new String[newArgs.size()]));
 356     }
 357 
 358     public static void main(String[] args) {
 359         // Provide a help
 360         if (args.length == 0) {
 361             launcherHelp();
 362             return;
 363         }
 364         // No arguments imply help for jstack, jmap, jinfo but launch clhsdb and hsdb
 365         if (args.length == 1 && !args[0].equals("clhsdb") && !args[0].equals("hsdb")) {
 366             toolHelp(args[0]);
 367             return;
 368         }
 369 
 370         for (String arg : args) {
 371             if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")) {
 372                 toolHelp(args[0]);
 373                 return;
 374             }


 392             if (args[0].equals("jstack")) {
 393                 runJSTACK(oldArgs);
 394                 return;
 395             }
 396 
 397             if (args[0].equals("jmap")) {
 398                 runJMAP(oldArgs);
 399                 return;
 400             }
 401 
 402             if (args[0].equals("jinfo")) {
 403                 runJINFO(oldArgs);
 404                 return;
 405             }
 406 
 407             if (args[0].equals("jsnap")) {
 408                 runJSNAP(oldArgs);
 409                 return;
 410             }
 411 
 412             throw new SAGetoptException("Unknown tool: " + args[0]);
 413         } catch (SAGetoptException e) {
 414             System.err.println(e.getMessage());
 415             toolHelp(args[0]);
 416         }
 417     }
 418 }
< prev index next >