Tabnine Logo
VirtualMachine
Code IndexAdd Tabnine to your IDE (free)

How to use
VirtualMachine
in
com.sun.tools.attach

Best Java code snippets using com.sun.tools.attach.VirtualMachine (Showing top 20 results out of 657)

Refine searchRefine arrow

  • Properties
origin: jmxtrans/jmxtrans

public static JMXServiceURL extractJMXServiceURLFromPid(String pid) throws IOException {
  try {
    VirtualMachine vm = VirtualMachine.attach(pid);
    try {
      String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      if (connectorAddress == null) {
        String agent = vm.getSystemProperties().getProperty("java.home") +
            File.separator + "lib" + File.separator + "management-agent.jar";
        vm.loadAgent(agent);
        connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      }
      return new JMXServiceURL(connectorAddress);
    } finally {
      vm.detach();
    }
  }
  catch(Exception e) {
    throw new IOException(e);
  }
}
origin: patric-r/jvmtop

private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map,
  Map<Integer, LocalVirtualMachine> existingVmMap)
 List<VirtualMachineDescriptor> vms = VirtualMachine.list();
 for (VirtualMachineDescriptor vmd : vms)
    try
     VirtualMachine vm = VirtualMachine.attach(vmd);
     attachable = true;
     Properties agentProps = vm.getAgentProperties();
     address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
     vm.detach();
origin: alibaba/jvm-sandbox

private void attachAgent(final String targetJvmPid,
             final String agentJarPath,
             final String cfg) throws Exception {
  VirtualMachine vmObj = null;
  try {
    vmObj = VirtualMachine.attach(targetJvmPid);
    if (vmObj != null) {
      vmObj.loadAgent(agentJarPath, cfg);
    }
  } finally {
    if (null != vmObj) {
      vmObj.detach();
    }
  }
}
origin: btraceio/btrace

  private static String getJavaHome(BTraceTask btt) {
    try {
      VirtualMachine vm = VirtualMachine.attach(String.valueOf(btt.getPid()));
      return vm.getSystemProperties().getProperty("java.home");
    } catch (AttachNotSupportedException e) {
      LOGGER.log(Level.SEVERE, null, e);
    } catch (IOException e) {
      LOGGER.log(Level.SEVERE, null, e);
    }
    return null;
  }
}
origin: org.gridkit.lab/jvm-attach-api

@SuppressWarnings("unused")
private static String attachManagementAgent(VirtualMachine vm) throws IOException, AgentLoadException, AgentInitializationException
{
   Properties localProperties = vm.getAgentProperties();
   if (localProperties.containsKey("com.sun.management.jmxremote.localConnectorAddress")) {
     return ((String)localProperties.get("com.sun.management.jmxremote.localConnectorAddress"));
   }
  
  String jhome = vm.getSystemProperties().getProperty("java.home");
  Object localObject = jhome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
  File localFile = new File((String)localObject);
  
  if (!(localFile.exists())) {
    localObject = jhome + File.separator + "lib" + File.separator + "management-agent.jar";
     localFile = new File((String)localObject);
    if (!(localFile.exists())) {
      throw new IOException("Management agent not found"); 
    }
  }
    localObject = localFile.getCanonicalPath();         
   vm.loadAgent((String)localObject, "com.sun.management.jmxremote");
   localProperties = vm.getAgentProperties();
   return ((String)localProperties.get("com.sun.management.jmxremote.localConnectorAddress"));
  }
 
origin: crashub/crash

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();
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);
Properties config = new Properties();
for (String property : properties) {
 int index = property.indexOf('=');
 if (index == -1) {
  config.setProperty(property, "");
 } else {
  config.setProperty(property.substring(0, index), property.substring(index + 1));
origin: stackoverflow.com

 List<VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor desc : vms) {
  VirtualMachine vm;
  try {
    vm = VirtualMachine.attach(desc);
  } catch (AttachNotSupportedException e) {
    continue;
  }
  Properties props = vm.getAgentProperties();
  String connectorAddress =
    props.getProperty("com.sun.management.jmxremote.localConnectorAddress");
  if (connectorAddress == null) {
    continue;
  }
  JMXServiceURL url = new JMXServiceURL(connectorAddress);
  JMXConnector connector = JMXConnectorFactory.connect(url);
  try {
    MBeanServerConnection mbeanConn = connector.getMBeanServerConnection();
    Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
    ...
  } finally {
    jmxConnector.close();
  }
}
origin: apache/geode

VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
try {
 Properties agentProps = vm.getAgentProperties();
 connectorAddress = agentProps.getProperty(PROPERTY_LOCAL_CONNECTOR_ADDRESS);
  vm.startLocalManagementAgent();
  agentProps = vm.getAgentProperties();
  connectorAddress = agentProps.getProperty(PROPERTY_LOCAL_CONNECTOR_ADDRESS);
 vm.detach();
origin: btraceio/btrace

@Override
public int getTaskPort(BTraceTask task) {
  VirtualMachine vm = null;
  try {
    vm = VirtualMachine.attach(String.valueOf(task.getPid()));
    String portStr = vm.getSystemProperties().getProperty(PORT_PROPERTY);
    return portStr != null ? Integer.parseInt(portStr) : findFreePort();
  } catch (AttachNotSupportedException ex) {
    Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex);
  } catch (IOException ex) {
    Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex);
  } finally {
    if (vm != null) {
      try {
        vm.detach();
      } catch (IOException e) {
        LOGGER.log(Level.SEVERE, null, e);
      }
    }
  }
  return findFreePort();
}
origin: apache/oozie

