1 /*
   2  * Copyright (c) 2004, 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.tools;
  26 
  27 import sun.jvm.hotspot.runtime.*;
  28 
  29 public class JInfo extends Tool {
  30     public JInfo(int m) {
  31         mode = m;
  32     }
  33 
  34     protected boolean needsJavaPrefix() {
  35         return false;
  36     }
  37 
  38     public String getName() {
  39         return "jinfo";
  40     }
  41 
  42     protected void printFlagsUsage() {
  43         System.out.println("    -flags\tto print VM flags");
  44         System.out.println("    -sysprops\tto print Java System properties");
  45         System.out.println("    <no option>\tto print both of the above");
  46         super.printFlagsUsage();
  47     }
  48 
  49     public static final int MODE_FLAGS = 0;
  50     public static final int MODE_SYSPROPS = 1;
  51     public static final int MODE_BOTH = 2;
  52 
  53     public void run() {
  54         Tool tool = null;
  55         switch (mode) {
  56         case MODE_FLAGS:
  57             printVMFlags();
  58             return;
  59         case MODE_SYSPROPS:
  60             tool = new SysPropsDumper();
  61             break;
  62         case MODE_BOTH: {
  63             tool = new Tool() {
  64                     public void run() {
  65                         Tool sysProps = new SysPropsDumper();
  66                         sysProps.setAgent(getAgent());
  67                         System.out.println("Java System Properties:");
  68                         System.out.println();
  69                         sysProps.run();
  70                         System.out.println();
  71                         System.out.println("VM Flags:");
  72                         printVMFlags();
  73                         System.out.println();
  74                     }
  75                 };
  76             }
  77             break;
  78 
  79         default:
  80             usage();
  81             break;
  82         }
  83         tool.setAgent(getAgent());
  84         tool.run();
  85     }
  86 
  87     public static void main(String[] args) {
  88         int mode = -1;
  89         switch (args.length) {
  90         case 1:
  91             if (args[0].charAt(0) == '-') {
  92                 // -h or -help or some invalid flag
  93                 new JInfo(mode).usage();
  94             } else {
  95                 mode = MODE_BOTH;
  96             }
  97             break;
  98         case 2:
  99         case 3: {
 100             String modeFlag = args[0];
 101             if (modeFlag.equals("-flags")) {
 102                 mode = MODE_FLAGS;
 103             } else if (modeFlag.equals("-sysprops")) {
 104                 mode = MODE_SYSPROPS;
 105             } else if (modeFlag.charAt(0) == '-') {
 106                 // -h or -help or some invalid flag
 107                 new JInfo(mode).usage();
 108             } else {
 109                 mode = MODE_BOTH;
 110             }
 111 
 112             if (mode != MODE_BOTH) {
 113                 // we have to consume first flag argument.
 114                 String[] newArgs = new String[args.length - 1];
 115                 for (int i = 0; i < newArgs.length; i++) {
 116                     newArgs[i] = args[i + 1];
 117                 }
 118                 args = newArgs;
 119             }
 120             break;
 121         }
 122 
 123         default:
 124             new JInfo(mode).usage();
 125         }
 126 
 127         JInfo jinfo = new JInfo(mode);
 128         jinfo.start(args);
 129         jinfo.stop();
 130     }
 131 
 132     private void printVMFlags() {
 133         String str = Arguments.getJVMFlags();
 134         if (str != null) {
 135             System.out.println(str);
 136         }
 137         str = Arguments.getJVMArgs();
 138         if (str != null) {
 139             System.out.println(str);
 140         }
 141     }
 142 
 143     private int mode;
 144 }