private VirtualMachine connect(String bndlPrefix, AttachingConnector connector, Map args)
      throws DebuggerException {
    if (bndlPrefix != null) {
      if (connector.transport().name().equals("dt_shmem")) {
        Argument a = (Argument) args.get("name");
        if (a == null) println(bundle.getString(bndlPrefix + "_shmem_noargs"), ERR_OUT);
        else
          println(
              new MessageFormat(bundle.getString(bndlPrefix + "_shmem"))
                  .format(new Object[] {a.value()}),
              ERR_OUT);
      } else if (connector.transport().name().equals("dt_socket")) {
        Argument name = (Argument) args.get("hostname");
        Argument port = (Argument) args.get("port");
        if ((name == null) || (port == null))
          println(bundle.getString(bndlPrefix + "_socket_noargs"), ERR_OUT);
        else
          println(
              new MessageFormat(bundle.getString(bndlPrefix + "_socket"))
                  .format(new Object[] {name.value(), port.value()}),
              ERR_OUT);
      } else println(bundle.getString(bndlPrefix), ERR_OUT);
    }

    // launch VM
    try { // S ystem.out.println ("attach to:" + ac + " : " + password); // NOI18N
      return connector.attach(args);
    } catch (Exception e) {
      finishDebugger();
      throw new DebuggerException(
          new MessageFormat(bundle.getString("EXC_While_connecting_to_debuggee"))
              .format(new Object[] {e.toString()}),
          e);
    }
  }
  private VirtualMachine launch(DebuggerInfo info) throws DebuggerException {
    // create process & read password for local debugging

    // create main class & arguments ...............................................
    StringBuffer sb = new StringBuffer();
    sb.append(mainClassName);
    String[] infoArgs = info.getArguments();
    int i, k = infoArgs.length;
    for (i = 0; i < k; i++) sb.append(" \"").append(infoArgs[i]).append('"'); // NOI18N
    String main = new String(sb);

    // create connector ..............................................................
    VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
    java.util.List lcs = vmm.launchingConnectors();
    k = lcs.size();
    for (i = 0; i < k; i++)
      if (((LaunchingConnector) lcs.get(i)).name().indexOf("RawCommandLineLaunch") >= 0) // NOI18N
      break;
    if (i == k) {
      finishDebugger();
      throw new DebuggerException(
          new MessageFormat(bundle.getString("EXC_Cannot_find_launcher"))
              .format(
                  new Object[] {
                    "RawCommandLineLaunch" // NOI18N
                  }));
    }
    LaunchingConnector lc = (LaunchingConnector) lcs.get(i);
    String transport = lc.transport().name();

    // create commandLine & NbProcessDescriptor ..............................
    NbProcessDescriptor debugerProcess;
    if (info instanceof ProcessDebuggerInfo)
      debugerProcess = ((ProcessDebuggerInfo) info).getDebuggerProcess();
    else debugerProcess = ProcessDebuggerType.DEFAULT_DEBUGGER_PROCESS;

    // generate password
    String password;
    if (transport.equals("dt_shmem")) { // NOI18N
      connector = getAttachingConnectorFor("dt_shmem");
      password = generatePassword();
      args = connector.defaultArguments();
      ((Argument) args.get("name")).setValue(password);
    } else {
      try {
        java.net.ServerSocket ss = new java.net.ServerSocket(0);
        password = "" + ss.getLocalPort(); // NOI18N
        ss.close();
      } catch (java.io.IOException e) {
        finishDebugger();
        throw new DebuggerException(
            new MessageFormat(bundle.getString("EXC_Cannot_find_empty_local_port"))
                .format(new Object[] {e.toString()}));
      }
      connector = getAttachingConnectorFor("dt_socket");
      args = connector.defaultArguments();
      ((Argument) args.get("port")).setValue(password);
    }
    HashMap map =
        Utils.processDebuggerInfo(
            info,
            "-Xdebug -Xnoagent -Xrunjdwp:transport="
                + // NOI18N
                transport
                + ",address="
                + // NOI18N
                password
                + ",suspend=y ", // NOI18N
            main);
    MapFormat format = new MapFormat(map);
    String commandLine =
        format.format(
            debugerProcess.getProcessName()
                + " "
                + // NOI18N
                debugerProcess.getArguments());
    println(commandLine, ERR_OUT);
    /*
    We mus wait on process start to connect...
    try {
      process = debugerProcess.exec (format);
    } catch (java.io.IOException exc) {
      finishDebugger ();
      throw new DebuggerException (
        new MessageFormat (bundle.getString ("EXC_While_create_debuggee")).
          format (new Object[] {
            debugerProcess.getProcessName (),
            exc.toString ()
          }),
        exc
      );
    }
    return connect (
      null,
      connector,
      args
    );*/

    /*      S ystem.out.println ("attaching: ");
    Utils.showConnectors (vmm.attachingConnectors ());

    S ystem.out.println ("launching: ");
    Utils.showConnectors (vmm.launchingConnectors ());

    S ystem.out.println ("listening: ");
    Utils.showConnectors (vmm.listeningConnectors ());*/

    // set debugger-start arguments
    Map params = lc.defaultArguments();
    ((Argument) params.get("command"))
        .setValue( // NOI18N
            commandLine);
    ((Argument) params.get("address"))
        .setValue( // NOI18N
            password);

    // launch VM
    try {
      return lc.launch(params);
    } catch (VMStartException exc) {
      showOutput(process = exc.process(), ERR_OUT, ERR_OUT);
      finishDebugger();
      throw new DebuggerException(
          new MessageFormat(bundle.getString("EXC_While_create_debuggee"))
              .format(
                  new Object[] {format.format(debugerProcess.getProcessName()), exc.toString()}),
          exc);
    } catch (Exception exc) {
      finishDebugger();
      throw new DebuggerException(
          new MessageFormat(bundle.getString("EXC_While_create_debuggee"))
              .format(
                  new Object[] {format.format(debugerProcess.getProcessName()), exc.toString()}),
          exc);
    }
  }
