private static AttachingConnector getAttachingConnectorFor(String transport) {
   List acs = Bootstrap.virtualMachineManager().attachingConnectors();
   AttachingConnector ac;
   int i, k = acs.size();
   for (i = 0; i < k; i++)
     if ((ac = (AttachingConnector) acs.get(i)).transport().name().equals(transport)) return ac;
   return null;
 }
Пример #2
0
 private AttachingConnector connector(String connectorName) {
   for (AttachingConnector c : Bootstrap.virtualMachineManager().attachingConnectors()) {
     if (connectorName.equals(c.name())) {
       return c;
     }
   }
   return null;
 }
Пример #3
0
 private Connector findConnector(String name) {
   for (Connector connector : Bootstrap.virtualMachineManager().allConnectors()) {
     if (connector.name().equals(name)) {
       return connector;
     }
   }
   return null;
 }
Пример #4
0
  private AttachingConnector getConnector() {
    VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();

    for (Connector connector : vmManager.attachingConnectors()) {
      if ("com.sun.jdi.SocketAttach".equals(connector.name()))
        return (AttachingConnector) connector;
    }
    throw new IllegalStateException();
  }
Пример #5
0
 /** Attach to a target VM using the specified address and Connector arguments. */
 public VirtualMachine attach(String address, Map args)
     throws IOException, IllegalConnectorArgumentsException {
   String ts = argument(ARG_TIMEOUT, args).value();
   int timeout = 0;
   if (ts.length() > 0) {
     timeout = Integer.decode(ts).intValue();
   }
   Connection connection = transportService.attach(address, timeout, 0);
   return Bootstrap.virtualMachineManager().createVirtualMachine(connection);
 }
Пример #6
0
 /*
  * Find Connector by name
  */
 private static Connector findConnector(String name) {
   List connectors = Bootstrap.virtualMachineManager().allConnectors();
   Iterator iter = connectors.iterator();
   while (iter.hasNext()) {
     Connector connector = (Connector) iter.next();
     if (connector.name().equals(name)) {
       return connector;
     }
   }
   return null;
 }
Пример #7
0
 private VirtualMachine createVirtualMachine(Class virtualMachineImplClass, int pid)
     throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
   java.lang.reflect.Method createByPIDMethod =
       virtualMachineImplClass.getMethod(
           "createVirtualMachineForPID",
           new Class[] {VirtualMachineManager.class, Integer.TYPE, Integer.TYPE});
   return (VirtualMachine)
       createByPIDMethod.invoke(
           null,
           new Object[] {Bootstrap.virtualMachineManager(), new Integer(pid), new Integer(0)});
 }
Пример #8
0
 private static boolean supportsSharedMemory() {
   List connectors = Bootstrap.virtualMachineManager().allConnectors();
   Iterator iter = connectors.iterator();
   while (iter.hasNext()) {
     Connector connector = (Connector) iter.next();
     if (connector.transport() == null) {
       continue;
     }
     if ("dt_shmem".equals(connector.transport().name())) {
       return true;
     }
   }
   return false;
 }
 Connector() {
   VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
   acs = vmm.attachingConnectors();
   setLayout(new GridBagLayout());
   refresh(0);
 }
Пример #10
0
  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);
    }
  }
Пример #11
0
 public VMListener(int port) {
   VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
   this.lc = vmm.listeningConnectors().get(0);
   this.args = lc.defaultArguments();
   args.get("port").setValue(Integer.toString(port));
 }
Пример #12
0
 public static void main(String[] args) throws Exception {
   EarlyReturnTest meee = new EarlyReturnTest(args);
   vmm = Bootstrap.virtualMachineManager();
   meee.startTests();
 }
