< prev index next >

test/hotspot/jtreg/runtime/appcds/jvmti/InstrumentationTest.java

Print this page
   1 /*
   2  * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @summary Exercise the java.lang.instrument.Instrumentation APIs on classes archived
  28  *          using CDS/AppCDSv1/AppCDSv2.
  29  * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes
  30  * @requires vm.cds
  31  * @requires vm.flavor != "minimal"
  32  * @modules java.base/jdk.internal.misc
  33  *          jdk.jartool/sun.tools.jar
  34  *          java.management
  35  * @build sun.hotspot.WhiteBox
  36  *        InstrumentationApp
  37  *        InstrumentationClassFileTransformer
  38  *        InstrumentationRegisterClassFileTransformer
  39  * @run main/othervm InstrumentationTest
  40  */
  41 
  42 // Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java
  43 // Note: Util       is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java
  44 
  45 import com.sun.tools.attach.VirtualMachine;
  46 import com.sun.tools.attach.VirtualMachineDescriptor;
  47 import java.io.File;
  48 import java.io.FileOutputStream;
  49 import java.util.List;
  50 import jdk.test.lib.Asserts;
  51 import jdk.test.lib.cds.CDSOptions;
  52 import jdk.test.lib.process.OutputAnalyzer;
  53 import jdk.test.lib.process.ProcessTools;
  54 
  55 public class InstrumentationTest {
  56     public static String bootClasses[] = {
  57         "InstrumentationApp$Intf",
  58         "InstrumentationApp$Bar",
  59         "sun.hotspot.WhiteBox",
  60     };
  61     public static String appClasses[] = {
  62         "InstrumentationApp",
  63         "InstrumentationApp$Foo",
  64         "InstrumentationApp$MyLoader",
  65     };
  66     public static String custClasses[] = {
  67         "InstrumentationApp$Coo",
  68     };
  69     public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);


 162         checkAttach(t);
 163     }
 164 
 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();
 208         }
 209     }
 210 
 211     static class AgentAttachThread extends Thread {
 212         String flagFile;
 213         String agentJar;
 214         volatile boolean succeeded;
 215 
 216         AgentAttachThread(String flagFile, String agentJar) {
 217             this.flagFile = flagFile;
 218             this.agentJar = agentJar;
 219             this.succeeded = false;
 220         }
 221 
 222         static String getPid(String flagFile) throws Throwable {
 223             while (true) {
 224                 // Keep polling until the child process has been launched. If for some
 225                 // reason the child process fails to launch, this test will be terminated
 226                 // by JTREG's time-out mechanism.
 227                 Thread.sleep(100);
 228                 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
 229                 for (VirtualMachineDescriptor vmd : vmds) {
 230                     if (vmd.displayName().contains(flagFile) && vmd.displayName().contains("InstrumentationApp")) {
 231                         // We use flagFile (which has the PID of this process) as a unique identifier
 232                         // to ident the child process, which we want to attach to.
 233                         System.out.println("Process found: " + vmd.id() + " " + vmd.displayName());
 234                         return vmd.id();




 235                     }
 236                 }
 237             }
 238         }
 239 
 240         public void run() {
 241             try {
 242                 String pid = getPid(flagFile);

 243                 VirtualMachine vm = VirtualMachine.attach(pid);
 244                 System.out.println(agentJar);
 245                 vm.loadAgent(agentJar);
 246             } catch (Throwable t) {
 247                 t.printStackTrace();
 248                 throw new RuntimeException(t);
 249             }
 250 
 251             // Delete the flagFile to indicate to the child process that we
 252             // have attached to it, so it should proceed.
 253             File f = new File(flagFile);
 254             for (int i=0; i<5; i++) {
 255                 // The detele may fail on Windows if the child JVM is checking
 256                 // f.exists() at exactly the same time?? Let's do a little
 257                 // dance.
 258                 f.delete();
 259                 try {
 260                     Thread.sleep(10);
 261                 } catch (Throwable t) {;}
 262             }
   1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @summary Exercise the java.lang.instrument.Instrumentation APIs on classes archived
  28  *          using CDS/AppCDSv1/AppCDSv2.
  29  * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes
  30  * @requires vm.cds
  31  * @requires vm.flavor != "minimal"



  32  * @build sun.hotspot.WhiteBox
  33  *        InstrumentationApp
  34  *        InstrumentationClassFileTransformer
  35  *        InstrumentationRegisterClassFileTransformer
  36  * @run main/othervm InstrumentationTest
  37  */
  38 
  39 // Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java
  40 // Note: Util       is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java
  41 
  42 import com.sun.tools.attach.VirtualMachine;

  43 import java.io.File;
  44 import java.io.FileInputStream;
  45 import java.util.Scanner;
  46 import jdk.test.lib.Asserts;
  47 import jdk.test.lib.cds.CDSOptions;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 import jdk.test.lib.process.ProcessTools;
  50 
  51 public class InstrumentationTest {
  52     public static String bootClasses[] = {
  53         "InstrumentationApp$Intf",
  54         "InstrumentationApp$Bar",
  55         "sun.hotspot.WhiteBox",
  56     };
  57     public static String appClasses[] = {
  58         "InstrumentationApp",
  59         "InstrumentationApp$Foo",
  60         "InstrumentationApp$MyLoader",
  61     };
  62     public static String custClasses[] = {
  63         "InstrumentationApp$Coo",
  64     };
  65     public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);


 158         checkAttach(t);
 159     }
 160 
 161     static int flagFileSerial = 1;
 162     static private String getFlagFile(boolean attachAgent) {
 163         if (attachAgent) {
 164             // Do not reuse the same file name as Windows may fail to
 165             // delete the file.
 166             return "attach.flag." + ProcessHandle.current().pid() +
 167                     "." + (flagFileSerial++) + "." + System.currentTimeMillis();
 168         } else {
 169             return "noattach";
 170         }
 171     }
 172 
 173     static AgentAttachThread doAttach(boolean attachAgent, String flagFile, String agentJar) throws Throwable {
 174         if (!attachAgent) {
 175             return null;
 176         }
 177 
 178         // Hand-shake protocol with the child process
 179         // [1] Parent process (this process) launches child process (InstrumentationApp)
 180         //     and then waits until child process writes its pid into the flagFile.
 181         // [2] Child process process starts up, writes its pid into the flagFile,
 182         //     and waits for the flagFile to be deleted.
 183         // [3] When parent process gets the pid, it attaches to the child process
 184         //     (if we attempt to attach to a process too early, the SIGQUIT
 185         //     may cause the child to die) and deletes the flagFile.
 186         // [4] Child process resumes execution.
 187         
 188         File f = new File(flagFile);
 189         f.delete();
 190         if (f.exists()) {
 191             throw new RuntimeException("Flag file should not exist: " + f);


 192         }
 193 
 194         // At this point, the child process is not yet launched. Note that
 195         // TestCommon.exec() and OutputAnalyzer.OutputAnalyzer() both block
 196         // until the child process has finished.
 197         //
 198         // So, we will launch a AgentAttachThread which will poll the flagFile
 199         // until the child process is launched.


 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();
 208         }
 209     }
 210 
 211     static class AgentAttachThread extends Thread {
 212         String flagFile;
 213         String agentJar;
 214         volatile boolean succeeded;
 215 
 216         AgentAttachThread(String flagFile, String agentJar) {
 217             this.flagFile = flagFile;
 218             this.agentJar = agentJar;
 219             this.succeeded = false;
 220         }
 221 
 222         static String getPid(String flagFile) throws Throwable {
 223             while (true) {
 224                 // Keep polling until the child process has been launched. If for some
 225                 // reason the child process fails to launch, this test will be terminated
 226                 // by JTREG's time-out mechanism.
 227                 Thread.sleep(100);
 228                 File f = new File(flagFile);
 229                 if (f.exists() && f.length() > 100) {
 230                     try (FileInputStream in = new FileInputStream(f)) {
 231                         Scanner scanner = new Scanner(in);
 232                         return Long.toString(scanner.nextLong());
 233                     } catch (Throwable t) {
 234                         // This may happen on Windows if the child process has not
 235                         // fully closed the output stream to the flagFile
 236                         System.out.println("Ignored: " + t);
 237                         t.printStackTrace(System.out);
 238                         continue;
 239                     }
 240                 }
 241             }
 242         }
 243 
 244         public void run() {
 245             try {
 246                 String pid = getPid(flagFile);
 247                 System.out.println("child pid = " + pid);
 248                 VirtualMachine vm = VirtualMachine.attach(pid);
 249                 System.out.println(agentJar);
 250                 vm.loadAgent(agentJar);
 251             } catch (Throwable t) {
 252                 t.printStackTrace();
 253                 throw new RuntimeException(t);
 254             }
 255 
 256             // Delete the flagFile to indicate to the child process that we
 257             // have attached to it, so it should proceed.
 258             File f = new File(flagFile);
 259             for (int i=0; i<5; i++) {
 260                 // The detele may fail on Windows if the child JVM is checking
 261                 // f.exists() at exactly the same time?? Let's do a little
 262                 // dance.
 263                 f.delete();
 264                 try {
 265                     Thread.sleep(10);
 266                 } catch (Throwable t) {;}
 267             }
< prev index next >