test/hotspot/jtreg/runtime/appcds/jvmti/InstrumentationTest.java
author iklam
Tue, 26 Mar 2019 11:34:40 -0700
changeset 54297 f98f158c1db8
parent 52125 28375a1de254
permissions -rw-r--r--
8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out Summary: Avoid using VirtualMachine.list(). Use Process.pid() instead. Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     1
/*
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     4
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     8
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    13
 * accompanied this code).
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    14
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    18
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    21
 * questions.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    22
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    23
 */
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    24
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    25
/*
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    26
 * @test
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    27
 * @summary Exercise the java.lang.instrument.Instrumentation APIs on classes archived
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    28
 *          using CDS/AppCDSv1/AppCDSv2.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    29
 * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes
48469
7312ae4465d6 8193672: [test] Enhance vm.cds property to check for all conditions required to run CDS tests
iklam
parents: 48403
diff changeset
    30
 * @requires vm.cds
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    31
 * @requires vm.flavor != "minimal"
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    32
 * @build sun.hotspot.WhiteBox
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    33
 *        InstrumentationApp
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    34
 *        InstrumentationClassFileTransformer
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    35
 *        InstrumentationRegisterClassFileTransformer
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    36
 * @run main/othervm InstrumentationTest
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    37
 */
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    38
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    39
// Note: TestCommon is from /test/hotspot/jtreg/runtime/appcds/TestCommon.java
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    40
// Note: Util       is from /test/hotspot/jtreg/runtime/appcds/test-classes/TestCommon.java
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    41
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    42
import com.sun.tools.attach.VirtualMachine;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    43
import java.io.File;
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
    44
import java.io.FileInputStream;
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
    45
