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