Package com.sun.tools.attach

Examples of com.sun.tools.attach.VirtualMachine



            File bytemanJar = new File(bytemanLib, BytemanConfiguration.BYTEMAN_JAR);
            GenerateScriptUtil.copy(bytemanInputJar, new FileOutputStream(bytemanJar));

            VirtualMachine vm = VirtualMachine.attach(pid);
            String agentProperties = config.agentProperties();
            vm.loadAgent(bytemanJar.getAbsolutePath(), "listener:true,port:" + config.clientAgentPort() + (agentProperties != null ? ",prop:" +  agentProperties:""));
            vm.detach();
        }
        catch (IOException e)
        {
            throw new RuntimeException("Could not write byteman.jar to disk", e);
        }
View Full Code Here


            if (pid < 0) {
                logger.warning(Strings.get("invalid.pid"));
                return false;
            }

            VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
            String ir = System.getProperty(INSTALL_ROOT_PROPERTY);
            File dir = new File(ir, "lib" + File.separator + "monitor");

            if (!dir.isDirectory()) {
                logger.warning(Strings.get("missing.agent.jar.dir", dir));
                return false;
            }

            File agentJar = new File(dir, "flashlight-agent.jar");

            if (!agentJar.isFile()) {
                logger.log(Level.WARNING, Strings.get("missing.agent.jar", dir));
                return false;
            }

            vm.loadAgent(SmartFile.sanitize(agentJar.getPath()), options);
            isAttached = true;
        }
        catch (Throwable t) {
            logger.warning(Strings.get("attach.agent.exception", t.getMessage()));
            isAttached = false;
View Full Code Here

        int port = server.bind();
        log.log(Level.INFO, "Callback server set on port " + port);
        sb.append(port);
        String options = sb.toString();
        Integer pid = pids.get(0);
        final VirtualMachine vm = VirtualMachine.attach("" + pid);
        log.log(Level.INFO, "Loading agent with command " + options + " as agent " + agentFile.getCanonicalPath());
        vm.loadAgent(agentFile.getCanonicalPath(), options);
        server.accept();
        shell = server.getShell();
        closeable.add(new Closeable() {
          public void close() throws IOException {
            vm.detach();
          }
        });
      } else {
        for (Integer pid : pids) {
          log.log(Level.INFO, "Attaching to remote process " + pid);
          VirtualMachine vm = VirtualMachine.attach("" + pid);
          String options = sb.toString();
          log.log(Level.INFO, "Loading agent with command " + options + " as agent " + agentFile.getCanonicalPath());
          vm.loadAgent(agentFile.getCanonicalPath(), options);
        }
        shell = null;
      }
    } else {
      final Bootstrap bootstrap = new Bootstrap(
View Full Code Here

            List<Map<String, Object>> content = (List<Map<String, Object>>) o.get("Content");
            return content;
    }

    private static String loadManagementAgentAndGetAddress(int vmid) throws IOException {
        VirtualMachine vm = null;
        String name = String.valueOf(vmid);
        try {
            vm = VirtualMachine.attach(name);
        } catch (AttachNotSupportedException x) {
            throw new IOException(x.getMessage(), x);
        }

        String home = vm.getSystemProperties().getProperty("java.home");

        // Normally in ${java.home}/jre/lib/management-agent.jar but might
        // be in ${java.home}/lib in build environments.

        String agent = home + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
        File f = new File(agent);
        if (!f.exists()) {
            agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
            f = new File(agent);
            if (!f.exists()) {
                throw new IOException("Management agent not found");
            }
        }

        agent = f.getCanonicalPath();
        try {
            vm.loadAgent(agent, "com.sun.management.jmxremote");
        } catch (AgentLoadException x) {
            throw new IOException(x.getMessage(), x);
        } catch (AgentInitializationException x) {
            throw new IOException(x.getMessage(), x);
        }

        // get the connector address
        Properties agentProps = vm.getAgentProperties();
        String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        vm.detach();

        return address;
    }
View Full Code Here

      if (i != 0) {
        agentArgs.append(" ");
      }
      agentArgs.append(stagerArgs[i]);
    }
    final VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgent(agentPath, agentArgs.toString());
    vm.detach();
    loader.handleAfter(loader.stageHandler.consoleErr, null);
  }
View Full Code Here

            List<Map<String, Object>> content = (List<Map<String, Object>>) o.get("Content");
            return content;
    }

    private static String loadManagementAgentAndGetAddress(int vmid) throws IOException {
        VirtualMachine vm = null;
        String name = String.valueOf(vmid);
        try {
            vm = VirtualMachine.attach(name);
        } catch (AttachNotSupportedException x) {
            throw new IOException(x.getMessage(), x);
        }

        String home = vm.getSystemProperties().getProperty("java.home");

        // Normally in ${java.home}/jre/lib/management-agent.jar but might
        // be in ${java.home}/lib in build environments.

        String agent = home + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
        File f = new File(agent);
        if (!f.exists()) {
            agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
            f = new File(agent);
            if (!f.exists()) {
                throw new IOException("Management agent not found");
            }
        }

        agent = f.getCanonicalPath();
        try {
            vm.loadAgent(agent, "com.sun.management.jmxremote");
        } catch (AgentLoadException x) {
            throw new IOException(x.getMessage(), x);
        } catch (AgentInitializationException x) {
            throw new IOException(x.getMessage(), x);
        }

        // get the connector address
        Properties agentProps = vm.getAgentProperties();
        String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        vm.detach();

        return address;
    }
