8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file
authordsamersoff
Thu, 18 Aug 2016 13:19:38 +0300
changeset 40685 e6f3a9fff607
parent 40468 ae9bae97a312
child 40686 1b855fa5c9c5
8157236: attach on ARMv7 fails with com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file Summary: Add more diagnostic to attach code Reviewed-by: dholmes, alanb
jdk/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java
jdk/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java
jdk/src/jdk.attach/macosx/classes/sun/tools/attach/VirtualMachineImpl.java
jdk/src/jdk.attach/macosx/native/libattach/VirtualMachineImpl.c
jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
jdk/src/jdk.attach/solaris/classes/sun/tools/attach/VirtualMachineImpl.java
--- a/jdk/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/aix/classes/sun/tools/attach/VirtualMachineImpl.java	Thu Aug 18 13:19:38 2016 +0300
@@ -76,20 +76,29 @@
                 sendQuitTo(pid);
 
                 // give the target VM time to start the attach mechanism
-                int i = 0;
-                long delay = 200;
-                int retries = (int)(attachTimeout() / delay);
+                final int delay_step = 100;
+                final long timeout = attachTimeout();
+                long time_spend = 0;
+                long delay = 0;
                 do {
+                    // Increase timeout on each attempt to reduce polling
+                    delay += delay_step;
                     try {
                         Thread.sleep(delay);
                     } catch (InterruptedException x) { }
                     path = findSocketFile(pid);
-                    i++;
-                } while (i <= retries && path == null);
+
+                    time_spend += delay;
+                    if (time_spend > timeout/2 && path == null) {
+                        // Send QUIT again to give target VM the last chance to react
+                        sendQuitTo(pid);
+                    }
+                } while (time_spend <= timeout && path == null);
                 if (path == null) {
                     throw new AttachNotSupportedException(
-                        "Unable to open socket file: target process not responding " +
-                        "or HotSpot VM not loaded");
+                        String.format("Unable to open socket file %s: " +
+                          "target process %d doesn't respond within %dms " +
+                          "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
                 }
             } finally {
                 f.delete();
--- a/jdk/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java	Thu Aug 18 13:19:38 2016 +0300
@@ -44,9 +44,6 @@
     // Any changes to this needs to be synchronized with HotSpot.
     private static final String tmpdir = "/tmp";
 
-    // Indicates if this machine uses the old LinuxThreads
-    static boolean isLinuxThreads;
-
     // The patch to the socket file created by the target VM
     String path;
 
@@ -73,44 +70,37 @@
         if (path == null) {
             File f = createAttachFile(pid);
             try {
-                // On LinuxThreads each thread is a process and we don't have the
-                // pid of the VMThread which has SIGQUIT unblocked. To workaround
-                // this we get the pid of the "manager thread" that is created
-                // by the first call to pthread_create. This is parent of all
-                // threads (except the initial thread).
-                if (isLinuxThreads) {
-                    int mpid;
-                    try {
-                        mpid = getLinuxThreadsManager(pid);
-                    } catch (IOException x) {
-                        throw new AttachNotSupportedException(x.getMessage());
-                    }
-                    assert(mpid >= 1);
-                    sendQuitToChildrenOf(mpid);
-                } else {
-                    sendQuitTo(pid);
-                }
+                sendQuitTo(pid);
 
                 // give the target VM time to start the attach mechanism
-                int i = 0;
-                long delay = 200;
-                int retries = (int)(attachTimeout() / delay);
+                final int delay_step = 100;
+                final long timeout = attachTimeout();
+                long time_spend = 0;
+                long delay = 0;
                 do {
+                    // Increase timeout on each attempt to reduce polling
+                    delay += delay_step;
                     try {
                         Thread.sleep(delay);
                     } catch (InterruptedException x) { }
                     path = findSocketFile(pid);
-                    i++;
-                } while (i <= retries && path == null);
+
+                    time_spend += delay;
+                    if (time_spend > timeout/2 && path == null) {
+                        // Send QUIT again to give target VM the last chance to react
+                        sendQuitTo(pid);
+                    }
+                } while (time_spend <= timeout && path == null);
                 if (path == null) {
                     throw new AttachNotSupportedException(
-                        "Unable to open socket file: target process not responding " +
-                        "or HotSpot VM not loaded");
+                        String.format("Unable to open socket file %s: " +
+                          "target process %d doesn't respond within %dms " +
+                          "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
                 }
             } finally {
                 f.delete();
             }
-        }
+      }
 
         // Check that the file owner/permission to avoid attaching to
         // bogus process
@@ -340,6 +330,5 @@
 
     static {
         System.loadLibrary("attach");
-        isLinuxThreads = isLinuxThreads();
     }
 }
