annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/SetJNIFunctionTable/setjniftab002/setjniftab002.cpp @ 52515:1ebe04845112

8210665: Clean up JNI_ENV_ARG and factorize the macros for vmTestbase/jvmti[R-U] tests Summary: Remove JNI_ENV and JVMTI_ENV macros from jvmti/[R-U] tests Reviewed-by: cjplummer, sspitsyn
author jcbeyler
date Thu, 13 Sep 2018 13:03:50 -0700
parents e409244ce72e
children cdfabab3413f
rev   line source
iignatyev@50844 1 /*
iignatyev@50844 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
iignatyev@50844 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
iignatyev@50844 4 *
iignatyev@50844 5 * This code is free software; you can redistribute it and/or modify it
iignatyev@50844 6 * under the terms of the GNU General Public License version 2 only, as
iignatyev@50844 7 * published by the Free Software Foundation.
iignatyev@50844 8 *
iignatyev@50844 9 * This code is distributed in the hope that it will be useful, but WITHOUT
iignatyev@50844 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
iignatyev@50844 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
iignatyev@50844 12 * version 2 for more details (a copy is included in the LICENSE file that
iignatyev@50844 13 * accompanied this code).
iignatyev@50844 14 *
iignatyev@50844 15 * You should have received a copy of the GNU General Public License version
iignatyev@50844 16 * 2 along with this work; if not, write to the Free Software Foundation,
iignatyev@50844 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
iignatyev@50844 18 *
iignatyev@50844 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
iignatyev@50844 20 * or visit www.oracle.com if you need additional information or have any
iignatyev@50844 21 * questions.
iignatyev@50844 22 */
iignatyev@50844 23
iignatyev@50844 24 #include <stdio.h>
iignatyev@50844 25 #include <string.h>
iignatyev@50844 26 #include "jvmti.h"
iignatyev@50844 27 #include "agent_common.h"
iignatyev@50844 28 #include "JVMTITools.h"
iignatyev@50844 29
iignatyev@50844 30 extern "C" {
iignatyev@50844 31
iignatyev@50844 32 #define PASSED 0
iignatyev@50844 33 #define STATUS_FAILED 2
iignatyev@50844 34
iignatyev@50844 35 static JavaVM *vm;
iignatyev@50844 36 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 37
iignatyev@50844 38 static int verbose = 0;
iignatyev@50844 39 static jint result = PASSED;
iignatyev@50844 40
iignatyev@50844 41 void redirect(JNIEnv *env, jvmtiError exError) {
iignatyev@50844 42 jvmtiError err;
iignatyev@50844 43
iignatyev@50844 44 if (verbose)
iignatyev@50844 45 printf("\ntrying to overwrite the JNI function table expecting the error %s to be returned ...\n",
iignatyev@50844 46 TranslateError(exError));
iignatyev@50844 47
iignatyev@52294 48 if ((err = jvmti->SetJNIFunctionTable(NULL)) !=
iignatyev@50844 49 exError) {
iignatyev@50844 50 result = STATUS_FAILED;
iignatyev@50844 51 printf("(%s,%d): TEST FAILED: SetJNIFunctionTable() returns %s instead of %s as expected\n",
iignatyev@50844 52 __FILE__, __LINE__, TranslateError(err), TranslateError(exError));
iignatyev@50844 53 return;
iignatyev@50844 54 }
iignatyev@50844 55 else if (verbose)
iignatyev@50844 56 printf("CHECK PASSED: SetJNIFunctionTable() returns %s as expected\n",
iignatyev@50844 57 TranslateError(err));
iignatyev@50844 58 }
iignatyev@50844 59
iignatyev@50844 60 JNIEXPORT jint JNICALL
iignatyev@50844 61 Java_nsk_jvmti_SetJNIFunctionTable_setjniftab002_check(JNIEnv *env, jobject obj) {
iignatyev@52294 62 jint res;
iignatyev@50844 63 JNIEnv *nextEnv = NULL;
iignatyev@50844 64
iignatyev@50844 65 if (jvmti == NULL) {
iignatyev@50844 66 printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
iignatyev@50844 67 __FILE__, __LINE__);
iignatyev@50844 68 return STATUS_FAILED;
iignatyev@50844 69 }
iignatyev@50844 70
iignatyev@50844 71 /* a) Verifying the error JVMTI_ERROR_NULL_POINTER */
iignatyev@50844 72 if (verbose)
iignatyev@50844 73 printf("\na) Checking the function with the parameter JVMTI_ERROR_NULL_POINTER ...\n");
iignatyev@50844 74 redirect(env, JVMTI_ERROR_NULL_POINTER);
iignatyev@50844 75
iignatyev@50844 76 /* b) Verifying the error JVMTI_ERROR_UNATTACHED_THREAD
iignatyev@50844 77 Note: the JNI spec says that the main thread can be detached from the VM
iignatyev@50844 78 only since JDK 1.2 */
iignatyev@50844 79 if (verbose)
iignatyev@50844 80 printf("\nb) Checking the function with the detached thread ...\n\ndetaching the main thread ...\n");
jcbeyler@52515 81 if ((res = vm->DetachCurrentThread()) != JNI_OK)
iignatyev@50844 82 printf("(%s,%d): Warning: DetachCurrentThread() returns: %d\n\
iignatyev@50844 83 \tcheck with the detached main thread skipped\n",
iignatyev@52294 84 __FILE__, __LINE__, res);
iignatyev@50844 85 else {
iignatyev@50844 86 redirect(env, JVMTI_ERROR_UNATTACHED_THREAD);
iignatyev@50844 87
iignatyev@50844 88 if (verbose)
iignatyev@50844 89 printf("\nattaching the main thread back ...\n");
jcbeyler@52515 90 if ((res = vm->AttachCurrentThread((void **) &nextEnv, (void *) 0)) != JNI_OK) {
iignatyev@50844 91 printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
iignatyev@52294 92 __FILE__, __LINE__, res);
iignatyev@50844 93 return STATUS_FAILED;
iignatyev@50844 94 }
iignatyev@50844 95 }
iignatyev@50844 96
iignatyev@50844 97 return result;
iignatyev@50844 98 }
iignatyev@50844 99
iignatyev@50844 100 #ifdef STATIC_BUILD
iignatyev@50844 101 JNIEXPORT jint JNICALL Agent_OnLoad_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 102 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 103 }
iignatyev@50844 104 JNIEXPORT jint JNICALL Agent_OnAttach_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 105 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 106 }
iignatyev@50844 107 JNIEXPORT jint JNI_OnLoad_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 108 return JNI_VERSION_1_8;
iignatyev@50844 109 }
iignatyev@50844 110 #endif
iignatyev@50844 111 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 112 jint res;
iignatyev@50844 113
iignatyev@50844 114 if (options != NULL && strcmp(options, "-verbose") == 0)
iignatyev@50844 115 verbose = 1;
iignatyev@50844 116
iignatyev@50844 117 if (verbose)
iignatyev@50844 118 printf("verbose mode on\n");
iignatyev@50844 119
jcbeyler@52515 120 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 121 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 122 printf("(%s,%d): Failed to call GetEnv\n", __FILE__, __LINE__);
iignatyev@50844 123 return JNI_ERR;
iignatyev@50844 124 }
iignatyev@50844 125
iignatyev@50844 126 vm = jvm;
iignatyev@50844 127
iignatyev@50844 128 return JNI_OK;
iignatyev@50844 129 }
iignatyev@50844 130
iignatyev@50844 131 }