annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/SetJNIFunctionTable/setjniftab002/setjniftab002.cpp @ 52294:e409244ce72e

8209611: use C++ compiler for hotspot tests Reviewed-by: vlivanov, ihse, erikj, jcbeyler
author iignatyev
date Tue, 28 Aug 2018 14:37:34 -0700
parents test/hotspot/jtreg/vmTestbase/nsk/jvmti/SetJNIFunctionTable/setjniftab002/setjniftab002.c@46c67f5e27c2
children 1ebe04845112
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 #ifdef __cplusplus
iignatyev@50844 31 extern "C" {
iignatyev@50844 32 #endif
iignatyev@50844 33
iignatyev@50844 34 #ifndef JNI_ENV_ARG
iignatyev@50844 35 #ifdef __cplusplus
iignatyev@50844 36 #define JNI_ENV_ARG(x, y) y
iignatyev@50844 37 #define JNI_ENV_PTR(x) x
iignatyev@50844 38 #else
iignatyev@50844 39 #define JNI_ENV_ARG(x, y) x, y
iignatyev@50844 40 #define JNI_ENV_PTR(x) (*x)
iignatyev@50844 41 #endif
iignatyev@50844 42 #endif
iignatyev@50844 43
iignatyev@50844 44 #ifndef JNI_ENV_ARG1
iignatyev@50844 45 #ifdef __cplusplus
iignatyev@50844 46 #define JNI_ENV_ARG1(x)
iignatyev@50844 47 #else
iignatyev@50844 48 #define JNI_ENV_ARG1(x) x
iignatyev@50844 49 #endif
iignatyev@50844 50 #endif
iignatyev@50844 51
iignatyev@50844 52 #define PASSED 0
iignatyev@50844 53 #define STATUS_FAILED 2
iignatyev@50844 54
iignatyev@50844 55 static JavaVM *vm;
iignatyev@50844 56 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 57
iignatyev@50844 58 static int verbose = 0;
iignatyev@50844 59 static jint result = PASSED;
iignatyev@50844 60
iignatyev@50844 61 void redirect(JNIEnv *env, jvmtiError exError) {
iignatyev@50844 62 jvmtiError err;
iignatyev@50844 63
iignatyev@50844 64 if (verbose)
iignatyev@50844 65 printf("\ntrying to overwrite the JNI function table expecting the error %s to be returned ...\n",
iignatyev@50844 66 TranslateError(exError));
iignatyev@50844 67
iignatyev@52294 68 if ((err = jvmti->SetJNIFunctionTable(NULL)) !=
iignatyev@50844 69 exError) {
iignatyev@50844 70 result = STATUS_FAILED;
iignatyev@50844 71 printf("(%s,%d): TEST FAILED: SetJNIFunctionTable() returns %s instead of %s as expected\n",
iignatyev@50844 72 __FILE__, __LINE__, TranslateError(err), TranslateError(exError));
iignatyev@50844 73 return;
iignatyev@50844 74 }
iignatyev@50844 75 else if (verbose)
iignatyev@50844 76 printf("CHECK PASSED: SetJNIFunctionTable() returns %s as expected\n",
iignatyev@50844 77 TranslateError(err));
iignatyev@50844 78 }
iignatyev@50844 79
iignatyev@50844 80 JNIEXPORT jint JNICALL
iignatyev@50844 81 Java_nsk_jvmti_SetJNIFunctionTable_setjniftab002_check(JNIEnv *env, jobject obj) {
iignatyev@52294 82 jint res;
iignatyev@50844 83 JNIEnv *nextEnv = NULL;
iignatyev@50844 84
iignatyev@50844 85 if (jvmti == NULL) {
iignatyev@50844 86 printf("(%s,%d): TEST FAILURE: JVMTI client was not properly loaded\n",
iignatyev@50844 87 __FILE__, __LINE__);
iignatyev@50844 88 return STATUS_FAILED;
iignatyev@50844 89 }
iignatyev@50844 90
iignatyev@50844 91 /* a) Verifying the error JVMTI_ERROR_NULL_POINTER */
iignatyev@50844 92 if (verbose)
iignatyev@50844 93 printf("\na) Checking the function with the parameter JVMTI_ERROR_NULL_POINTER ...\n");
iignatyev@50844 94 redirect(env, JVMTI_ERROR_NULL_POINTER);
iignatyev@50844 95
iignatyev@50844 96 /* b) Verifying the error JVMTI_ERROR_UNATTACHED_THREAD
iignatyev@50844 97 Note: the JNI spec says that the main thread can be detached from the VM
iignatyev@50844 98 only since JDK 1.2 */
iignatyev@50844 99 if (verbose)
iignatyev@50844 100 printf("\nb) Checking the function with the detached thread ...\n\ndetaching the main thread ...\n");
iignatyev@52294 101 if ((res =
iignatyev@50844 102 JNI_ENV_PTR(vm)->DetachCurrentThread(
iignatyev@52294 103 JNI_ENV_ARG1(vm))) != JNI_OK)
iignatyev@50844 104 printf("(%s,%d): Warning: DetachCurrentThread() returns: %d\n\
iignatyev@50844 105 \tcheck with the detached main thread skipped\n",
iignatyev@52294 106 __FILE__, __LINE__, res);
iignatyev@50844 107 else {
iignatyev@50844 108 redirect(env, JVMTI_ERROR_UNATTACHED_THREAD);
iignatyev@50844 109
iignatyev@50844 110 if (verbose)
iignatyev@50844 111 printf("\nattaching the main thread back ...\n");
iignatyev@52294 112 if ((res =
iignatyev@50844 113 JNI_ENV_PTR(vm)->AttachCurrentThread(
iignatyev@52294 114 JNI_ENV_ARG(vm, (void **) &nextEnv), (void *) 0)) != JNI_OK) {
iignatyev@50844 115 printf("(%s,%d): TEST FAILURE: waitingThread: AttachCurrentThread() returns: %d\n",
iignatyev@52294 116 __FILE__, __LINE__, res);
iignatyev@50844 117 return STATUS_FAILED;
iignatyev@50844 118 }
iignatyev@50844 119 }
iignatyev@50844 120
iignatyev@50844 121 return result;
iignatyev@50844 122 }
iignatyev@50844 123
iignatyev@50844 124 #ifdef STATIC_BUILD
iignatyev@50844 125 JNIEXPORT jint JNICALL Agent_OnLoad_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 126 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 127 }
iignatyev@50844 128 JNIEXPORT jint JNICALL Agent_OnAttach_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 129 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 130 }
iignatyev@50844 131 JNIEXPORT jint JNI_OnLoad_setjniftab002(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 132 return JNI_VERSION_1_8;
iignatyev@50844 133 }
iignatyev@50844 134 #endif
iignatyev@50844 135 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 136 jint res;
iignatyev@50844 137
iignatyev@50844 138 if (options != NULL && strcmp(options, "-verbose") == 0)
iignatyev@50844 139 verbose = 1;
iignatyev@50844 140
iignatyev@50844 141 if (verbose)
iignatyev@50844 142 printf("verbose mode on\n");
iignatyev@50844 143
iignatyev@50844 144 res = JNI_ENV_PTR(jvm)->
iignatyev@50844 145 GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
iignatyev@50844 146 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 147 printf("(%s,%d): Failed to call GetEnv\n", __FILE__, __LINE__);
iignatyev@50844 148 return JNI_ERR;
iignatyev@50844 149 }
iignatyev@50844 150
iignatyev@50844 151 vm = jvm;
iignatyev@50844 152
iignatyev@50844 153 return JNI_OK;
iignatyev@50844 154 }
iignatyev@50844 155
iignatyev@50844 156 #ifdef __cplusplus
iignatyev@50844 157 }
iignatyev@50844 158 #endif