< prev index next >

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

Print this page

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -27,28 +27,23 @@
  * @summary Exercise the java.lang.instrument.Instrumentation APIs on classes archived
  *          using CDS/AppCDSv1/AppCDSv2.
  * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes
  * @requires vm.cds
  * @requires vm.flavor != "minimal"
- * @modules java.base/jdk.internal.misc
- *          jdk.jartool/sun.tools.jar
- *          java.management
  * @build sun.hotspot.WhiteBox
  *        InstrumentationApp
  *        InstrumentationClassFileTransformer
  *        InstrumentationRegisterClassFileTransformer
  * @run main/othervm InstrumentationTest
  */
 
 // Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java
-// Note: Util       is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java
+// Note: Util       is from /test/hotspot/jtreg/runtime/appcds/test-classes/Util.java
 
 import com.sun.tools.attach.VirtualMachine;
-import com.sun.tools.attach.VirtualMachineDescriptor;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.util.List;
 import jdk.test.lib.Asserts;
 import jdk.test.lib.cds.CDSOptions;
 import jdk.test.lib.process.OutputAnalyzer;
 import jdk.test.lib.process.ProcessTools;
 

@@ -119,11 +114,15 @@
                 "-XX:+UnlockDiagnosticVMOptions",
                 "-XX:+WhiteBoxAPI",
                 "-Xshare:off",
                 agentCmdArg,
                 "InstrumentationApp", flagFile, bootJar, appJar, custJar);
-        TestCommon.executeAndLog(pb, "no-sharing").shouldHaveExitValue(0);
+        Process process = pb.start();
+        if (t != null) {
+            t.setPid(process.pid());
+        }
+        TestCommon.executeAndLog(process, "no-sharing").shouldHaveExitValue(0);
         checkAttach(t);
 
         // Dump the AppCDS archive. On some platforms AppCDSv2 may not be enabled, so we
         // first try the v2 classlist, and if that fails, revert to the v1 classlist.
         // Note that the InstrumentationApp$ArchivedIfAppCDSv2Enabled class is archived

@@ -148,17 +147,21 @@
         System.out.println("============================================================");
         System.out.println("");
 
         flagFile = getFlagFile(attachAgent);
         t = doAttach(attachAgent, flagFile, agentJar);
-        out = TestCommon.execAuto("-cp", appJar,
+        pb = TestCommon.createProcessBuilderWithAutoArchive("-cp", appJar,
                 bootCP,
                 "-XX:+UnlockDiagnosticVMOptions",
                 "-XX:+WhiteBoxAPI",
                 agentCmdArg,
                "InstrumentationApp", flagFile, bootJar, appJar, custJar);
-
+        process = pb.start();
+        if (t != null) {
+            t.setPid(process.pid());
+        }
+        out = TestCommon.executeAndLog(process, "auto");
         CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
         TestCommon.checkExec(out, opts);
         checkAttach(t);
     }
 

@@ -210,39 +213,40 @@
 
     static class AgentAttachThread extends Thread {
         String flagFile;
         String agentJar;
         volatile boolean succeeded;
+        volatile long pid;
+        volatile boolean pidIsKnown;
 
         AgentAttachThread(String flagFile, String agentJar) {
             this.flagFile = flagFile;
             this.agentJar = agentJar;
             this.succeeded = false;
+            this.pidIsKnown = false;
         }
 
-        static String getPid(String flagFile) throws Throwable {
-            while (true) {
-                // Keep polling until the child process has been launched. If for some
-                // reason the child process fails to launch, this test will be terminated
+        void setPid(long pid) {
+            this.pid = pid;
+            this.pidIsKnown = true;
+        }
+
+        String getPid() throws Throwable {
+            while (!pidIsKnown) {
+                // Keep polling until the child process has been launched, and its pid is known.
+                // If for some reason the child process fails to launch, this test will be terminated
                 // by JTREG's time-out mechanism.
                 Thread.sleep(100);
-                List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
-                for (VirtualMachineDescriptor vmd : vmds) {
-                    if (vmd.displayName().contains(flagFile) && vmd.displayName().contains("InstrumentationApp")) {
-                        // We use flagFile (which has the PID of this process) as a unique identifier
-                        // to ident the child process, which we want to attach to.
-                        System.out.println("Process found: " + vmd.id() + " " + vmd.displayName());
-                        return vmd.id();
-                    }
-                }
             }
+            System.out.println("pid = " + pid);
+            return Long.toString(pid);
         }
 
         public void run() {
             try {
-                String pid = getPid(flagFile);
-                VirtualMachine vm = VirtualMachine.attach(pid);
+                String pidStr = getPid();
+                VirtualMachine vm = VirtualMachine.attach(pidStr);
                 System.out.println(agentJar);
                 vm.loadAgent(agentJar);
             } catch (Throwable t) {
                 t.printStackTrace();
                 throw new RuntimeException(t);
< prev index next >