예제 #1
0
 private void usage() {
   System.out.println("usage: java " + getClass().getName() + " <pid> [server id]");
   System.out.println("   or: java " + getClass().getName() + " <executable> <core> [server id]");
   System.out.println("\"pid\" must be the process ID of a HotSpot process.");
   System.out.println("If reading a core file, \"executable\" must (currently) be the");
   System.out.println("full path name to the precise java executable which generated");
   System.out.println("the core file (not, on Solaris, the \"java\" wrapper script in");
   System.out.println("the \"bin\" subdirectory of the JDK.)");
   System.out.println("The \"server id\" is a unique name for a specific remote debuggee.");
   System.exit(1);
 }
예제 #2
0
  private void run(String[] args) {
    if ((args.length < 1) || (args.length > 3)) {
      usage();
    }

    // Attempt to handle "-h" or "-help"
    if (args[0].startsWith("-")) {
      usage();
    }

    int pid = 0;
    boolean usePid = false;
    String coreFileName = null;
    // FIXME: would be nice to pick this up from the core file
    // somehow, but that doesn't look possible. Should at least figure
    // it out from a path to the JDK.
    String javaExecutableName = null;
    String serverID = null;

    switch (args.length) {
      case 1:
        try {
          pid = Integer.parseInt(args[0]);
          usePid = true;
        } catch (NumberFormatException e) {
          usage();
        }
        break;

      case 2:
        // either we have pid and server id or exec file and core file
        try {
          pid = Integer.parseInt(args[0]);
          usePid = true;
          serverID = args[1];
        } catch (NumberFormatException e) {
          pid = -1;
          usePid = false;
          javaExecutableName = args[0];
          coreFileName = args[1];
        }
        break;

      case 3:
        javaExecutableName = args[0];
        coreFileName = args[1];
        serverID = args[2];
        break;

      default:
        // should not happend, taken care already.
        break;
    }

    final HotSpotAgent agent = new HotSpotAgent();
    try {
      if (usePid) {
        System.err.println(
            "Attaching to process ID " + pid + " and starting RMI services, please wait...");
        agent.startServer(pid, serverID);
      } else {
        System.err.println(
            "Attaching to core "
                + coreFileName
                + " from executable "
                + javaExecutableName
                + " and starting RMI services, please wait...");
        agent.startServer(javaExecutableName, coreFileName, serverID);
      }
    } catch (DebuggerException e) {
      if (usePid) {
        System.err.print("Error attaching to process or starting server: ");
      } else {
        System.err.print("Error attaching to core file or starting server: ");
      }
      e.printStackTrace();
      System.exit(1);
    }

    // shutdown hook to clean-up the server in case of forced exit.
    Runtime.getRuntime()
        .addShutdownHook(
            new java.lang.Thread(
                new Runnable() {
                  public void run() {
                    agent.shutdownServer();
                  }
                }));
    System.err.println("Debugger attached and RMI services started.");
  }