for(VirtualMachineDescriptor d : VirtualMachine.list()) {
  String remoteUniqueId = VirtualMachine.attach(d).getSystemProperties().getProperty("processSettings.unique.id");
  if(remoteUniqueId != null && remoteUniqueId.equals(uniqueId))
String agent = virtualMachine.getSystemProperties().getProperty("java.home") +
    File.separator + "lib" + File.separator + "management-agent.jar";
virtualMachine.loadAgent(agent);
final Object portObject = virtualMachine.getAgentProperties().
    get("com.sun.management.jmxremote.localConnectorAddress");
origin: stackoverflow.com

 // attach to target VM
VirtualMachine vm = VirtualMachine.attach("2177");

// get system properties in target VM
Properties props = vm.getSystemProperties();

// construct path to management agent
String home = props.getProperty("java.home");
String agent = home + File.separator + "lib" + File.separator 
  + "your-agent-example.jar";

// load agent into target VM
vm.loadAgent(agent, "com.sun.management.jmxremote.port=5000");

// detach
vm.detach();
origin: DataDog/jmxfetch

private String getJmxUrlForProcessRegex(String processRegex)
    throws com.sun.tools.attach.AttachNotSupportedException, IOException {
  for (com.sun.tools.attach.VirtualMachineDescriptor vmd :
      com.sun.tools.attach.VirtualMachine.list()) {
    if (vmd.displayName().matches(processRegex)) {
      com.sun.tools.attach.VirtualMachine vm =
          com.sun.tools.attach.VirtualMachine.attach(vmd);
      String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      // If jmx agent is not running in VM, load it and return the connector url
      if (connectorAddress == null) {
        loadJmxAgent(vm);
        // agent is started, get the connector address
        connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      }
      return connectorAddress;
    }
  }
  throw new IOException(
      "No match found. Available JVMs can be listed with the `list_jvms` command.");
}
origin: DataDog/jmxfetch

  private void loadJmxAgent(com.sun.tools.attach.VirtualMachine vm) throws IOException {
    String agent =
        vm.getSystemProperties().getProperty("java.home")
            + File.separator
            + "lib"
            + File.separator
            + "management-agent.jar";
    try {
      vm.loadAgent(agent);
    } catch (Exception e) {
      LOGGER.warn("Error initializing JMX agent", e);
    }
  }
}
origin: stackoverflow.com

VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(PID);
 try {
   Properties props = vm.getAgentProperties();
   System.out.println(props.getProperty("sun.jdwp.listenerAddress"));
 } finally {
   vm.detach();
 }
origin: spring-projects/sts4

@Override
protected String getJmxUrl() {
  String address = null;
  try {
    address = withTimeout(() -> vm.getAgentProperties().getProperty(LOCAL_CONNECTOR_ADDRESS));
  } catch (Exception e) {
    //ignore
  }
  if (address==null) {
    try {
      address = withTimeout(() -> vm.startLocalManagementAgent());
    } catch (Exception e) {
      logger.error("Error starting local management agent", e);
    }
  }
  return address;
}
origin: bytemanproject/byteman

private static String getProperty(String id, String property)
{
  VirtualMachine vm = null;
  try {
    vm = VirtualMachine.attach(id);
    String value = (String)vm.getSystemProperties().get(property);
    return value;
  } catch (AttachNotSupportedException e) {
    return null;
  } catch (IOException e) {
    return null;
  } finally {
    if (vm != null) {
      try {
        vm.detach();
      } catch (IOException e) {
        // ignore;
      }
    }
  }
}
origin: patric-r/jvmtop

public static LocalVirtualMachine getDelegateMachine(VirtualMachine vm)
  throws IOException
{
 // privileges.
 boolean attachable = false;
 String address = null;
 String name = String.valueOf(vm.id()); // default display name to pid
 attachable = true;
 Properties agentProps = vm.getAgentProperties();
 address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
 vm.detach();
 return new LocalVirtualMachine(Integer.parseInt(vm.id()), name, attachable,
   address);
}
origin: neo4j/neo4j