View Full Code Here

            throw new AssertionError(e);
        }
    }

    private static JMXServiceURL getLocalConnectorAddress(String id, boolean startAgent) {
        VirtualMachine vm = null;
        try {
            vm = VirtualMachine.attach(id);
            String connectorAddr = vm.getAgentProperties().getProperty(PROP_LOCAL_CONNECTOR_ADDRESS);
            if (connectorAddr == null && startAgent) {
                final String agent = Paths.get(vm.getSystemProperties().getProperty(PROP_JAVA_HOME)).resolve(MANAGEMENT_AGENT).toString();
                vm.loadAgent(agent);
                connectorAddr = vm.getAgentProperties().getProperty(PROP_LOCAL_CONNECTOR_ADDRESS);
            }
            final JMXServiceURL url = connectorAddr != null ? new JMXServiceURL(connectorAddr) : null;
            vm.detach();
            return url;
        } catch (AttachNotSupportedException e) {
            throw new UnsupportedOperationException(e);
        } catch (Exception e) {
            try {
                if (vm != null)
                    vm.detach();
            } catch (IOException ex) {
                e.addSuppressed(ex);
            }
            throw e instanceof RuntimeException ? (RuntimeException)e : new RuntimeException(e);
        }
View Full Code Here

            List<Map<String, Object>> content = (List<Map<String, Object>>) o.get("Content");
            return content;
    }

    private static String loadManagementAgentAndGetAddress(int vmid) throws IOException {
        VirtualMachine vm = null;
        String name = String.valueOf(vmid);
        try {
            vm = VirtualMachine.attach(name);
        } catch (AttachNotSupportedException x) {
            throw new IOException(x.getMessage(), x);
        }

        String home = vm.getSystemProperties().getProperty("java.home");

        // Normally in ${java.home}/jre/lib/management-agent.jar but might
        // be in ${java.home}/lib in build environments.

        String agent = home + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
        File f = new File(agent);
        if (!f.exists()) {
            agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
            f = new File(agent);
            if (!f.exists()) {
                throw new IOException("Management agent not found");
            }
        }

        agent = f.getCanonicalPath();
        try {
            vm.loadAgent(agent, "com.sun.management.jmxremote");
        } catch (AgentLoadException x) {
            throw new IOException(x.getMessage(), x);
        } catch (AgentInitializationException x) {
            throw new IOException(x.getMessage(), x);
        }

        // get the connector address
        Properties agentProps = vm.getAgentProperties();
        String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        vm.detach();

        return address;
    }
View Full Code Here

        }
    }

    @Nonnull
    protected MBeanServerConnection connectToMbeanServer(@Nonnull String pid) throws IOException {
        VirtualMachine vm;
        try {
            Integer.parseInt(pid);
        } catch (Exception e) {
            logger.warn("Exception parsing PID '{}'", pid, e);
        }
        try {
            vm = VirtualMachine.attach(pid);
        } catch (Exception e) {
            throw new IllegalStateException("Exception attaching VM with PID '" + pid + "'", e);
        }


        logger.trace("VM Agent Properties");
        for (String key : new TreeSet<String>(vm.getAgentProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getAgentProperties().get(key));
        }
        logger.trace("VM System Properties");
        for (String key : new TreeSet<String>(vm.getSystemProperties().stringPropertyNames())) {
            logger.trace("\t {}: {}", key, vm.getSystemProperties().get(key));
        }


        String connectorAddress =
                vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
        if (connectorAddress == null) {
            String agent = vm.getSystemProperties().getProperty(
                    "java.home") + File.separator + "lib" + File.separator +
                    "management-agent.jar";
            try {
                vm.loadAgent(agent);
            } catch (Exception e) {
                throw new IllegalStateException("Exception loading agent " + agent);
            }
            connectorAddress = vm.getAgentProperties().getProperty(
                    "com.sun.management.jmxremote.localConnectorAddress");
        }

        if (connectorAddress == null) {
            throw new IllegalStateException("Could not attach to pid: " + pid + ". No connector available");
View Full Code Here

    private final static String LOCAL_CONNECTOR_ADDRESS_PROP =
        "com.sun.management.jmxremote.localConnectorAddress";
    public static void main(String[] args) throws Exception {
        String pid = args[0]; // pid as a string
        VirtualMachine vm = VirtualMachine.attach(pid);

        String agentPropLocalConnectorAddress = (String)
            vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);

        int vmid = Integer.parseInt(pid);
        String jvmstatLocalConnectorAddress =
            ConnectorAddressLink.importFrom(vmid);

        if (agentPropLocalConnectorAddress == null &&
            jvmstatLocalConnectorAddress == null) {
            // No JMX Connector address so attach to VM, and load
            // management-agent.jar
            startManagementAgent(pid);
            agentPropLocalConnectorAddress = (String)
                vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
            jvmstatLocalConnectorAddress =
                ConnectorAddressLink.importFrom(vmid);
        }

View Full Code Here

TOP

Related Classes of com.sun.tools.attach.VirtualMachine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.