1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot;
  26 
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 
  30 import sun.jvm.hotspot.tools.JStack;
  31 import sun.jvm.hotspot.tools.JMap;
  32 import sun.jvm.hotspot.tools.JInfo;
  33 import sun.jvm.hotspot.tools.JSnap;
  34 
  35 public class SALauncher {
  36 
  37     private static boolean launcherHelp() {
  38         System.out.println("    clhsdb       \tcommand line debugger");
  39         System.out.println("    hsdb         \tui debugger");
  40         System.out.println("    jstack --help\tto get more information");
  41         System.out.println("    jmap   --help\tto get more information");
  42         System.out.println("    jinfo  --help\tto get more information");
  43         System.out.println("    jsnap  --help\tto get more information");
  44         return false;
  45     }
  46 
  47     private static boolean commonHelp() {
  48         // --pid <pid>
  49         // --exe <exe>
  50         // --core <core>
  51         System.out.println("    --exe\texecutable image name");
  52         System.out.println("    --core\tpath to coredump");
  53         System.out.println("    --pid\tpid of process to attach");
  54         return false;
  55     }
  56 
  57     private static boolean jinfoHelp() {
  58         // --flags -> -flags
  59         // --sysprops -> -sysprops
  60         System.out.println("    --flags\tto print VM flags");
  61         System.out.println("    --sysprops\tto print Java System properties");
  62         System.out.println("    <no option>\tto print both of the above");
  63         return commonHelp();
  64     }
  65 
  66     private static boolean jmapHelp() {
  67         // --heap -> -heap
  68         // --binaryheap -> -heap:format=b
  69         // --histo -> -histo
  70         // --clstats -> -clstats
  71         // --finalizerinfo -> -finalizerinfo
  72 
  73         System.out.println("    <no option>\tto print same info as Solaris pmap");
  74         System.out.println("    --heap\tto print java heap summary");
  75         System.out.println("    --binaryheap\tto dump java heap in hprof binary format");
  76         System.out.println("    --histo\tto print histogram of java object heap");
  77         System.out.println("    --clstats\tto print class loader statistics");
  78         System.out.println("    --finalizerinfo\tto print information on objects awaiting finalization");
  79         return commonHelp();
  80     }
  81 
  82     private static boolean jstackHelp() {
  83         // --locks -> -l
  84         // --mixed -> -m
  85         System.out.println("    --locks\tto print java.util.concurrent locks");
  86         System.out.println("    --mixed\tto print both java and native frames (mixed mode)");
  87         return commonHelp();
  88     }
  89 
  90     private static boolean jsnapHelp() {
  91         System.out.println(" <no option>\tdump perfromance counters");
  92         return commonHelp();
  93     }
  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 runCLHSDB(String[] oldArgs) {
 115         SAGetopt sg = new SAGetopt(oldArgs);
 116         String[] longOpts = {"exe=", "core=", "pid="};
 117 
 118         ArrayList<String> newArgs = new ArrayList();
 119         String exeORpid = null;
 120         String core = null;
 121         String s = null;
 122 
 123         while((s = sg.next(null, longOpts)) != null) {
 124             if (s.equals("exe")) {
 125                 exeORpid = sg.getOptarg();
 126                 continue;
 127             }
 128             if (s.equals("core")) {
 129                 core = sg.getOptarg();
 130                 continue;
 131             }
 132             if (s.equals("pid")) {
 133                 exeORpid = sg.getOptarg();
 134                 continue;
 135             }
 136         }
 137 
 138         if (exeORpid != null) {
 139             newArgs.add(exeORpid);
 140             if (core != null) {
 141                 newArgs.add(core);
 142             }
 143         }
 144         CLHSDB.main(newArgs.toArray(new String[newArgs.size()]));
 145     }
 146 
 147     private static void runHSDB(String[] oldArgs) {
 148         SAGetopt sg = new SAGetopt(oldArgs);
 149         String[] longOpts = {"exe=", "core=", "pid="};
 150 
 151         ArrayList<String> newArgs = new ArrayList();
 152         String exeORpid = null;
 153         String core = null;
 154         String s = null;
 155 
 156         while((s = sg.next(null, longOpts)) != null) {
 157             if (s.equals("exe")) {
 158                 exeORpid = sg.getOptarg();
 159                 continue;
 160             }
 161             if (s.equals("core")) {
 162                 core = sg.getOptarg();
 163                 continue;
 164             }
 165             if (s.equals("pid")) {
 166                 exeORpid = sg.getOptarg();
 167                 continue;
 168             }
 169         }
 170 
 171         if (exeORpid != null) {
 172             newArgs.add(exeORpid);
 173             if (core != null) {
 174                 newArgs.add(core);
 175             }
 176         }
 177         HSDB.main(newArgs.toArray(new String[newArgs.size()]));
 178     }
 179 
 180     private static void runJSTACK(String[] oldArgs) {
 181         SAGetopt sg = new SAGetopt(oldArgs);
 182         String[] longOpts = {"exe=", "core=", "pid=",
 183                                  "mixed", "locks"};
 184 
 185         ArrayList<String> newArgs = new ArrayList();
 186         String exeORpid = null;
 187         String core = null;
 188         String s = null;
 189 
 190         while((s = sg.next(null, longOpts)) != null) {
 191             if (s.equals("exe")) {
 192                 exeORpid = sg.getOptarg();
 193                 continue;
 194             }
 195             if (s.equals("core")) {
 196                 core = sg.getOptarg();
 197                 continue;
 198             }
 199             if (s.equals("pid")) {
 200                 exeORpid = sg.getOptarg();
 201                 continue;
 202             }
 203             if (s.equals("mixed")) {
 204                 newArgs.add("-m");
 205                 continue;
 206             }
 207             if (s.equals("locks")) {
 208                 newArgs.add("-l");
 209                 continue;
 210             }
 211         }
 212 
 213         if (exeORpid != null) {
 214             newArgs.add(exeORpid);
 215             if (core != null) {
 216                 newArgs.add(core);
 217             }
 218         }
 219 
 220         JStack.main(newArgs.toArray(new String[newArgs.size()]));
 221     }
 222 
 223     private static void runJMAP(String[] oldArgs) {
 224         SAGetopt sg = new SAGetopt(oldArgs);
 225         String[] longOpts = {"exe=", "core=", "pid=",
 226               "heap", "binaryheap", "histo", "clstats", "finalizerinfo"};
 227 
 228         ArrayList<String> newArgs = new ArrayList();
 229         String exeORpid = null;
 230         String core = null;
 231         String s = null;
 232 
 233         while((s = sg.next(null, longOpts)) != null) {
 234             if (s.equals("exe")) {
 235                 exeORpid = sg.getOptarg();
 236                 continue;
 237             }
 238             if (s.equals("core")) {
 239                 core = sg.getOptarg();
 240                 continue;
 241             }
 242             if (s.equals("pid")) {
 243                 exeORpid = sg.getOptarg();
 244                 continue;
 245             }
 246             if (s.equals("heap")) {
 247                 newArgs.add("-heap");
 248                 continue;
 249             }
 250             if (s.equals("binaryheap")) {
 251                 newArgs.add("-heap:format=b");
 252                 continue;
 253             }
 254             if (s.equals("histo")) {
 255                 newArgs.add("-histo");
 256                 continue;
 257             }
 258             if (s.equals("clstats")) {
 259                 newArgs.add("-clstats");
 260                 continue;
 261             }
 262             if (s.equals("finalizerinfo")) {
 263                 newArgs.add("-finalizerinfo");
 264                 continue;
 265             }
 266         }
 267 
 268         if (exeORpid != null) {
 269             newArgs.add(exeORpid);
 270             if (core != null) {
 271                 newArgs.add(core);
 272             }
 273         }
 274 
 275         JMap.main(newArgs.toArray(new String[newArgs.size()]));
 276     }
 277 
 278     private static void runJINFO(String[] oldArgs) {
 279         SAGetopt sg = new SAGetopt(oldArgs);
 280         String[] longOpts = {"exe=", "core=", "pid=",
 281                                      "flags", "sysprops"};
 282 
 283         ArrayList<String> newArgs = new ArrayList();
 284         String exeORpid = null;
 285         String core = null;
 286         String s = null;
 287 
 288         while((s = sg.next(null, longOpts)) != null) {
 289             if (s.equals("exe")) {
 290                 exeORpid = sg.getOptarg();
 291                 continue;
 292             }
 293             if (s.equals("core")) {
 294                 core = sg.getOptarg();
 295                 continue;
 296             }
 297             if (s.equals("pid")) {
 298                 exeORpid = sg.getOptarg();
 299                 continue;
 300             }
 301             if (s.equals("flags")) {
 302                 newArgs.add("-flags");
 303                 continue;
 304             }
 305             if (s.equals("sysprops")) {
 306                 newArgs.add("-sysprops");
 307                 continue;
 308             }
 309         }
 310 
 311         if (exeORpid != null) {
 312             newArgs.add(exeORpid);
 313             if (core != null) {
 314                 newArgs.add(core);
 315             }
 316         }
 317 
 318         JInfo.main(newArgs.toArray(new String[newArgs.size()]));
 319     }
 320 
 321     private static void runJSNAP(String[] oldArgs) {
 322         SAGetopt sg = new SAGetopt(oldArgs);
 323         String[] longOpts = {"exe=", "core=", "pid="};
 324 
 325         ArrayList<String> newArgs = new ArrayList();
 326         String exeORpid = null;
 327         String core = null;
 328         String s = null;
 329 
 330         while((s = sg.next(null, longOpts)) != null) {
 331             if (s.equals("exe")) {
 332                 exeORpid = sg.getOptarg();
 333                 continue;
 334             }
 335             if (s.equals("core")) {
 336                 core = sg.getOptarg();
 337                 continue;
 338             }
 339             if (s.equals("pid")) {
 340                 exeORpid = sg.getOptarg();
 341                 continue;
 342             }
 343         }
 344 
 345         if (exeORpid != null) {
 346             newArgs.add(exeORpid);
 347             if (core != null) {
 348                 newArgs.add(core);
 349             }
 350         }
 351 
 352         JSnap.main(newArgs.toArray(new String[newArgs.size()]));
 353     }
 354 
 355     public static void main(String[] args) {
 356         // Provide a help
 357         if (args.length == 0) {
 358             launcherHelp();
 359             return;
 360         }
 361         // No arguments imply help for jstack, jmap, jinfo but launch clhsdb and hsdb
 362         if (args.length == 1 && !args[0].equals("clhsdb") && !args[0].equals("hsdb")) {
 363             toolHelp(args[0]);
 364             return;
 365         }
 366 
 367         for (String arg : args) {
 368             if (arg.equals("-h") || arg.equals("-help") || arg.equals("--help")) {
 369                 toolHelp(args[0]);
 370                 return;
 371             }
 372         }
 373 
 374         String[] oldArgs = Arrays.copyOfRange(args, 1, args.length);
 375 
 376         // Run SA interactive mode
 377         if (args[0].equals("clhsdb")) {
 378             runCLHSDB(oldArgs);
 379             return;
 380         }
 381 
 382         if (args[0].equals("hsdb")) {
 383             runHSDB(oldArgs);
 384             return;
 385         }
 386 
 387         // Run SA tmtools mode
 388         if (args[0].equals("jstack")) {
 389             runJSTACK(oldArgs);
 390             return;
 391         }
 392 
 393         if (args[0].equals("jmap")) {
 394             runJMAP(oldArgs);
 395             return;
 396         }
 397 
 398         if (args[0].equals("jinfo")) {
 399             runJINFO(oldArgs);
 400             return;
 401         }
 402 
 403         if (args[0].equals("jsnap")) {
 404             runJSNAP(oldArgs);
 405             return;
 406         }
 407     }
 408 }
--- EOF ---