agent/src/share/classes/sun/jvm/hotspot/tools/Tool.java

Print this page
rev 5644 : 6626412: jstack using SA prints some info messages into err stream
Reviewed-by:


   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 java.io.PrintStream;
  28 import java.util.Hashtable;
  29 
  30 import sun.jvm.hotspot.*;
  31 import sun.jvm.hotspot.runtime.*;
  32 import sun.jvm.hotspot.debugger.*;

  33 
  34 // generic command line or GUI tool.
  35 // override run & code main as shown below.
  36 
  37 public abstract class Tool implements Runnable {
  38    private HotSpotAgent agent;
  39    private JVMDebugger jvmDebugger;
  40    private int debugeeType;
  41 
  42    // debugeeType is one of constants below
  43    protected static final int DEBUGEE_PID    = 0;
  44    protected static final int DEBUGEE_CORE   = 1;
  45    protected static final int DEBUGEE_REMOTE = 2;
  46 
  47    public Tool() {
  48    }
  49 
  50    public Tool(JVMDebugger d) {
  51       jvmDebugger = d;
  52    }


 130       }
 131    }
 132 
 133    private int start(String[] args) {
 134 
 135       if ((args.length < 1) || (args.length > 2)) {
 136          usage();
 137          return 1;
 138       }
 139 
 140       // Attempt to handle -h or -help or some invalid flag
 141       if (args[0].startsWith("-h")) {
 142           usage();
 143           return 0;
 144       } else if (args[0].startsWith("-")) {
 145           usage();
 146           return 1;
 147       }
 148 
 149       PrintStream err = System.err;

 150 
 151       int pid = 0;
 152       String coreFileName   = null;
 153       String executableName = null;
 154       String remoteServer   = null;
 155 
 156       switch (args.length) {
 157         case 1:
 158            try {
 159               pid = Integer.parseInt(args[0]);
 160               debugeeType = DEBUGEE_PID;
 161            } catch (NumberFormatException e) {
 162               // try remote server
 163               remoteServer = args[0];
 164               debugeeType  = DEBUGEE_REMOTE;
 165            }
 166            break;
 167 
 168         case 2:
 169            executableName = args[0];
 170            coreFileName   = args[1];
 171            debugeeType    = DEBUGEE_CORE;
 172            break;
 173 
 174         default:
 175            usage();
 176            return 1;
 177       }
 178 
 179       agent = new HotSpotAgent();
 180       try {
 181         switch (debugeeType) {
 182           case DEBUGEE_PID:
 183              err.println("Attaching to process ID " + pid + ", please wait...");
 184              agent.attach(pid);
 185              break;
 186 
 187           case DEBUGEE_CORE:
 188              err.println("Attaching to core " + coreFileName +
 189                          " from executable " + executableName + ", please wait...");
 190              agent.attach(executableName, coreFileName);
 191              break;
 192 
 193           case DEBUGEE_REMOTE:
 194              err.println("Attaching to remote server " + remoteServer + ", please wait...");
 195              agent.attach(remoteServer);
 196              break;
 197         }
 198       }
 199       catch (DebuggerException e) {
 200         switch (debugeeType) {
 201           case DEBUGEE_PID:
 202              err.print("Error attaching to process: ");
 203              break;
 204 
 205           case DEBUGEE_CORE:
 206              err.print("Error attaching to core file: ");
 207              break;
 208 
 209           case DEBUGEE_REMOTE:
 210              err.print("Error attaching to remote server: ");
 211              break;
 212         }
 213         if (e.getMessage() != null) {
 214           err.println(e.getMessage());
 215           e.printStackTrace();
 216         }
 217         err.println();
 218         return 1;
 219       }
 220 
 221       err.println("Debugger attached successfully.");
 222       startInternal();
 223       return 0;
 224    }
 225 
 226    // When using an existing JVMDebugger.
 227    public void start() {
 228 
 229       if (jvmDebugger == null) {
 230          throw new RuntimeException("Tool.start() called with no JVMDebugger set.");
 231       }
 232       agent = new HotSpotAgent();
 233       agent.attach(jvmDebugger);
 234       startInternal();
 235    }
 236 
 237    // Remains of the start mechanism, common to both start methods.
 238    private void startInternal() {
 239 
 240       PrintStream err = System.err;
 241       VM vm = VM.getVM();
 242       if (vm.isCore()) {
 243         err.println("Core build detected.");
 244       } else if (vm.isClientCompiler()) {
 245         err.println("Client compiler detected.");
 246       } else if (vm.isServerCompiler()) {
 247         err.println("Server compiler detected.");
 248       } else {
 249         throw new RuntimeException("Fatal error: "
 250             + "should have been able to detect core/C1/C2 build");
 251       }
 252 
 253       String version = vm.getVMRelease();
 254       if (version != null) {
 255         err.print("JVM version is ");
 256         err.println(version);
 257       }
 258 
 259       run();
 260    }
 261 }


   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 java.io.PrintStream;

  28 
  29 import sun.jvm.hotspot.HotSpotAgent;
  30 import sun.jvm.hotspot.debugger.DebuggerException;
  31 import sun.jvm.hotspot.debugger.JVMDebugger;
  32 import sun.jvm.hotspot.runtime.VM;
  33 
  34 // generic command line or GUI tool.
  35 // override run & code main as shown below.
  36 
  37 public abstract class Tool implements Runnable {
  38    private HotSpotAgent agent;
  39    private JVMDebugger jvmDebugger;
  40    private int debugeeType;
  41 
  42    // debugeeType is one of constants below
  43    protected static final int DEBUGEE_PID    = 0;
  44    protected static final int DEBUGEE_CORE   = 1;
  45    protected static final int DEBUGEE_REMOTE = 2;
  46 
  47    public Tool() {
  48    }
  49 
  50    public Tool(JVMDebugger d) {
  51       jvmDebugger = d;
  52    }


 130       }
 131    }
 132 
 133    private int start(String[] args) {
 134 
 135       if ((args.length < 1) || (args.length > 2)) {
 136          usage();
 137          return 1;
 138       }
 139 
 140       // Attempt to handle -h or -help or some invalid flag
 141       if (args[0].startsWith("-h")) {
 142           usage();
 143           return 0;
 144       } else if (args[0].startsWith("-")) {
 145           usage();
 146           return 1;
 147       }
 148 
 149       PrintStream err = System.err;
 150       PrintStream out = System.out;
 151 
 152       int pid = 0;
 153       String coreFileName   = null;
 154       String executableName = null;
 155       String remoteServer   = null;
 156 
 157       switch (args.length) {
 158         case 1:
 159            try {
 160               pid = Integer.parseInt(args[0]);
 161               debugeeType = DEBUGEE_PID;
 162            } catch (NumberFormatException e) {
 163               // try remote server
 164               remoteServer = args[0];
 165               debugeeType  = DEBUGEE_REMOTE;
 166            }
 167            break;
 168 
 169         case 2:
 170            executableName = args[0];
 171            coreFileName   = args[1];
 172            debugeeType    = DEBUGEE_CORE;
 173            break;
 174 
 175         default:
 176            usage();
 177            return 1;
 178       }
 179 
 180       agent = new HotSpotAgent();
 181       try {
 182         switch (debugeeType) {
 183           case DEBUGEE_PID:
 184              out.println("Attaching to process ID " + pid + ", please wait...");
 185              agent.attach(pid);
 186              break;
 187 
 188           case DEBUGEE_CORE:
 189              out.println("Attaching to core " + coreFileName +
 190                          " from executable " + executableName + ", please wait...");
 191              agent.attach(executableName, coreFileName);
 192              break;
 193 
 194           case DEBUGEE_REMOTE:
 195              out.println("Attaching to remote server " + remoteServer + ", please wait...");
 196              agent.attach(remoteServer);
 197              break;
 198         }
 199       }
 200       catch (DebuggerException e) {
 201         switch (debugeeType) {
 202           case DEBUGEE_PID:
 203              err.print("Error attaching to process: ");
 204              break;
 205 
 206           case DEBUGEE_CORE:
 207              err.print("Error attaching to core file: ");
 208              break;
 209 
 210           case DEBUGEE_REMOTE:
 211              err.print("Error attaching to remote server: ");
 212              break;
 213         }
 214         if (e.getMessage() != null) {
 215           err.println(e.getMessage());
 216           e.printStackTrace();
 217         }
 218         err.println();
 219         return 1;
 220       }
 221 
 222       out.println("Debugger attached successfully.");
 223       startInternal();
 224       return 0;
 225    }
 226 
 227    // When using an existing JVMDebugger.
 228    public void start() {
 229 
 230       if (jvmDebugger == null) {
 231          throw new RuntimeException("Tool.start() called with no JVMDebugger set.");
 232       }
 233       agent = new HotSpotAgent();
 234       agent.attach(jvmDebugger);
 235       startInternal();
 236    }
 237 
 238    // Remains of the start mechanism, common to both start methods.
 239    private void startInternal() {
 240 
 241       PrintStream out = System.out;
 242       VM vm = VM.getVM();
 243       if (vm.isCore()) {
 244         out.println("Core build detected.");
 245       } else if (vm.isClientCompiler()) {
 246         out.println("Client compiler detected.");
 247       } else if (vm.isServerCompiler()) {
 248         out.println("Server compiler detected.");
 249       } else {
 250         throw new RuntimeException("Fatal error: "
 251             + "should have been able to detect core/C1/C2 build");
 252       }
 253 
 254       String version = vm.getVMRelease();
 255       if (version != null) {
 256         out.print("JVM version is ");
 257         out.println(version);
 258       }
 259 
 260       run();
 261    }
 262 }