Skip to content

Commit

Permalink
7167: Agent Plugin
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Macdonald <aptmac@openjdk.org>
Co-authored-by: Jessye Coleman-Shapiro <jescolem@openjdk.org>
Co-authored-by: Kangcheng Xu <kxu@openjdk.org>
Reviewed-by: hirt, jkang
  • Loading branch information
4 people committed Jul 7, 2021
1 parent 1d7397e commit 2e8a251
Show file tree
Hide file tree
Showing 73 changed files with 9,122 additions and 2 deletions.
21 changes: 21 additions & 0 deletions application/org.openjdk.jmc.console.agent/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JMC Agent Plugin
Bundle-SymbolicName: org.openjdk.jmc.console.agent;singleton:=true
Bundle-Version: 8.1.0.qualifier
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
org.eclipse.jface.text,
org.eclipse.ui.editors,
org.eclipse.core.filesystem,
org.eclipse.ui.ide,
org.eclipse.core.resources,
org.eclipse.e4.core.contexts,
org.openjdk.jmc.console.ui,
org.openjdk.jmc.flightrecorder.ui,
org.openjdk.jmc.rjmx
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: javax.inject,
org.eclipse.debug.core.sourcelookup.containers
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.openjdk.jmc.console.agent.AgentPlugin
6 changes: 6 additions & 0 deletions application/org.openjdk.jmc.console.agent/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source.. = src/main/java/
output.. = target/classes/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions application/org.openjdk.jmc.console.agent/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2020, 2021 Red Hat Inc. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
The contents of this file are subject to the terms of either the Universal Permissive License
v 1.0 as shown at http://oss.oracle.com/licenses/upl
or the following license:
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<?eclipse version="3.4"?>
<plugin>
<extension point="org.openjdk.jmc.rjmx.actionProvider">
<action
factory="org.openjdk.jmc.console.agent.actions.AgentEditorOpener"
icon="icons/agent.png"
label="Agent Plugin">
</action>
<provider
icon="icons/agent.png"
id="org.openjdk.jmc.console.agent"
description="The JMC Agent Plugin can be used to declaratively add JFR instrumentation to a running program"
label="Agent Plugin"
priority="101"
doubleClickActionIndex="0">
<action
factory="org.openjdk.jmc.console.agent.actions.AgentEditorOpener"
icon="icons/agent.png"
label="Open the JMC Agent Plugin">
</action>
</provider>
</extension>

<extension point="org.eclipse.ui.editors">
<editor
class="org.openjdk.jmc.console.agent.editor.AgentEditor"
default="true"
id="org.openjdk.jmc.console.agent.editor.AgentEditor"
icon="icons/agent.png"
name="Agent Plugin"/>
</extension>

<extension point="org.eclipse.ui.editors">
<editor
id="org.openjdk.jmc.console.agent.tabs.raweditor.RawEditor"
name="Agent XML Editor"
icon="icons/agent.png"
extensions="xml"
class="org.openjdk.jmc.console.agent.tabs.raweditor.RawEditor"
contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor">
</editor>
</extension>

<extension point="org.eclipse.ui.commands">
<command
id="org.openjdk.jmc.console.agent.openPresetManagerHandler"
defaultHandler="org.openjdk.jmc.console.agent.manager.OpenPresetManagerHandler"
name="Open Agent Preset Manager">
</command>
</extension>

<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:window?after=additions">
<command
commandId="org.openjdk.jmc.console.agent.openPresetManagerHandler"
icon="icons/agent.png"
label="Agent Preset Manager"
style="push">
</command>
</menuContribution>
</extension>
</plugin>
77 changes: 77 additions & 0 deletions application/org.openjdk.jmc.console.agent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2020, 2021 Red Hat Inc. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
The contents of this file are subject to the terms of either the Universal Permissive License
v 1.0 as shown at http://oss.oracle.com/licenses/upl
or the following license:
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openjdk.jmc</groupId>
<artifactId>missioncontrol.application</artifactId>
<version>${revision}${changelist}</version>
</parent>

<artifactId>org.openjdk.jmc.console.agent</artifactId>
<version>8.1.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<properties>
<jmc.agent.version>1.0.0-SNAPSHOT</jmc.agent.version>
<java.version>1.8</java.version>

