Skip to content

Commit

Permalink
Merge pull request #4650 from pdbain-ibm/attach_jps
Browse files Browse the repository at this point in the history
Implement jps: java process status
  • Loading branch information
pshipton authored Feb 23, 2019
2 parents 988fb06 + 142da72 commit 655bf0d
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 4 deletions.
3 changes: 2 additions & 1 deletion jcl/.classpath
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*******************************************************************************
* Copyright (c) 2017, 2017 IBM Corp. and others
* Copyright (c) 2017, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -29,6 +29,7 @@
<classpathentry excluding="**/module-info.java" kind="src" path="src/java.management/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/jdk.management/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/openj9.cuda/share/classes"/>
<classpathentry excluding="**/module-info.java" kind="src" path="src/openj9.dataaccess/share/classes"/>
Expand Down
11 changes: 11 additions & 0 deletions jcl/jpp_configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<source path="src/java.logging/share/classes"/>
<source path="src/java.management/share/classes"/>
<source path="src/jdk.attach/share/classes"/>
<source path="src/jdk.jcmd/share/classes"/>
<source path="src/openj9.jvm/share/classes"/>
<source path="src/jdk.management/share/classes"/>
<source path="src/openj9.cuda/share/classes" />
Expand All @@ -94,6 +95,7 @@
<source path="src/java.logging/share/classes"/>
<source path="src/java.management/share/classes"/>
<source path="src/jdk.attach/share/classes"/>
<source path="src/jdk.jcmd/share/classes"/>
<source path="src/openj9.jvm/share/classes"/>
<source path="src/jdk.management/share/classes"/>
<source path="src/openj9.cuda/share/classes" />
Expand All @@ -115,6 +117,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -142,6 +145,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -169,6 +173,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -196,6 +201,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -223,6 +229,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -250,6 +257,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -277,6 +285,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -304,6 +313,7 @@
<classpathentry kind="src" path="src/java.base/share/classes"/>
<classpathentry kind="src" path="src/java.management/share/classes"/>
<classpathentry kind="src" path="src/jdk.attach/share/classes"/>
<classpathentry kind="src" path="src/jdk.jcmd/share/classes"/>
<classpathentry kind="src" path="src/openj9.jvm/share/classes"/>
<classpathentry kind="src" path="src/jdk.management/share/classes"/>
<classpathentry kind="src" path="src/openj9.cuda/share/classes"/>
Expand Down Expand Up @@ -335,6 +345,7 @@
<source path="src/java.management/share/classes"/>
<source path="src/jdk.management/share/classes"/>
<source path="src/jdk.attach/share/classes"/>
<source path="src/jdk.jcmd/share/classes"/>
<source path="src/openj9.jvm/share/classes"/>
<source path="src/openj9.dataaccess/share/classes"/>
<source path="src/openj9.sharedclasses/share/classes"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*[INCLUDE-IF Sidecar16]*/
/*******************************************************************************
* Copyright (c) 2009, 2018 IBM Corp. and others
* Copyright (c) 2009, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -35,6 +35,7 @@
import java.security.PrivilegedAction;
import java.util.Objects;
import java.util.Properties;

/*[IF Sidecar19-SE]*/
import jdk.internal.vm.VMSupport;
/*[ELSE] Sidecar19-SE
Expand Down Expand Up @@ -199,7 +200,11 @@ boolean doCommand(InputStream cmdStream, OutputStream respStream) {
+ " " + attachError); //$NON-NLS-1$
}
} else if (cmd.startsWith(Command.GET_SYSTEM_PROPERTIES)) {
replyWithProperties(com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties());
Properties internalProperties = com.ibm.oti.vm.VM.getVMLangAccess().internalGetProperties();
String argumentString = String.join(" ", com.ibm.oti.vm.VM.getVMArgs()); //$NON-NLS-1$
Properties newProperties = (Properties) internalProperties.clone();
newProperties.put("sun.jvm.args", argumentString); //$NON-NLS-1$
replyWithProperties(newProperties);
} else if (cmd.startsWith(Command.GET_AGENT_PROPERTIES)) {
replyWithProperties(AttachHandler.getAgentProperties());
} else if (cmd.startsWith(Command.START_LOCAL_MANAGEMENT_AGENT)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*[INCLUDE-IF Sidecar18-SE]*/
/*******************************************************************************
* Copyright (c) 2019, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

package openj9.tools.attach.diagnostics;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.spi.AttachProvider;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

/**
* Java Process Status
* A tool for listing Java processes and their information.
*
*/
public class Jps {

private static final String SUN_JAVA_COMMAND = "sun.java.command"; //$NON-NLS-1$
private static final String SUN_JVM_ARGS = "sun.jvm.args"; //$NON-NLS-1$
private static boolean printApplicationArguments;
private static boolean printJvmArguments;
private static boolean noPackageName;
private static boolean vmidOnly;
/**
* Print a list of Java processes and information about them.
* @param args Arguments to the application
*/
public static void main(String[] args) {
int rc = 0;
parseArguments(args);
final List<AttachProvider> providers = AttachProvider.providers();
AttachProvider theProvider = null;
if (0 != providers.size()) {
theProvider = providers.get(0);
}
if (null == theProvider) {
System.err.println("no attach providers available"); //$NON-NLS-1$
rc = 1;
} else {
List<VirtualMachineDescriptor> vmds = theProvider.listVirtualMachines();
for (VirtualMachineDescriptor vmd: vmds) {
StringBuilder outputBuffer = new StringBuilder(vmd.id());
if (!vmidOnly) {
try {
VirtualMachine theVm = theProvider.attachVirtualMachine(vmd);
try {
Properties vmProperties = theVm.getSystemProperties();
String theCommand = vmProperties.getProperty(SUN_JAVA_COMMAND, ""); //$NON-NLS-1$
String parts[] = theCommand.split("\\s+", 2); /* split into at most 2 parts: command and argument string */ //$NON-NLS-1$
if (noPackageName) {
String commandName = parts[0];
int finalDot = commandName.lastIndexOf('.');
parts[0] = commandName.substring(finalDot + 1); /* if the dot is missing, we get the whole string */
}
if (printApplicationArguments) {
for (String p:parts) {
outputBuffer.append(' ');
outputBuffer.append(p);
}
} else if (parts.length > 0) { /* some Java processes do not use the Java launcher */
outputBuffer.append(' ');
outputBuffer.append(parts[0]);
}
if (printJvmArguments) {
String jvmArguments = vmProperties.getProperty(SUN_JVM_ARGS);
if ((null != jvmArguments) && !jvmArguments.isEmpty()) {
outputBuffer.append(' ');
outputBuffer.append(jvmArguments);
}
}
} finally {
theVm.detach();
}
} catch (AttachNotSupportedException | IOException e) {
outputBuffer.append(' ');
outputBuffer.append("<no information available>"); //$NON-NLS-1$
}
}
System.out.println(outputBuffer.toString());
}
}
System.exit(rc);
}

@SuppressWarnings("nls")
private static void parseArguments(String[] args) {
printApplicationArguments = false;
printJvmArguments = false;
noPackageName = true;
vmidOnly = false;
final String HELPTEXT = "jps: Print a list of Java processes and information about them%n"
+ " -J: supply arguments to the Java VM running jps%n"
+ " -l: print the application package name%n"
+ " -q: print only the virtual machine identifiers%n"
+ " -m: print the application arguments%n"
+ " -v: print the Java VM arguments, including those produced automatically%n";
for (String a: args) {
switch (a) {
case "-l":
noPackageName = false;
break;
case "-m":
printApplicationArguments = true;
break;
case "-q":
vmidOnly = true;
break;
case "-v":
printJvmArguments = true;
break;
/* implicitly handle -h and -help via the default case */
default:
System.out.printf(HELPTEXT);
System.exit(1);
break;
}
}
}
}
51 changes: 51 additions & 0 deletions test/functional/Java8andUp/playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,57 @@
</test>
<!-- Attach API tests -->