--- a/jdk/src/jdk.attach/macosx/classes/sun/tools/attach/VirtualMachineImpl.java	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/macosx/classes/sun/tools/attach/VirtualMachineImpl.java	Thu Aug 18 13:19:38 2016 +0300
@@ -70,26 +70,34 @@
         // Then we attempt to find the socket file again.
         path = findSocketFile(pid);
         if (path == null) {
-            File f = new File(tmpdir, ".attach_pid" + pid);
-            createAttachFile(f.getPath());
+            File f = createAttachFile(pid);
             try {
                 sendQuitTo(pid);
 
                 // give the target VM time to start the attach mechanism
-                int i = 0;
-                long delay = 200;
-                int retries = (int)(attachTimeout() / delay);
+                final int delay_step = 100;
+                final long timeout = attachTimeout();
+                long time_spend = 0;
+                long delay = 0;
                 do {
+                    // Increase timeout on each attempt to reduce polling
+                    delay += delay_step;
                     try {
                         Thread.sleep(delay);
                     } catch (InterruptedException x) { }
                     path = findSocketFile(pid);
-                    i++;
-                } while (i <= retries && path == null);
+
+                    time_spend += delay;
+                    if (time_spend > timeout/2 && path == null) {
+                        // Send QUIT again to give target VM the last chance to react
+                        sendQuitTo(pid);
+                    }
+                } while (time_spend <= timeout && path == null);
                 if (path == null) {
                     throw new AttachNotSupportedException(
-                        "Unable to open socket file: target process not responding " +
-                        "or HotSpot VM not loaded");
+                        String.format("Unable to open socket file %s: " +
+                          "target process %d doesn't respond within %dms " +
+                          "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
                 }
             } finally {
                 f.delete();
@@ -282,6 +290,12 @@
         write(fd, b, 0, 1);
     }
 
+    private File createAttachFile(int pid) throws IOException {
+        String fn = ".attach_pid" + pid;
+        File f = new File(tmpdir, fn);
+        createAttachFile0(f.getPath());
+        return f;
+    }
 
     //-- native methods
 
@@ -299,7 +313,7 @@
 
     static native void write(int fd, byte buf[], int off, int bufLen) throws IOException;
 
-    static native void createAttachFile(String path);
+    static native void createAttachFile0(String path);
 
     static native String getTempDir();
 
--- a/jdk/src/jdk.attach/macosx/native/libattach/VirtualMachineImpl.c	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/macosx/native/libattach/VirtualMachineImpl.c	Thu Aug 18 13:19:38 2016 +0300
@@ -270,7 +270,7 @@
  * Method:    createAttachFile
  * Signature: (Ljava.lang.String;)V
  */
-JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_createAttachFile(JNIEnv *env, jclass cls, jstring path)
+JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_createAttachFile0(JNIEnv *env, jclass cls, jstring path)
 {
     const char* _path;
     jboolean isCopy;
--- a/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java	Thu Aug 18 13:19:38 2016 +0300
@@ -319,7 +319,7 @@
 
     // -- attach timeout support
 
-    private static long defaultAttachTimeout = 5000;
+    private static long defaultAttachTimeout = 10000;
     private volatile long attachTimeout;
 
     /*
--- a/jdk/src/jdk.attach/solaris/classes/sun/tools/attach/VirtualMachineImpl.java	Tue Aug 16 09:57:50 2016 +0200
+++ b/jdk/src/jdk.attach/solaris/classes/sun/tools/attach/VirtualMachineImpl.java	Thu Aug 18 13:19:38 2016 +0300
@@ -71,27 +71,36 @@
         } catch (FileNotFoundException fnf1) {
             File f = createAttachFile(pid);
             try {
-                // kill -QUIT will tickle target VM to check for the
-                // attach file.
                 sigquit(pid);
 
                 // give the target VM time to start the attach mechanism
-                int i = 0;
-                long delay = 200;
-                int retries = (int)(attachTimeout() / delay);
+                final int delay_step = 100;
+                final long timeout = attachTimeout();
+                long time_spend = 0;
+                long delay = 0;
                 do {
+                    // Increase timeout on each attempt to reduce polling
+                    delay += delay_step;
                     try {
                         Thread.sleep(delay);
                     } catch (InterruptedException x) { }
                     try {
                         fd = openDoor(pid);
-                    } catch (FileNotFoundException fnf2) { }
-                    i++;
-                } while (i <= retries && fd == -1);
-                if (fd == -1) {
+                    } catch (FileNotFoundException fnf2) {
+                        // pass
+                    }
+
+                    time_spend += delay;
+                    if (time_spend > timeout/2 && fd == -1) {
+                        // Send QUIT again to give target VM the last chance to react
+                        sigquit(pid);
+                    }
+                } while (time_spend <= timeout && fd == -1);
+                if (fd  == -1) {
                     throw new AttachNotSupportedException(
-                        "Unable to open door: target process not responding or " +
-                        "HotSpot VM not loaded");
+                        String.format("Unable to open door %s: " +
+                          "target process %d doesn't respond within %dms " +
+                          "or HotSpot VM not loaded", f.getPath(), pid, time_spend));
                 }
             } finally {
                 f.delete();