<spotless.config.path>${basedir}/../../configuration/ide/eclipse/formatting/formatting.xml</spotless.config.path>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<extraClasspathElements>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</extraClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021 Red Hat Inc. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The contents of this file are subject to the terms of either the Universal Permissive License
* v 1.0 as shown at http://oss.oracle.com/licenses/upl
*
* or the following license:
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided with
* the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.openjdk.jmc.console.agent;

import org.openjdk.jmc.rjmx.ConnectionException;
import org.openjdk.jmc.rjmx.IConnectionHandle;
import org.openjdk.jmc.rjmx.IConnectionListener;
import org.openjdk.jmc.rjmx.IServerHandle;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;

public final class AgentJmxHelper {
private final static String AGENT_OBJECT_NAME = "org.openjdk.jmc.jfr.agent:type=AgentController"; //$NON-NLS-1$
private final static String DEFINE_EVENT_PROBES = "defineEventProbes"; //$NON-NLS-1$
private final static String RETRIEVE_EVENT_PROBES = "retrieveEventProbes"; //$NON-NLS-1$
private final static String RETRIEVE_CURRENT_TRANSFORMS = "retrieveCurrentTransforms"; //$NON-NLS-1$
private final static String CONNECTION_USAGE = "Agent MBean"; //$NON-NLS-1$

private final IServerHandle serverHandle;
private final IConnectionHandle connectionHandle;
private final MBeanServerConnection mbsc;

private final Set<IConnectionListener> connectionListeners = new HashSet<>();

public AgentJmxHelper(IServerHandle serverHandle) throws ConnectionException {
this.serverHandle = Objects.requireNonNull(serverHandle);
connectionHandle = serverHandle.connect(CONNECTION_USAGE, this::onConnectionChange);
mbsc = connectionHandle.getServiceOrDummy(MBeanServerConnection.class);
}

public IServerHandle getServerHandle() {
return serverHandle;
}

public IConnectionHandle getConnectionHandle() {
return connectionHandle;
}

public MBeanServerConnection getMBeanServerConnection() {
return mbsc;
}

public void addConnectionChangedListener(IConnectionListener connectionListener) {
connectionListeners.add(Objects.requireNonNull(connectionListener));
}

public void removeConnectionChangedListener(IConnectionListener connectionListener) {
connectionListeners.remove(connectionListener);
}

public boolean isLocalJvm() {
return connectionHandle.getServerDescriptor().getJvmInfo() != null;
}

public boolean isMXBeanRegistered() {
try {
return mbsc.isRegistered(new ObjectName(AGENT_OBJECT_NAME));
} catch (MalformedObjectNameException | IOException e) {
AgentPlugin.getDefault().getLogger().log(Level.SEVERE, "Could not check if agent MXBean is registered", e);
}
return false;
}

public String retrieveEventProbes() {
try {
Object result = mbsc.invoke(new ObjectName(AGENT_OBJECT_NAME), RETRIEVE_EVENT_PROBES, new Object[0],
new String[0]);
return result.toString();
} catch (InstanceNotFoundException
| MalformedObjectNameException
| MBeanException
| ReflectionException
| IOException e) {
AgentPlugin.getDefault().getLogger().log(Level.WARNING, "Could not retrieve event probes", e);
}
return null;
}

public CompositeData[] retrieveCurrentTransforms() {
try {
Object result = mbsc.invoke(new ObjectName(AGENT_OBJECT_NAME), RETRIEVE_CURRENT_TRANSFORMS, new Object[0],
new String[0]);
return (CompositeData[]) result;
} catch (InstanceNotFoundException
| MalformedObjectNameException
| MBeanException
| ReflectionException
| IOException e) {
AgentPlugin.getDefault().getLogger().log(Level.WARNING, "Could not retrieve current transforms", e);
}
return null;
}

public void defineEventProbes(String xmlDescription) {
try {
Object[] params = {xmlDescription};
String[] signature = {String.class.getName()};
mbsc.invoke(new ObjectName(AGENT_OBJECT_NAME), DEFINE_EVENT_PROBES, params, signature);
} catch (InstanceNotFoundException
| MalformedObjectNameException
| MBeanException
| ReflectionException
| IOException e) {
AgentPlugin.getDefault().getLogger().log(Level.WARNING, "Could not define event probes: " + xmlDescription,
e);
}
}

public void onConnectionChange(IConnectionHandle connection) {
for (IConnectionListener listener : connectionListeners) {
listener.onConnectionChange(connection);
}
}
}
Loading

0 comments on commit 2e8a251

Please sign in to comment.