import java.util.Scanner;
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    46
import jdk.test.lib.Asserts;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    47
import jdk.test.lib.cds.CDSOptions;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    48
import jdk.test.lib.process.OutputAnalyzer;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    49
import jdk.test.lib.process.ProcessTools;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    50
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    51
public class InstrumentationTest {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    52
    public static String bootClasses[] = {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    53
        "InstrumentationApp$Intf",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    54
        "InstrumentationApp$Bar",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    55
        "sun.hotspot.WhiteBox",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    56
    };
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    57
    public static String appClasses[] = {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    58
        "InstrumentationApp",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    59
        "InstrumentationApp$Foo",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    60
        "InstrumentationApp$MyLoader",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    61
    };
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    62
    public static String custClasses[] = {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    63
        "InstrumentationApp$Coo",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    64
    };
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    65
    public static String sharedClasses[] = TestCommon.concat(bootClasses, appClasses);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    66
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    67
    public static String agentClasses[] = {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    68
        "InstrumentationClassFileTransformer",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    69
        "InstrumentationRegisterClassFileTransformer",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    70
        "Util",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    71
    };
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    72
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    73
    public static void main(String[] args) throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    74
        runTest(false);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    75
        runTest(true);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    76
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    77
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    78
    public static void runTest(boolean attachAgent) throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    79
        String bootJar =
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    80
            ClassFileInstaller.writeJar("InstrumentationBoot.jar", bootClasses);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    81
        String appJar =
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    82
            ClassFileInstaller.writeJar("InstrumentationApp.jar",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    83
                                        TestCommon.concat(appClasses,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    84
                                                          "InstrumentationApp$ArchivedIfAppCDSv2Enabled"));
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    85
        String custJar =
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    86
            ClassFileInstaller.writeJar("InstrumentationCust.jar", custClasses);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    87
        String agentJar =
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    88
            ClassFileInstaller.writeJar("InstrumentationAgent.jar",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    89
                                        ClassFileInstaller.Manifest.fromSourceFile("InstrumentationAgent.mf"),
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    90
                                        agentClasses);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    91
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    92
        String bootCP = "-Xbootclasspath/a:" + bootJar;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    93
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    94
        System.out.println("");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    95
        System.out.println("============================================================");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    96
        System.out.println("CDS: NO, attachAgent: " + (attachAgent ? "YES" : "NO"));
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    97
        System.out.println("============================================================");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    98
        System.out.println("");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    99
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   100
        String agentCmdArg, flagFile;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   101
        if (attachAgent) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   102
            // we will attach the agent, so don't specify -javaagent in the command line. We'll use
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   103
            // something harmless like -showversion to make it easier to construct the command line
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   104
            agentCmdArg = "-showversion";
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   105
        } else {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   106
            agentCmdArg = "-javaagent:" + agentJar;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   107
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   108
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   109
        // First, run the test class directly, w/o sharing, as a baseline reference
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   110
        flagFile = getFlagFile(attachAgent);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   111
        AgentAttachThread t = doAttach(attachAgent, flagFile, agentJar);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   112
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   113
                bootCP,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   114
                "-cp", appJar,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   115
                "-XX:+UnlockDiagnosticVMOptions",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   116
                "-XX:+WhiteBoxAPI",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   117
                "-Xshare:off",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   118
                agentCmdArg,
52125
28375a1de254 8212028: Use run-test makefile framework for testing in Oracle's Mach5
erikj
parents: 48469
diff changeset
   119
                "InstrumentationApp", flagFile, bootJar, appJar, custJar);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   120
        TestCommon.executeAndLog(pb, "no-sharing").shouldHaveExitValue(0);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   121
        checkAttach(t);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   122
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   123
        // Dump the AppCDS archive. On some platforms AppCDSv2 may not be enabled, so we
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   124
        // first try the v2 classlist, and if that fails, revert to the v1 classlist.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   125
        // Note that the InstrumentationApp$ArchivedIfAppCDSv2Enabled class is archived
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   126
        // only if V2 is enabled. This is tested by InstrumentationApp.isAppCDSV2Enabled().
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   127
        String[] v2Classes = {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   128
            "InstrumentationApp$ArchivedIfAppCDSv2Enabled",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   129
            "java/lang/Object id: 0",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   130
            "InstrumentationApp$Intf id: 1",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   131
            "InstrumentationApp$Coo  id: 2 super: 0 interfaces: 1 source: " + custJar,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   132
        };
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   133
        String[] sharedClassesWithV2 = TestCommon.concat(v2Classes, sharedClasses);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   134
        OutputAnalyzer out = TestCommon.dump(appJar, sharedClassesWithV2, bootCP);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   135
        if (out.getExitValue() != 0) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   136
            System.out.println("Redumping with AppCDSv2 disabled");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   137
                TestCommon.testDump(appJar, sharedClasses, bootCP);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   138
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   139
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   140
        // Run with AppCDS.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   141
        System.out.println("");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   142
        System.out.println("============================================================");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   143
        System.out.println("CDS: YES, attachAgent: " + (attachAgent ? "YES" : "NO"));
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   144
        System.out.println("============================================================");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   145
        System.out.println("");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   146
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   147
        flagFile = getFlagFile(attachAgent);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   148
        t = doAttach(attachAgent, flagFile, agentJar);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   149
        out = TestCommon.execAuto("-cp", appJar,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   150
                bootCP,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   151
                "-XX:+UnlockDiagnosticVMOptions",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   152
                "-XX:+WhiteBoxAPI",
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   153
                agentCmdArg,
52125
28375a1de254 8212028: Use run-test makefile framework for testing in Oracle's Mach5
erikj
parents: 48469
diff changeset
   154
               "InstrumentationApp", flagFile, bootJar, appJar, custJar);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   155
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   156
        CDSOptions opts = (new CDSOptions()).setXShareMode("auto");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   157
        TestCommon.checkExec(out, opts);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   158
        checkAttach(t);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   159
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   160
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   161
    static int flagFileSerial = 1;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   162
    static private String getFlagFile(boolean attachAgent) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   163
        if (attachAgent) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   164
            // Do not reuse the same file name as Windows may fail to
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   165
            // delete the file.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   166
            return "attach.flag." + ProcessHandle.current().pid() +
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   167
                    "." + (flagFileSerial++) + "." + System.currentTimeMillis();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   168
        } else {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   169
            return "noattach";
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   170
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   171
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   172
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   173
    static AgentAttachThread doAttach(boolean attachAgent, String flagFile, String agentJar) throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   174
        if (!attachAgent) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   175
            return null;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   176
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   177
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   178
        // Hand-shake protocol with the child process
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   179
        // [1] Parent process (this process) launches child process (InstrumentationApp)
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   180
        //     and then waits until child process writes its pid into the flagFile.
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   181
        // [2] Child process process starts up, writes its pid into the flagFile,
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   182
        //     and waits for the flagFile to be deleted.
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   183
        // [3] When parent process gets the pid, it attaches to the child process
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   184
        //     (if we attempt to attach to a process too early, the SIGQUIT
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   185
        //     may cause the child to die) and deletes the flagFile.
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   186
        // [4] Child process resumes execution.
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   187
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   188
        File f = new File(flagFile);
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   189
        f.delete();
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   190
        if (f.exists()) {
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   191
            throw new RuntimeException("Flag file should not exist: " + f);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   192
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   193
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   194
        // At this point, the child process is not yet launched. Note that
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   195
        // TestCommon.exec() and OutputAnalyzer.OutputAnalyzer() both block
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   196
        // until the child process has finished.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   197
        //
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   198
        // So, we will launch a AgentAttachThread which will poll the flagFile
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   199
        // until the child process is launched.
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   200
        AgentAttachThread t = new AgentAttachThread(flagFile, agentJar);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   201
        t.start();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   202
        return t;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   203
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   204
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   205
    static void checkAttach(AgentAttachThread thread) throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   206
        if (thread != null) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   207
            thread.check();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   208
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   209
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   210
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   211
    static class AgentAttachThread extends Thread {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   212
        String flagFile;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   213
        String agentJar;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   214
        volatile boolean succeeded;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   215
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   216
        AgentAttachThread(String flagFile, String agentJar) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   217
            this.flagFile = flagFile;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   218
            this.agentJar = agentJar;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   219
            this.succeeded = false;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   220
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   221
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   222
        static String getPid(String flagFile) throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   223
            while (true) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   224
                // Keep polling until the child process has been launched. If for some
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   225
                // reason the child process fails to launch, this test will be terminated
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   226
                // by JTREG's time-out mechanism.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   227
                Thread.sleep(100);
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   228
                File f = new File(flagFile);
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   229
                if (f.exists() && f.length() > 100) {
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   230
                    try (FileInputStream in = new FileInputStream(f)) {
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   231
                        Scanner scanner = new Scanner(in);
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   232
                        return Long.toString(scanner.nextLong());
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   233
                    } catch (Throwable t) {
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   234
                        // This may happen on Windows if the child process has not
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   235
                        // fully closed the output stream to the flagFile
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   236
                        System.out.println("Ignored: " + t);
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   237
                        t.printStackTrace(System.out);
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   238
                        continue;
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   239
                    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   240
                }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   241
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   242
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   243
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   244
        public void run() {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   245
            try {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   246
                String pid = getPid(flagFile);
54297
f98f158c1db8 8217347: [TESTBUG] runtime/appcds/jvmti/InstrumentationTest.java timed out
iklam
parents: 52125
diff changeset
   247
                System.out.println("child pid = " + pid);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   248
                VirtualMachine vm = VirtualMachine.attach(pid);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   249
                System.out.println(agentJar);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   250
                vm.loadAgent(agentJar);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   251
            } catch (Throwable t) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   252
                t.printStackTrace();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   253
                throw new RuntimeException(t);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   254
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   255
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   256
            // Delete the flagFile to indicate to the child process that we
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   257
            // have attached to it, so it should proceed.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   258
            File f = new File(flagFile);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   259
            for (int i=0; i<5; i++) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   260
                // The detele may fail on Windows if the child JVM is checking
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   261
                // f.exists() at exactly the same time?? Let's do a little
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   262
                // dance.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   263
                f.delete();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   264
                try {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   265
                    Thread.sleep(10);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   266
                } catch (Throwable t) {;}
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   267
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   268
            if (f.exists()) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   269
                throw new RuntimeException("Failed to delete " + f);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   270
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   271
            System.out.println("Attach succeeded (parent)");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   272
            succeeded = true;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   273
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   274
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   275
        void check() throws Throwable {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   276
            super.join();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   277
            if (!succeeded) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   278
                throw new RuntimeException("Attaching agent to child VM failed");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   279
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   280
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   281
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   282
}
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   283