8196977: add test for JDK-8161605
authoramenkov
Wed, 07 Feb 2018 12:55:47 -0800
changeset 48879 7bbf7c4d89b5
parent 48878 2f265d2ac735
child 48881 b480310039f1
8196977: add test for JDK-8161605 Reviewed-by: sspitsyn, mseledtsov
test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/CanGenerateAllClassHook.java
test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/libCanGenerateAllClassHook.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/CanGenerateAllClassHook.java	Wed Feb 07 12:55:47 2018 -0800
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2018, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8161605
+ * @summary Tests that jvmtiEnv::GetPotentialCapabilities reports
+ *          can_generate_all_class_hook_events capability with CDS (-Xshare:on)
+ *          at ONLOAD and LIVE phases
+ * @library /test/lib
+ * @compile CanGenerateAllClassHook.java
+ * @run main/othervm/native CanGenerateAllClassHook
+ */
+
+import jdk.test.lib.cds.CDSTestUtils;
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.process.ProcessTools;
+import java.io.File;
+import java.io.IOException;
+
+/*
+ * The simplest way to test is to use system classes.jsa,
+ * but we cannot rely on tested JRE/JDK has it.
+ * So the test runs 2 java processes -
+ * 1st to generate custom shared archive file:
+ *     java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:dump
+ * and 2nd to perform the actual testing using generated shared archive:
+ *     java -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=<jsa_file> -Xshare:on
+  *         -agentlib:<agent> CanGenerateAllClassHook
+ */
+public class CanGenerateAllClassHook {
+
+    private static final String agentLib = "CanGenerateAllClassHook";
+
+    private static native int getClassHookAvail();
+    private static native int getOnLoadClassHookAvail();
+
+    public static void main(String[] args) throws Exception {
+        if (args.length == 0) {
+            // this is master run
+
+            final File jsaFile = File.createTempFile(agentLib, ".jsa");
+            jsaFile.deleteOnExit();
+            final String jsaPath = jsaFile.getAbsolutePath();
+
+            log("generating CDS archive...");
+            execJava(
+                        "-XX:+UnlockDiagnosticVMOptions",
+                        "-XX:SharedArchiveFile=" + jsaPath,
+                        "-Xshare:dump")
+                    .shouldHaveExitValue(0);
+            log("CDS generation completed.");
+
+            OutputAnalyzer output = execJava(
+                    "-XX:+UnlockDiagnosticVMOptions",
+                    "-XX:SharedArchiveFile=" + jsaPath,
+                    "-Xshare:on",
+                    "-agentlib:" + agentLib,
+                    // copy java.library.path
+                    "-Djava.library.path=" + System.getProperty("java.library.path"),
+                    // specify "-showversion" to ensure the test runs in shared mode
+                    "-showversion",
+                    // class to run
+                    CanGenerateAllClassHook.class.getCanonicalName(),
+                    // and arg
+                    "test");
+            // Xshare:on can cause intermittent failure
+            // checkExec handles this.
+            CDSTestUtils.checkExec(output);
+
+            log("Test PASSED.");
+        } else {
+            // this is test run
+            try {
+                System.loadLibrary(agentLib);
+            } catch (UnsatisfiedLinkError ex) {
+                System.err.println("Failed to load " + agentLib + " lib");
+                System.err.println("java.library.path: " + System.getProperty("java.library.path"));
+                throw ex;
+            }
+
+            final int onLoadValue = getOnLoadClassHookAvail();
+            final int liveValue = getClassHookAvail();
+            // Possible values returned:
+            // 1 - the capability is supported;
+            // 0 - the capability is not supported;
+            // -1 - error occured.
+
+            log("can_generate_all_class_hook_events value capability:");
+            log("ONLOAD phase: " + (onLoadValue < 0 ? "Failed to read" : onLoadValue));
+            log("LIVE phase: " + (liveValue < 0 ? "Failed to read" : liveValue));
+            if (onLoadValue != 1 || liveValue != 1) {
+                throw new RuntimeException("The can_generate_all_class_hook_events capability "
+                        + " is expected to be available in both ONLOAD and LIVE phases");
+            }
+        }
+    }
+
+    private static void log(String msg) {
+        System.out.println(msg);
+        System.out.flush();
+    }
+
+    private static OutputAnalyzer execJava(String... args) throws IOException {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
+
+        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+
+        log("[STDERR]\n" + output.getStderr());
+        log("[STDOUT]\n" + output.getStdout());
+
+        return output;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/libCanGenerateAllClassHook.c	Wed Feb 07 12:55:47 2018 -0800
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2018, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "jvmti.h"
+#include "jni.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef JNI_ENV_ARG
+
+#ifdef __cplusplus
+#define JNI_ENV_ARG(x, y) y
+#define JNI_ENV_PTR(x) x
+#else
+#define JNI_ENV_ARG(x,y) x, y
+#define JNI_ENV_PTR(x) (*x)
+#endif
+
+#endif
+
+
+static jvmtiEnv *jvmti = NULL;
+
+typedef enum {
+    Yes = 1,
+    No = 0,
+    Error = -1
+} IsAvail;
+
+static IsAvail onLoadIsAvail = Error;
+
+void reportError(const char *msg, int err) {
+    printf("%s, error: %d\n", msg, err);
+}
+
+static IsAvail isClassHookAvail() {
+    IsAvail result = Error;
+    do {
+        jvmtiCapabilities caps;
+        jvmtiPhase phase;
+        jvmtiError err;
+
+        if (jvmti == NULL) {
+            reportError("jvmti is NULL", -1);
+            break;
+        }
+
+        err = (*jvmti)->GetPhase(jvmti, &phase);
+        if (err != JVMTI_ERROR_NONE) {
+            reportError("GetPhase failed", err);
+            break;
+        }
+
+        err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);
+        if (err != JVMTI_ERROR_NONE) {
+            reportError("GetPotentialCapabilities failed", err);
+            break;
+        }
+
+        result = caps.can_generate_all_class_hook_events ? Yes : No;
+
+        printf("isClassHookAvail: phase=%d, value=%d\n", phase, result);
+    } while (0);
+    return result;
+}
+
+
+JNIEXPORT jint JNICALL
+Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)
+{
+    jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_9);
+    if (res != JNI_OK || jvmti == NULL) {
+        reportError("GetEnv failed", res);
+        return JNI_ERR;
+    }
+
+    // check and save can_generate_all_class_hook_events for ONLOAD phase
+    onLoadIsAvail = isClassHookAvail();
+
+    return JNI_OK;
+}
+
+JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
+    return JNI_VERSION_9;
+}
+
+
+JNIEXPORT jint JNICALL
+Java_CanGenerateAllClassHook_getClassHookAvail(JNIEnv *env, jclass cls) {
+    return isClassHookAvail();
+}
+
+JNIEXPORT jint JNICALL
+Java_CanGenerateAllClassHook_getOnLoadClassHookAvail(JNIEnv *env, jclass cls) {
+    return onLoadIsAvail;
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+