< prev index next >

src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java

Print this page

        

@@ -37,10 +37,12 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Properties;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /*
  * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
  */

@@ -84,22 +86,30 @@
      * name and it will be expended in the target VM.
      */
     private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
         throws AgentLoadException, AgentInitializationException, IOException
     {
-        InputStream in = execute("load",
-                                 agentLibrary,
-                                 isAbsolute ? "true" : "false",
-                                 options);
-        try {
-            int result = readInt(in);
-            if (result != 0) {
-                throw new AgentInitializationException("Agent_OnAttach failed", result);
+        try (BufferedReader reader = new BufferedReader(
+                   new InputStreamReader(
+                            execute("load", agentLibrary,
+                                     Boolean.toString(isAbsolute), options)))) {
+            String result = reader.readLine();
+            if (result == null) {
+                throw new AgentLoadException("Target VM did not respond");
+            } else {
+                Matcher matcher = Pattern.compile("^return code: (\\d+)$")
+                                         .matcher(result);
+                if (matcher.matches()) {
+                    int retCode = Integer.parseInt(matcher.group(1));
+                    if (retCode != 0) {
+                        throw new AgentInitializationException(
+                                              "Agent_OnAttach failed", retCode);
+                    }
+                } else {
+                    throw new AgentLoadException(result);
+                }
             }
-        } finally {
-            in.close();
-
         }
     }
 
     /*
      * Load agent library - library name will be expanded in target VM
< prev index next >