changeset 0:a7cd8a8a01f1

Add dynamic agent attacher.
author Yasumasa Suenaga <suenaga.yasumasa@lab.ntt.co.jp>
date Wed, 10 Apr 2013 16:42:00 +0900
parents
children 1267223a0004
files AgentAttacher/AgentAttacher.java
diffstat 1 files changed, 220 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AgentAttacher/AgentAttacher.java	Wed Apr 10 16:42:00 2013 +0900
@@ -0,0 +1,220 @@
+/*
+ * AgentAttacher.java
+ * Created on 2013/3/19
+ *
+ * Copyright (C) 2011-2013 Nippon Telegraph and Telephone Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+import java.lang.String;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.BufferedReader;
+import java.io.File;
+import java.util.List;
+
+import com.sun.tools.attach.AgentInitializationException;
+import com.sun.tools.attach.AgentLoadException;
+import com.sun.tools.attach.AttachNotSupportedException;
+import com.sun.tools.attach.VirtualMachine;
+import com.sun.tools.attach.VirtualMachineDescriptor;
+
+class AgentAttacher
+{
+	
+	static public void printAttachList(List<VirtualMachineDescriptor> vmlist)
+	{
+		/* Print vm list. */
+		System.out.println("Num Process-Info");
+		for(int i = 0, j = vmlist.size(); i < j; i++)
+		{
+			VirtualMachineDescriptor vmd = vmlist.get(i);
+			System.out.printf("%3d %s%n", i, vmd.displayName());
+		}
+	}
+	
+	static public boolean attachProcess(VirtualMachineDescriptor vmd, String agentPath, String options)
+	{
+		try
+		{
+			/* attach to JVM. */
+			VirtualMachine vm = VirtualMachine.attach(vmd);
+			
+			if (vm == null)
+			{
+				System.err.println("Not found virtual-machine...");
+				return false;
+			}
+			
+			/* load agent to JVM. */
+			if (options.length() == 0)
+			{
+				vm.loadAgentPath(agentPath);
+			}
+			else
+			{
+				vm.loadAgentPath(agentPath, options);
+			}
+			
+			/* detach JVM. */
+			vm.detach();
+		}
+		catch (SecurityException e)
+		{
+			System.err.println("Attach is deny.");
+			System.err.println("Please check security-manager in Java application.");
+			
+			return false;
+		}
+		catch (AttachNotSupportedException e)
+		{
+			System.err.println("Attach is not supported by JavaVM.");
+			System.err.println("Or no respones attach-request by JavaVM.");
+			
+			return false;
+		}
+		catch (AgentLoadException e)
+		{
+			System.err.println("Failure agent-load.");
+			System.err.println("Please confirm agent-load-method exists. e.g. Agent_OnAttach.");
+			System.err.println("And confirm required shared-library exists.");
+			
+			return false;
+		}
+		catch (AgentInitializationException e)
+		{
+			System.err.println("Agent failure in initailization.");
+			System.err.println("return code:" + Integer.toString(e.returnValue()));
+			System.err.println("Please check agent library's log.");
+			
+			return false;
+		}
+		catch (IOException e)
+		{
+			/* Unknown exception. */
+			e.printStackTrace();
+			return false;
+		}
+		
+		return true;
+	}
+	
+	static public void main(String[] args)
+	{
+		/* Check arguments. */
+		if (args.length == 1 && (args[0].equals("--help") || args[0].equals("-h")))
+		{
+			System.out.println("agentattacher agent-full-path [agent-options]");
+			System.out.println("agentattacher --help|-h");
+			
+			return;
+		}
+		else if (args.length <= 0)
+		{
+			System.out.println("please set commandline argment...");
+			
+			return;
+		}
+		else if (args.length >= 3)
+		{
+			System.out.println("commandline argment is too large...");
+			return;
+		}
+		
+		/*
+		 * args.length == 1
+		 *  agentpath : args[0]
+		 *  agent opt : ""
+		 *  
+		 *  like as below
+		 *  # java -agentpath:args[0] ~java app~
+		 * 
+		 * args.length == 2
+		 *  agentpath : args[0]
+		 *  agent opt : args[1]
+		 *  
+		 *  like as below
+		 *  # java -agentpath:args[0]=args[1] ~java app~
+		 */
+		
+		/* Is exist agent file. */
+		File agentFile = new File(args[0]);
+		if (!agentFile.exists())
+		{
+			System.out.println("agent is no exist...");
+			return;
+		}
+		
+		/* Copy strings from arguments. */
+		String agent_path = args[0];
+		String options = "";
+		if (args.length == 2)
+		{
+			options = args[1];
+		}
+		
+		/* Get vm list. */
+		List<VirtualMachineDescriptor> vmlist = VirtualMachine.list();
+		if (vmlist == null)
+		{
+			System.out.println("Not found attachable process.");
+			return;
+		}
+		
+		/* Print attachable java application list. */
+		printAttachList(vmlist);
+		
+		/* Get target process number. */
+		System.out.print("Input attach target process-number:");
+		BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
+		
+		/* Parse Input integer. */
+		int num;
+		try
+		{
+			num = Integer.parseInt(bReader.readLine());
+		}
+		catch (NumberFormatException e)
+		{
+			num = -1;
+		}
+		catch (IOException e)
+		{
+			num = -1;
+		}
+		
+		/* Check input number. */
+		if (num < 0 || num >= vmlist.size())
+		{
+			System.out.println("Illegal Number");
+			System.out.println("Attacher is terminating now...");
+		}
+		else
+		{
+			/* Attach process. */
+			if (!attachProcess(vmlist.get(num), agent_path, options))
+			{
+				System.out.println("Attacher failed.");
+			}
+			else
+			{
+				System.out.println("Attacher is successed.");
+			}
+		}
+	}
+}
+