/**
   * Starts the debugger. The method stops the current debugging (if any) and takes information from
   * the provided info (containing the class to start and arguments to pass it and name of class to
   * stop debugging in) and starts new debugging session.
   *
   * @param info debugger info about class to start
   * @exception DebuggerException if an error occures during the start of the debugger
   */
  public void startDebugger(DebuggerInfo info) throws DebuggerException {
    debuggerInfo = info;
    if (virtualMachine != null) finishDebugger();

    stopOnMain = info.getStopClassName() != null;
    mainClassName =
        info
            .getClassName(); // S ystem.out.println ("JPDADebugger stop on " + info.getStopClassName
                             // ()); // NOI18N

    // open output window ...
    super.startDebugger(info);

    // stop on main
    if (stopOnMain) {
      try {
        String stopClassName = debuggerInfo.getStopClassName();
        AbstractDebugger d = (AbstractDebugger) TopManager.getDefault().getDebugger();
        breakpointMain = new CoreBreakpoint[stopMethodNames.length];
        for (int x = 0; x < breakpointMain.length; x++) {
          breakpointMain[x] = (CoreBreakpoint) d.createBreakpoint(true);
          breakpointMain[x].setClassName(""); // NOI18N
          breakpointMain[x].setMethodName(stopMethodNames[x]); // NOI18N
          CoreBreakpoint.Action[] a = breakpointMain[x].getActions();
          int i, ii = a.length;
          for (i = 0; i < ii; i++)
            if (a[i] instanceof PrintAction) {
              ((PrintAction) a[i]).setPrintText(bundle.getString("CTL_Stop_On_Main_print_text"));
            }
          breakpointMain[x].setClassName(stopClassName);
        }

        addPropertyChangeListener(
            new PropertyChangeListener() {
              public void propertyChange(PropertyChangeEvent ev) {
                if (ev.getPropertyName().equals(PROP_STATE)) {
                  if ((((Integer) ev.getNewValue()).intValue() == DEBUGGER_STOPPED)
                      || (((Integer) ev.getNewValue()).intValue() == DEBUGGER_NOT_RUNNING)) {
                    if (breakpointMain != null) {
                      for (int x = 0; x < breakpointMain.length; x++) breakpointMain[x].remove();
                      breakpointMain = null;
                    }
                    removePropertyChangeListener(this);
                  }
                }
              }
            });

      } catch (DebuggerException e) {
        e.printStackTrace();
      }
    }

    // start & init remote debugger ............................................
    boolean launch = false;
    if (info instanceof ReconnectDebuggerInfo) {
      virtualMachine = reconnect((ReconnectDebuggerInfo) info);
    } else if (info instanceof RemoteDebuggerInfo) {
      virtualMachine = connect((RemoteDebuggerInfo) info);
    } else {
      virtualMachine = launch(info);
      process = virtualMachine.process();
      showOutput(process, STD_OUT, STD_OUT);
      connectInput(process);
      launch = true;
    }
    requestManager = virtualMachine.eventRequestManager();
    operator =
        new Operator(
            virtualMachine,
            launch
                ? new Runnable() {
                  public void run() {
                    startDebugger();
                  }
                }
                : null,
            new Runnable() {
              public void run() {
                try {
                  finishDebugger();
                } catch (DebuggerException e) {
                }
              }
            });
    operator.start();
    if (!launch) startDebugger();
  }
Example #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.");
  }