165 static int flagFileSerial = 1; 166 static private String getFlagFile(boolean attachAgent) { 167 if (attachAgent) { 168 // Do not reuse the same file name as Windows may fail to 169 // delete the file. 170 return "attach.flag." + ProcessHandle.current().pid() + 171 "." + (flagFileSerial++) + "." + System.currentTimeMillis(); 172 } else { 173 return "noattach"; 174 } 175 } 176 177 static AgentAttachThread doAttach(boolean attachAgent, String flagFile, String agentJar) throws Throwable { 178 if (!attachAgent) { 179 return null; 180 } 181 182 // We use the flagFile to prevent the child process to make progress, until we have 183 // attached to it. 184 File f = new File(flagFile); 185 FileOutputStream o = new FileOutputStream(f); 186 o.write(1); 187 o.close(); 188 if (!f.exists()) { 189 throw new RuntimeException("Failed to create " + f); 190 } 191 192 // At this point, the child process is not yet launched. Note that 193 // TestCommon.exec() and OutputAnalyzer.OutputAnalyzer() both block 194 // until the child process has finished. 195 // 196 // So, we will launch a AgentAttachThread which will poll the system 197 // until the child process is launched, and then do the attachment. 198 // The child process is uniquely identified by having flagFile in its 199 // command-line -- see AgentAttachThread.getPid(). 200 AgentAttachThread t = new AgentAttachThread(flagFile, agentJar); 201 t.start(); 202 return t; 203 } 204 205 static void checkAttach(AgentAttachThread thread) throws Throwable { 206 if (thread != null) { 207 thread.check(); | 165 static int flagFileSerial = 1; 166 static private String getFlagFile(boolean attachAgent) { 167 if (attachAgent) { 168 // Do not reuse the same file name as Windows may fail to 169 // delete the file. 170 return "attach.flag." + ProcessHandle.current().pid() + 171 "." + (flagFileSerial++) + "." + System.currentTimeMillis(); 172 } else { 173 return "noattach"; 174 } 175 } 176 177 static AgentAttachThread doAttach(boolean attachAgent, String flagFile, String agentJar) throws Throwable { 178 if (!attachAgent) { 179 return null; 180 } 181 182 // We use the flagFile to prevent the child process to make progress, until we have 183 // attached to it. 184 File f = new File(flagFile); 185 try (FileOutputStream o = new FileOutputStream(f)) { 186 o.write(1); 187 } 188 if (!f.exists()) { 189 throw new RuntimeException("Failed to create " + f); 190 } 191 192 // At this point, the child process is not yet launched. Note that 193 // TestCommon.exec() and OutputAnalyzer.OutputAnalyzer() both block 194 // until the child process has finished. 195 // 196 // So, we will launch a AgentAttachThread which will poll the system 197 // until the child process is launched, and then do the attachment. 198 // The child process is uniquely identified by having flagFile in its 199 // command-line -- see AgentAttachThread.getPid(). 200 AgentAttachThread t = new AgentAttachThread(flagFile, agentJar); 201 t.start(); 202 return t; 203 } 204 205 static void checkAttach(AgentAttachThread thread) throws Throwable { 206 if (thread != null) { 207 thread.check(); |