Пример #13
0
  public static void main(String argv[]) throws MissingResourceException {
    redirectOutMessage();

    String cmdLine = "";
    String javaArgs = "";
    int traceFlags = VirtualMachine.TRACE_NONE;
    boolean launchImmediately = false;
    String connectSpec = null;

    MessageOutput.textResources =
        ResourceBundle.getBundle(
            "com.sun.tools.example.debug.tty.TTYResources", Locale.getDefault());

    for (int i = 0; i < argv.length; i++) {
      String token = argv[i];
      if (token.equals("-dbgtrace")) {
        if ((i == argv.length - 1) || !Character.isDigit(argv[i + 1].charAt(0))) {
          traceFlags = VirtualMachine.TRACE_ALL;
        } else {
          String flagStr = "";
          try {
            flagStr = argv[++i];
            traceFlags = Integer.decode(flagStr).intValue();
          } catch (NumberFormatException nfe) {
            usageError("dbgtrace flag value must be an integer:", flagStr);
            return;
          }
        }
      } else if (token.equals("-X")) {
        usageError("Use java minus X to see");
        return;
      } else if (
      // Standard VM options passed on
      token.equals("-v")
          || token.startsWith("-v:")
          || // -v[:...]
          token.startsWith("-verbose")
          || // -verbose[:...]
          token.startsWith("-D")
          ||
          // -classpath handled below
          // NonStandard options passed on
          token.startsWith("-X")
          ||
          // Old-style options (These should remain in place as long as
          //  the standard VM accepts them)
          token.equals("-noasyncgc")
          || token.equals("-prof")
          || token.equals("-verify")
          || token.equals("-noverify")
          || token.equals("-verifyremote")
          || token.equals("-verbosegc")
          || token.startsWith("-ms")
          || token.startsWith("-mx")
          || token.startsWith("-ss")
          || token.startsWith("-oss")) {

        javaArgs = addArgument(javaArgs, token);
      } else if (token.equals("-tclassic")) {
        usageError("Classic VM no longer supported.");
        return;
      } else if (token.equals("-tclient")) {
        // -client must be the first one
        javaArgs = "-client " + javaArgs;
      } else if (token.equals("-tserver")) {
        // -server must be the first one
        javaArgs = "-server " + javaArgs;
      } else if (token.equals("-sourcepath")) {
        if (i == (argv.length - 1)) {
          usageError("No sourcepath specified.");
          return;
        }
        Env.setSourcePath(argv[++i]);
      } else if (token.equals("-classpath")) {
        if (i == (argv.length - 1)) {
          usageError("No classpath specified.");
          return;
        }
        javaArgs = addArgument(javaArgs, token);
        javaArgs = addArgument(javaArgs, argv[++i]);
      } else if (token.equals("-attach")) {
        if (connectSpec != null) {
          usageError("cannot redefine existing connection", token);
          return;
        }
        if (i == (argv.length - 1)) {
          usageError("No attach address specified.");
          return;
        }
        String address = argv[++i];

        /*
         * -attach is shorthand for one of the reference implementation's
         * attaching connectors. Use the shared memory attach if it's
         * available; otherwise, use sockets. Build a connect
         * specification string based on this decision.
         */
        if (supportsSharedMemory()) {
          connectSpec = "com.sun.jdi.SharedMemoryAttach:name=" + address;
        } else {
          String suboptions = addressToSocketArgs(address);
          connectSpec = "com.sun.jdi.SocketAttach:" + suboptions;
        }
      } else if (token.equals("-listen") || token.equals("-listenany")) {
        if (connectSpec != null) {
          usageError("cannot redefine existing connection", token);
          return;
        }
        String address = null;
        if (token.equals("-listen")) {
          if (i == (argv.length - 1)) {
            usageError("No attach address specified.");
            return;
          }
          address = argv[++i];
        }

        /*
         * -listen[any] is shorthand for one of the reference implementation's
         * listening connectors. Use the shared memory listen if it's
         * available; otherwise, use sockets. Build a connect
         * specification string based on this decision.
         */
        if (supportsSharedMemory()) {
          connectSpec = "com.sun.jdi.SharedMemoryListen:";
          if (address != null) {
            connectSpec += ("name=" + address);
          }
        } else {
          connectSpec = "com.sun.jdi.SocketListen:";
          if (address != null) {
            connectSpec += addressToSocketArgs(address);
          }
        }
      } else if (token.equals("-launch")) {
        launchImmediately = true;
      } else if (token.equals("-listconnectors")) {
        Commands evaluator = new Commands();
        evaluator.commandConnectors(Bootstrap.virtualMachineManager());
        return;
      } else if (token.equals("-connect")) {
        /*
         * -connect allows the user to pick the connector
         * used in bringing up the target VM. This allows
         * use of connectors other than those in the reference
         * implementation.
         */
        if (connectSpec != null) {
          usageError("cannot redefine existing connection", token);
          return;
        }
        if (i == (argv.length - 1)) {
          usageError("No connect specification.");
          return;
        }
        connectSpec = argv[++i];
      } else if (token.equals("-help")) {
        usage();
      } else if (token.equals("-version")) {
        Commands evaluator = new Commands();
        evaluator.commandVersion(progname, Bootstrap.virtualMachineManager());
        System.exit(0);
      } else if (token.startsWith("-")) {
        usageError("invalid option", token);
        return;
      } else {
        // Everything from here is part of the command line
        cmdLine = addArgument("", token);
        for (i++; i < argv.length; i++) {
          cmdLine = addArgument(cmdLine, argv[i]);
        }
        break;
      }
    }

    /*
     * Unless otherwise specified, set the default connect spec.
     */

    /*
     * Here are examples of jdb command lines and how the options
     * are interpreted as arguments to the program being debugged.
     * arg1       arg2
     * ----       ----
     * jdb hello a b       a          b
     * jdb hello "a b"     a b
     * jdb hello a,b       a,b
     * jdb hello a, b      a,         b
     * jdb hello "a, b"    a, b
     * jdb -connect "com.sun.jdi.CommandLineLaunch:main=hello  a,b"   illegal
     * jdb -connect  com.sun.jdi.CommandLineLaunch:main=hello "a,b"   illegal
     * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a,b"'  arg1 = a,b
     * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a b"'  arg1 = a b
     * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello  a b'   arg1 = a  arg2 = b
     * jdb -connect 'com.sun.jdi.CommandLineLaunch:main=hello "a," b' arg1 = a, arg2 = b
     */
    if (connectSpec == null) {
      connectSpec = "com.sun.jdi.CommandLineLaunch:";
    } else if (!connectSpec.endsWith(",") && !connectSpec.endsWith(":")) {
      connectSpec += ","; // (Bug ID 4285874)
    }

    cmdLine = cmdLine.trim();
    javaArgs = javaArgs.trim();

    if (cmdLine.length() > 0) {
      if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) {
        usageError("Cannot specify command line with connector:", connectSpec);
        return;
      }
      connectSpec += "main=" + cmdLine + ",";
    }

    if (javaArgs.length() > 0) {
      if (!connectSpec.startsWith("com.sun.jdi.CommandLineLaunch:")) {
        usageError("Cannot specify target vm arguments with connector:", connectSpec);
        return;
      }
      connectSpec += "options=" + javaArgs + ",";
    }

    try {
      if (!connectSpec.endsWith(",")) {
        connectSpec += ","; // (Bug ID 4285874)
      }
      Env.init(connectSpec, launchImmediately, traceFlags);
      new ThreadDumper();
    } catch (Exception e) {
      MessageOutput.printException("Internal exception:", e);
    }
  }