1
- /*[INCLUDE-IF Sidecar18-SE ]*/
1
+ /*[INCLUDE-IF JAVA_SPEC_VERSION >= 8 ]*/
2
2
/*******************************************************************************
3
3
* Copyright IBM Corp. and others 2019
4
4
*
20
20
*
21
21
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
22
22
*******************************************************************************/
23
-
24
23
package openj9 .tools .attach .diagnostics .tools ;
25
24
26
25
import java .io .IOException ;
31
30
import com .sun .tools .attach .spi .AttachProvider ;
32
31
33
32
import openj9 .internal .tools .attach .target .DiagnosticUtils ;
33
+ import openj9 .internal .tools .attach .target .IPC ;
34
34
import openj9 .tools .attach .diagnostics .attacher .AttacherDiagnosticsProvider ;
35
35
36
36
/**
40
40
public class Jcmd {
41
41
42
42
@ SuppressWarnings ("nls" )
43
- private static final String HELPTEXT = "Usage : jcmd <vmid> <arguments>%n"
43
+ private static final String HELPTEXT = "Usage : jcmd <vmid | display name | 0 > <arguments>%n"
44
44
+ "%n"
45
45
+ " -J : supply arguments to the Java VM running jcmd%n"
46
46
+ " -l : list JVM processes on the local machine%n"
47
47
+ " -h : print this help message%n"
48
48
+ "%n"
49
- + " <vmid> : Attach API VM ID as shown in jps or other Attach API-based tools%n"
49
+ + " <vmid> : Attach API VM ID as shown in jcmd or other Attach API-based tools%n"
50
+ + " <display name> : this argument is used to match (either fully or partially) the display name as shown in jcmd or other Attach API-based tools%n"
51
+ + " <0> : the jcmd command will be sent to all Java processes detected by this utility%n"
50
52
+ "%n"
51
53
+ " arguments:%n"
52
54
+ " help : print the list of diagnostic commands%n"
@@ -65,47 +67,65 @@ public class Jcmd {
65
67
* @param args Application arguments. See help text for details.
66
68
*/
67
69
public static void main (String [] args ) {
68
- String command = null ;
69
- /* An empty argument list is a request for a list of VMs */
70
+ if ((args .length == 1 ) && Arrays .stream (HELP_OPTIONS ).anyMatch (args [0 ]::equals )) {
71
+ System .out .printf (HELPTEXT );
72
+ return ;
73
+ }
74
+
75
+ List <AttachProvider > providers = AttachProvider .providers ();
76
+ if ((providers == null ) || providers .isEmpty ()) {
77
+ System .err .println ("no attach providers available" ); //$NON-NLS-1$
78
+ return ;
79
+ }
80
+
81
+ AttachProvider openj9Provider = providers .get (0 );
82
+ List <VirtualMachineDescriptor > vmds = openj9Provider .listVirtualMachines ();
83
+ if ((vmds == null ) || vmds .isEmpty ()) {
84
+ System .err .println ("no VMs found" ); //$NON-NLS-1$
85
+ return ;
86
+ }
87
+
88
+ /* An empty argument list is a request for a list of VMs. */
70
89
final String firstArgument = (0 == args .length ) ? "-l" : args [0 ]; //$NON-NLS-1$
71
90
if ("-l" .equals (firstArgument )) { //$NON-NLS-1$
72
- List < AttachProvider > providers = AttachProvider . providers ();
73
- AttachProvider myProvider = null ;
74
- if (! providers . isEmpty ()) {
75
- myProvider = providers . get ( 0 );
91
+ for ( VirtualMachineDescriptor vmd : vmds ) {
92
+ StringBuilder outputBuffer = new StringBuilder ( vmd . id ()) ;
93
+ Util . getTargetInformation ( openj9Provider , vmd , false , false , true , outputBuffer );
94
+ System . out . println ( outputBuffer . toString () );
76
95
}
77
- if (null == myProvider ) {
78
- System .err .println ("no attach providers available" ); //$NON-NLS-1$
79
- } else {
80
- for (VirtualMachineDescriptor vmd : myProvider .listVirtualMachines ()) {
81
- StringBuilder outputBuffer = new StringBuilder (vmd .id ());
82
- Util .getTargetInformation (myProvider , vmd , false , false , true , outputBuffer );
83
- System .out .println (outputBuffer .toString ());
84
- }
85
- }
86
- } else if ((args .length == 1 ) && (null != firstArgument ) && Arrays .stream (HELP_OPTIONS ).anyMatch (firstArgument ::equals )) {
87
- System .out .printf (HELPTEXT );
88
96
} else {
89
- command = DiagnosticUtils .makeJcmdCommand (args , 1 );
97
+ String command = DiagnosticUtils .makeJcmdCommand (args , 1 );
90
98
if (command .isEmpty ()) {
91
99
System .err .printf ("There is no jcmd command.%n" ); //$NON-NLS-1$
92
100
System .out .printf (HELPTEXT );
93
101
} else {
94
- String vmid = firstArgument ;
95
102
AttacherDiagnosticsProvider diagProvider = new AttacherDiagnosticsProvider ();
96
- try {
97
- diagProvider .attach (vmid );
103
+ List <String > vmids = Util .findMatchVMIDs (vmds , firstArgument );
104
+ boolean exceptionThrown = false ;
105
+ for (String vmid : vmids ) {
106
+ if (!vmid .equals (firstArgument )) {
107
+ // skip following if firstArgument is a VMID
108
+ // keep the output compatible with the old behavior (only one VMID accepted)
109
+ System .out .println (vmid + ":" ); //$NON-NLS-1$
110
+ }
98
111
try {
99
- Util .runCommandAndPrintResult (diagProvider , command , command ); // $NON-NLS-1$
100
- } finally {
101
- diagProvider .detach ();
112
+ IPC .logMessage ("attaching vmid = " + vmid ); //$NON-NLS-1$
113
+ diagProvider .attach (vmid );
114
+ try {
115
+ Util .runCommandAndPrintResult (diagProvider , command , command );
116
+ } finally {
117
+ diagProvider .detach ();
118
+ }
119
+ } catch (IOException e ) {
120
+ Util .handleCommandException (vmid , e );
121
+ exceptionThrown = true ;
122
+ // keep iterating the rest of VMIDs
102
123
}
103
- } catch ( IOException e ) {
104
- Util . handleCommandException ( vmid , e );
124
+ }
125
+ if ( exceptionThrown ) {
105
126
System .out .printf (HELPTEXT );
106
127
}
107
128
}
108
129
}
109
130
}
110
-
111
131
}
0 commit comments