69 throw new AttachNotSupportedException("Invalid process identifier"); 70 } 71 72 // The tool should be a different VM to the target. This check will 73 // eventually be enforced by the target VM. 74 if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) { 75 throw new IOException("Can not attach to current VM"); 76 } 77 } 78 79 /* 80 * Load agent library 81 * If isAbsolute is true then the agent library is the absolute path 82 * to the library and thus will not be expanded in the target VM. 83 * if isAbsolute is false then the agent library is just a library 84 * name and it will be expended in the target VM. 85 */ 86 private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) 87 throws AgentLoadException, AgentInitializationException, IOException 88 { 89 InputStream in = execute("load", 90 agentLibrary, 91 isAbsolute ? "true" : "false", 92 options); 93 try { 94 int result = readInt(in); 95 if (result != 0) { 96 throw new AgentInitializationException("Agent_OnAttach failed", result); 97 } 98 } finally { 99 in.close(); 100 101 } 102 } 103 104 /* 105 * Load agent library - library name will be expanded in target VM 106 */ 107 public void loadAgentLibrary(String agentLibrary, String options) 108 throws AgentLoadException, AgentInitializationException, IOException 109 { 110 loadAgentLibrary(agentLibrary, false, options); 111 } 112 113 /* 114 * Load agent - absolute path of library provided to target VM 115 */ 116 public void loadAgentPath(String agentLibrary, String options) 117 throws AgentLoadException, AgentInitializationException, IOException 118 { 119 loadAgentLibrary(agentLibrary, true, options); 120 } | 69 throw new AttachNotSupportedException("Invalid process identifier"); 70 } 71 72 // The tool should be a different VM to the target. This check will 73 // eventually be enforced by the target VM. 74 if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) { 75 throw new IOException("Can not attach to current VM"); 76 } 77 } 78 79 /* 80 * Load agent library 81 * If isAbsolute is true then the agent library is the absolute path 82 * to the library and thus will not be expanded in the target VM. 83 * if isAbsolute is false then the agent library is just a library 84 * name and it will be expended in the target VM. 85 */ 86 private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options) 87 throws AgentLoadException, AgentInitializationException, IOException 88 { 89 String msgPrefix = "return code: "; 90 InputStream in = execute("load", 91 agentLibrary, 92 isAbsolute ? "true" : "false", 93 options); 94 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 95 try (reader) { 96 String result = reader.readLine(); 97 if (result == null) { 98 throw new AgentLoadException("Target VM did not respond"); 99 } else if (result.startsWith(msgPrefix)) { 100 int retCode = Integer.parseInt(result.substring(msgPrefix.length())); 101 if (retCode != 0) { 102 throw new AgentInitializationException("Agent_OnAttach failed", retCode); 103 } 104 } else { 105 throw new AgentLoadException(result); 106 } 107 } 108 } 109 110 /* 111 * Load agent library - library name will be expanded in target VM 112 */ 113 public void loadAgentLibrary(String agentLibrary, String options) 114 throws AgentLoadException, AgentInitializationException, IOException 115 { 116 loadAgentLibrary(agentLibrary, false, options); 117 } 118 119 /* 120 * Load agent - absolute path of library provided to target VM 121 */ 122 public void loadAgentPath(String agentLibrary, String options) 123 throws AgentLoadException, AgentInitializationException, IOException 124 { 125 loadAgentLibrary(agentLibrary, true, options); 126 } |