Exemplo n.º 3
0
  public void breakpointReached(BreakpointEvent event) {
    String origMethodName = event.location().method().name();
    String methodName = origMethodName.substring(2);
    ThreadReference tr = event.thread();

    if (vm().canForceEarlyReturn()) {

      try {

        if ("bytef".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("ebyteValue");
          ByteValue theValue = (ByteValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
          /*
           * See what happens if we access the stack after the force
           * and before the resume.  Disabling this since spec says
           * the stack is undefined.  This type of code can be used to
           * pursue just what that means.
           *
           * StackFrame sf = tr.frame(0);
           * List<Value> ll = sf.getArgumentValues();
           * for (Value vv: ll) {
           *     System.out.println("vv = " + vv);
           * }
           */
        } else if ("charf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("echarValue");
          CharValue theValue = (CharValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("doublef".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("edoubleValue");
          DoubleValue theValue = (DoubleValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("floatf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("efloatValue");
          FloatValue theValue = (FloatValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("intf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eintValue");
          IntegerValue theValue = (IntegerValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("longf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("elongValue");
          LongValue theValue = (LongValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("shortf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eshortValue");
          ShortValue theValue = (ShortValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("booleanf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("ebooleanValue");
          BooleanValue theValue = (BooleanValue) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("stringf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("estringValue");
          StringReference theValue = (StringReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("classf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eclassValue");
          ClassObjectReference theValue =
              (ClassObjectReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("classLoaderf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eclassLoaderValue");
          ClassLoaderReference theValue =
              (ClassLoaderReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("threadf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("ethreadValue");
          ThreadReference theValue = (ThreadReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("threadGroupf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("ethreadGroupValue");
          ThreadGroupReference theValue =
              (ThreadGroupReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("intArrayf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eintArrayValue");
          ArrayReference theValue = (ArrayReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("nullObjectf".equals(methodName)) {
          tr.forceEarlyReturn(null);
        } else if ("objectf".equals(methodName)) {
          Field theValueField = targetClass.fieldByName("eobjectValue");
          ObjectReference theValue = (ObjectReference) targetClass.getValue(theValueField);
          tr.forceEarlyReturn(theValue);
        } else if ("voidf".equals(methodName)) {
          VoidValue theValue = vm().mirrorOfVoid();
          tr.forceEarlyReturn(theValue);
        } else {
          failure("failure: Unknown methodName: " + origMethodName);
        }

      } catch (Exception ex) {
        failure("failure: " + ex.toString());
        ex.printStackTrace();
      }
    } else {
      System.out.println("Cannot force early return for method: " + origMethodName);
    }
  }