<test>
<testCaseName>TestJps_SE80</testCaseName>
<command>$(JAVA_COMMAND) $(JVM_OPTIONS) \
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(JDK_HOME)$(D)lib$(D)tools.jar$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
-Dcom.ibm.tools.attach.enable=yes \
-Djdk.attach.allowAttachSelf=true \
-Dcom.ibm.tools.attach.timeout=15000 \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestJps \
-groups $(TEST_GROUP) \
-excludegroups $(DEFAULT_EXCLUDE); \
$(TEST_STATUS)</command>
<levels>
<level>extended</level>
</levels>
<groups>
<group>functional</group>
</groups>
<subsets>
<subset>8</subset>
</subsets>
<impls>
<impl>openj9</impl>
</impls>
</test>

<test>
<testCaseName>TestJps</testCaseName>
<command>$(JAVA_COMMAND) $(JVM_OPTIONS) \
-cp $(Q)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(TEST_RESROOT)$(D)GeneralTest.jar$(Q) \
--add-exports=java.base/com.ibm.tools.attach.target=ALL-UNNAMED \
-Dcom.ibm.tools.attach.enable=yes \
-Djdk.attach.allowAttachSelf=true \
-Dcom.ibm.tools.attach.timeout=15000 \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng.xml$(Q) -testnames TestJps \
-groups $(TEST_GROUP) \
-excludegroups $(DEFAULT_EXCLUDE); \
$(TEST_STATUS)</command>
<levels>
<level>extended</level>
</levels>
<groups>
<group>functional</group>
</groups>
<subsets>
<subset>9+</subset>
</subsets>
<impls>
<impl>openj9</impl>
</impls>
</test>

<test>
<testCaseName>TestFileLocking_SE80</testCaseName>
<command>$(JAVA_COMMAND) $(JVM_OPTIONS) \
Expand Down
Loading

0 comments on commit 655bf0d

Please sign in to comment.