vm = VirtualMachine.attach( String.valueOf( pid ) );
Properties agentProps = vm.getAgentProperties();
String address = (String) agentProps.get( "com.sun.management.jmxremote.localConnectorAddress" );
  address = vm.startLocalManagementAgent();
return new LocalVirtualMachine( address, vm.getSystemProperties() );
  vm.detach();
origin: stackoverflow.com

 public static void loadAgent() {
  logger.info("dynamically loading javaagent");
  String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
  int p = nameOfRunningVM.indexOf('@');
  String pid = nameOfRunningVM.substring(0, p);

  try {
    VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgent(jarFilePath, "");
    vm.detach();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
origin: bytemanproject/byteman

    throw new IllegalArgumentException("Install : invalid pid " +id);
  vm = VirtualMachine.attach(Integer.toString(pid));
} else {
  List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
  for (VirtualMachineDescriptor vmd: vmds) {
    String displayName = vmd.displayName();
      vm = VirtualMachine.attach(vmd);
      return;
        vm = VirtualMachine.attach(vmd);
        return;
com.sun.tools.attachVirtualMachine

Javadoc

A Java virtual machine.

A VirtualMachine represents a Java virtual machine to which this Java virtual machine has attached. The Java virtual machine to which it is attached is sometimes called the target virtual machine, or target VM. An application (typically a tool such as a managemet console or profiler) uses a VirtualMachine to load an agent into the target VM. For example, a profiler tool written in the Java Language might attach to a running application and load its profiler agent to profile the running application.

A VirtualMachine is obtained by invoking the #attach(String) method with an identifier that identifies the target virtual machine. The identifier is implementation-dependent but is typically the process identifier (or pid) in environments where each Java virtual machine runs in its own operating system process. Alternatively, a VirtualMachine instance is obtained by invoking the #attach(VirtualMachineDescriptor) method with a com.sun.tools.attach.VirtualMachineDescriptor obtained from the list of virtual machine descriptors returned by the #list method. Once a reference to a virtual machine is obtained, the #loadAgent, #loadAgentLibrary, and #loadAgentPathmethods are used to load agents into target virtual machine. The #loadAgent method is used to load agents that are written in the Java Language and deployed in a java.util.jar.JarFile. (See java.lang.instrument for a detailed description on how these agents are loaded and started). The #loadAgentLibrary and #loadAgentPath methods are used to load agents that are deployed in a dynamic library and make use of the JVM Tools Interface.

In addition to loading agents a VirtualMachine provides read access to the java.lang.System#getProperties() in the target VM. This can be useful in some environments where properties such as java.home, os.name, or os.arch are used to construct the path to agent that will be loaded into the target VM.

The following example demonstrates how VirtualMachine may be used:

 
// attach to target VM 
VirtualMachine vm = VirtualMachine.attach("2177"); 
// get system properties in target VM 
Properties props = vm.getSystemProperties(); 
// construct path to management agent 
String home = props.getProperty("java.home"); 
String agent = home + File.separator + "lib" + File.separator 
+ "management-agent.jar"; 
// load agent into target VM 
vm.loadAgent(agent, "com.sun.management.jmxremote.port=5000"); 
// detach 
vm.detach(); 

In this example we attach to a Java virtual machine that is identified by the process identifier 2177. The system properties from the target VM are then used to construct the path to a management agent which is then loaded into the target VM. Once loaded the client detaches from the target VM.

A VirtualMachine is safe for use by multiple concurrent threads.

Most used methods

  • attach
    Attaches to a Java virtual machine. This method obtains the list of attach providers by invoking
  • loadAgent
  • detach
    Detach from the virtual machine. After detaching from the virtual machine, any further attempt to in
  • getAgentProperties
    Returns the current agent properties in the target virtual machine. The target virtual machine can m
  • getSystemProperties
    Returns the current system properties in the target virtual machine. This method returns the system
  • list
    Return a list of Java virtual machines. This method returns a list of Java com.sun.tools.attach.V
  • startLocalManagementAgent
  • id
    Returns the identifier for this Java virtual machine.
  • provider
    Returns the provider that created this virtual machine.
  • loadAgentLibrary
  • loadAgentPath
  • startManagementAgent
  • loadAgentPath,
  • startManagementAgent

Popular in Java

  • Reactive rest calls using spring rest template
  • addToBackStack (FragmentTransaction)
  • getSystemService (Context)
  • notifyDataSetChanged (ArrayAdapter)
  • Font (java.awt)
    The Font class represents fonts, which are used to render text in a visible way. A font provides the
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • MessageFormat (java.text)
    Produces concatenated messages in language-neutral way. New code should probably use java.util.Forma
  • GregorianCalendar (java.util)
    GregorianCalendar is a concrete subclass of Calendarand provides the standard calendar used by most
  • JCheckBox (javax.swing)
  • JOptionPane (javax.swing)
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now