annotate test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassPrepare/classprep001/classprep001.cpp @ 53610:9cfc8b0c45fd

8214149: Move out assignments when not using NSK*VERIFY macros Summary: Move out the assignments from ifs Reviewed-by: sspitsyn, cjplummer
author jcbeyler
date Wed, 21 Nov 2018 10:46:45 -0800
parents 543a3fb81c4c
children c05ba185a1d3
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@52294 26 #include <inttypes.h>
iignatyev@50844 27 #include "jvmti.h"
iignatyev@50844 28 #include "agent_common.h"
iignatyev@50844 29 #include "JVMTITools.h"
iignatyev@50844 30
iignatyev@50844 31 extern "C" {
iignatyev@50844 32
iignatyev@50844 33
iignatyev@50844 34 #define PASSED 0
iignatyev@50844 35 #define STATUS_FAILED 2
iignatyev@50844 36
iignatyev@50844 37 #define EXP_STATUS (JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED)
iignatyev@50844 38
iignatyev@50844 39 typedef struct {
iignatyev@50844 40 char *sig;
iignatyev@50844 41 jint status;
iignatyev@50844 42 jint mcount;
iignatyev@50844 43 jint fcount;
iignatyev@50844 44 jint icount;
iignatyev@52294 45 } writable_class_info;
iignatyev@52294 46
iignatyev@52294 47 typedef struct {
iignatyev@52294 48 const char *sig;
iignatyev@52294 49 jint status;
iignatyev@52294 50 jint mcount;
iignatyev@52294 51 jint fcount;
iignatyev@52294 52 jint icount;
iignatyev@50844 53 } class_info;
iignatyev@50844 54
iignatyev@50844 55 static jvmtiEnv *jvmti = NULL;
iignatyev@50844 56 static jvmtiEventCallbacks callbacks;
iignatyev@50844 57 static jint result = PASSED;
iignatyev@50844 58 static jboolean printdump = JNI_FALSE;
iignatyev@50844 59 static size_t eventsCount = 0;
iignatyev@50844 60 static size_t eventsExpected = 0;
iignatyev@50844 61 static class_info classes[] = {
iignatyev@50844 62 {"Lnsk/jvmti/ClassPrepare/classprep001$TestInterface;", EXP_STATUS, 2, 1, 0},
iignatyev@50844 63 {"Lnsk/jvmti/ClassPrepare/classprep001$TestClass;", EXP_STATUS, 3, 2, 1}
iignatyev@50844 64 };
iignatyev@50844 65
iignatyev@50844 66 void printStatus(jint status) {
iignatyev@50844 67 int flags = 0;
iignatyev@50844 68 if ((status & JVMTI_CLASS_STATUS_VERIFIED) != 0) {
iignatyev@50844 69 printf("JVMTI_CLASS_STATUS_VERIFIED");
iignatyev@50844 70 flags++;
iignatyev@50844 71 }
iignatyev@50844 72 if ((status & JVMTI_CLASS_STATUS_PREPARED) != 0) {
iignatyev@50844 73 if (flags > 0) printf(" | ");
iignatyev@50844 74 printf("JVMTI_CLASS_STATUS_PREPARED");
iignatyev@50844 75 flags++;
iignatyev@50844 76 }
iignatyev@50844 77 if ((status & JVMTI_CLASS_STATUS_INITIALIZED) != 0) {
iignatyev@50844 78 if (flags > 0) printf(" | ");
iignatyev@50844 79 printf("JVMTI_CLASS_STATUS_INITIALIZED");
iignatyev@50844 80 flags++;
iignatyev@50844 81 }
iignatyev@50844 82 if ((status & JVMTI_CLASS_STATUS_ERROR) != 0) {
iignatyev@50844 83 if (flags > 0) printf(" | ");
iignatyev@50844 84 printf("JVMTI_CLASS_STATUS_ERROR");
iignatyev@50844 85 flags++;
iignatyev@50844 86 }
iignatyev@50844 87 printf(" (0x%x)\n", status);
iignatyev@50844 88 }
iignatyev@50844 89
iignatyev@50844 90 void JNICALL ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env,
iignatyev@50844 91 jthread thr, jclass cls) {
iignatyev@50844 92 jvmtiError err;
iignatyev@52294 93 writable_class_info inf;
iignatyev@50844 94 jmethodID *methods;
iignatyev@50844 95 jfieldID *fields;
iignatyev@50844 96 jclass *interfaces;
iignatyev@50844 97 char *name, *sig, *generic;
iignatyev@50844 98 int i;
iignatyev@50844 99
iignatyev@52294 100 err = jvmti_env->GetClassSignature(cls, &inf.sig, &generic);
iignatyev@50844 101 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 102 printf("(GetClassSignature#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 103 eventsCount, TranslateError(err), err);
iignatyev@50844 104 result = STATUS_FAILED;
iignatyev@50844 105 }
iignatyev@52294 106 err = jvmti_env->GetClassStatus(cls, &inf.status);
iignatyev@50844 107 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 108 printf("(GetClassStatus#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 109 eventsCount, TranslateError(err), err);
iignatyev@50844 110 result = STATUS_FAILED;
iignatyev@50844 111 }
iignatyev@52294 112 err = jvmti_env->GetClassMethods(cls, &inf.mcount, &methods);
iignatyev@50844 113 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 114 printf("(GetClassMethods#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 115 eventsCount, TranslateError(err), err);
iignatyev@50844 116 result = STATUS_FAILED;
iignatyev@50844 117 }
iignatyev@52294 118 err = jvmti_env->GetClassFields(cls, &inf.fcount, &fields);
iignatyev@50844 119 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 120 printf("(GetClassMethods#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 121 eventsCount, TranslateError(err), err);
iignatyev@50844 122 result = STATUS_FAILED;
iignatyev@50844 123 }
iignatyev@52294 124 err = jvmti_env->GetImplementedInterfaces(cls,
iignatyev@50844 125 &inf.icount, &interfaces);
iignatyev@50844 126 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 127 printf("(GetImplementedInterfaces#%" PRIuPTR ") unexpected error: %s (%d)\n",
iignatyev@50844 128 eventsCount, TranslateError(err), err);
iignatyev@50844 129 result = STATUS_FAILED;
iignatyev@50844 130 }
iignatyev@50844 131
iignatyev@50844 132 if (printdump == JNI_TRUE) {
iignatyev@50844 133 printf(">>> [class prepare event #%" PRIuPTR "]", eventsCount);
iignatyev@50844 134 printf(" \"%s\"\n", inf.sig);
iignatyev@50844 135 printf(">>> status: ");
iignatyev@50844 136 printStatus(inf.status);
iignatyev@50844 137 printf(">>> %d methods:", inf.mcount);
iignatyev@50844 138 for (i = 0; i < inf.mcount; i++) {
iignatyev@50844 139 if (i > 0) printf(",");
iignatyev@50844 140 if (methods[i] == NULL) {
iignatyev@50844 141 printf(" null");
iignatyev@50844 142 } else {
iignatyev@52294 143 err = jvmti_env->GetMethodName(methods[i],
iignatyev@50844 144 &name, &sig, &generic);
iignatyev@50844 145 if (err == JVMTI_ERROR_NONE) {
iignatyev@50844 146 printf(" \"%s%s\"", name, sig);
iignatyev@50844 147 } else {
iignatyev@50844 148 printf(" ???");
iignatyev@50844 149 }
iignatyev@50844 150 }
iignatyev@50844 151 }
iignatyev@50844 152 printf("\n");
iignatyev@50844 153 printf(">>> %d fields:", inf.fcount);
iignatyev@50844 154 for (i = 0; i < inf.fcount; i++) {
iignatyev@50844 155 if (i > 0) printf(",");
iignatyev@50844 156 if (fields[i] == NULL) {
iignatyev@50844 157 printf(" null");
iignatyev@50844 158 } else {
iignatyev@52294 159 err = jvmti_env->GetFieldName(cls, fields[i],
iignatyev@50844 160 &name, &sig, &generic);
iignatyev@50844 161 if (err == JVMTI_ERROR_NONE) {
iignatyev@50844 162 printf(" \"%s, %s\"", name, sig);
iignatyev@50844 163 } else {
iignatyev@50844 164 printf(" ???");
iignatyev@50844 165 }
iignatyev@50844 166 }
iignatyev@50844 167 }
iignatyev@50844 168 printf("\n");
iignatyev@50844 169 printf(">>> %d interfaces:", inf.icount);
iignatyev@50844 170 for (i = 0; i < inf.icount; i++) {
iignatyev@50844 171 if (i > 0) printf(",");
iignatyev@50844 172 if (interfaces[i] == NULL) {
iignatyev@50844 173 printf(" null");
iignatyev@50844 174 } else {
iignatyev@52294 175 err = jvmti_env->GetClassSignature(
iignatyev@50844 176 interfaces[i], &sig, &generic);
iignatyev@50844 177 if (err == JVMTI_ERROR_NONE) {
iignatyev@50844 178 printf(" \"%s\"", sig);
iignatyev@50844 179 } else {
iignatyev@50844 180 printf(" ???");
iignatyev@50844 181 }
iignatyev@50844 182 }
iignatyev@50844 183 }
iignatyev@50844 184 printf("\n");
iignatyev@50844 185 }
iignatyev@50844 186
iignatyev@50844 187 if (eventsCount >= eventsExpected) {
iignatyev@50844 188 printf("(#%" PRIuPTR ") too many events: %" PRIuPTR ", expected: %" PRIuPTR "\n",
iignatyev@50844 189 eventsCount, eventsCount + 1, eventsExpected);
iignatyev@50844 190 result = STATUS_FAILED;
iignatyev@50844 191 return;
iignatyev@50844 192 }
iignatyev@50844 193
iignatyev@50844 194 if (inf.sig == NULL || strcmp(inf.sig, classes[eventsCount].sig) != 0) {
iignatyev@50844 195 printf("(#%" PRIuPTR ") wrong class: \"%s\"",
iignatyev@50844 196 eventsCount, inf.sig);
iignatyev@50844 197 printf(", expected: \"%s\"\n", classes[eventsCount].sig);
iignatyev@50844 198 result = STATUS_FAILED;
iignatyev@50844 199 }
iignatyev@50844 200 if (inf.status != classes[eventsCount].status) {
iignatyev@50844 201 printf("(#%" PRIuPTR ") wrong status: ", eventsCount);
iignatyev@50844 202 printStatus(inf.status);
iignatyev@50844 203 printf(" expected: ");
iignatyev@50844 204 printStatus(classes[eventsCount].status);
iignatyev@50844 205 result = STATUS_FAILED;
iignatyev@50844 206 }
iignatyev@50844 207 if (inf.mcount != classes[eventsCount].mcount) {
iignatyev@50844 208 printf("(#%" PRIuPTR ") wrong number of methods: 0x%x",
iignatyev@50844 209 eventsCount, inf.mcount);
iignatyev@50844 210 printf(", expected: 0x%x\n", classes[eventsCount].mcount);
iignatyev@50844 211 result = STATUS_FAILED;
iignatyev@50844 212 }
iignatyev@50844 213 if (inf.fcount != classes[eventsCount].fcount) {
iignatyev@50844 214 printf("(#%" PRIuPTR ") wrong number of fields: 0x%x",
iignatyev@50844 215 eventsCount, inf.fcount);
iignatyev@50844 216 printf(", expected: 0x%x\n", classes[eventsCount].fcount);
iignatyev@50844 217 result = STATUS_FAILED;
iignatyev@50844 218 }
iignatyev@50844 219 if (inf.icount != classes[eventsCount].icount) {
iignatyev@50844 220 printf("(#%" PRIuPTR ") wrong number of interfaces: 0x%x",
iignatyev@50844 221 eventsCount, inf.icount);
iignatyev@50844 222 printf(", expected: 0x%x\n", classes[eventsCount].icount);
iignatyev@50844 223 result = STATUS_FAILED;
iignatyev@50844 224 }
iignatyev@50844 225 eventsCount++;
iignatyev@50844 226 }
iignatyev@50844 227
iignatyev@50844 228 #ifdef STATIC_BUILD
iignatyev@50844 229 JNIEXPORT jint JNICALL Agent_OnLoad_classprep001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 230 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 231 }
iignatyev@50844 232 JNIEXPORT jint JNICALL Agent_OnAttach_classprep001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 233 return Agent_Initialize(jvm, options, reserved);
iignatyev@50844 234 }
iignatyev@50844 235 JNIEXPORT jint JNI_OnLoad_classprep001(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 236 return JNI_VERSION_1_8;
iignatyev@50844 237 }
iignatyev@50844 238 #endif
iignatyev@50844 239 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
iignatyev@50844 240 jvmtiError err;
iignatyev@50844 241 jint res;
iignatyev@50844 242
iignatyev@50844 243 if (options != NULL && strcmp(options, "printdump") == 0) {
iignatyev@50844 244 printdump = JNI_TRUE;
iignatyev@50844 245 }
iignatyev@50844 246
jcbeyler@52467 247 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
iignatyev@50844 248 if (res != JNI_OK || jvmti == NULL) {
iignatyev@50844 249 printf("Wrong result of a valid call to GetEnv!\n");
iignatyev@50844 250 return JNI_ERR;
iignatyev@50844 251 }
iignatyev@50844 252
iignatyev@50844 253 callbacks.ClassPrepare = &ClassPrepare;
iignatyev@52294 254 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
iignatyev@50844 255 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 256 printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
iignatyev@50844 257 TranslateError(err), err);
iignatyev@50844 258 return JNI_ERR;
iignatyev@50844 259 }
iignatyev@50844 260
iignatyev@50844 261 return JNI_OK;
iignatyev@50844 262 }
iignatyev@50844 263
iignatyev@50844 264 JNIEXPORT void JNICALL
iignatyev@50844 265 Java_nsk_jvmti_ClassPrepare_classprep001_getReady(JNIEnv *env, jclass cls) {
iignatyev@50844 266 jvmtiError err;
iignatyev@50844 267 jthread prep_thread;
iignatyev@50844 268
iignatyev@50844 269 if (jvmti == NULL) {
iignatyev@50844 270 printf("JVMTI client was not properly loaded!\n");
iignatyev@50844 271 return;
iignatyev@50844 272 }
iignatyev@50844 273
jcbeyler@53610 274 err = jvmti->GetCurrentThread(&prep_thread);
jcbeyler@53610 275 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 276 printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
iignatyev@50844 277 result = STATUS_FAILED;
iignatyev@50844 278 return;
iignatyev@50844 279 }
iignatyev@50844 280
iignatyev@52294 281 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
iignatyev@50844 282 JVMTI_EVENT_CLASS_PREPARE, prep_thread);
iignatyev@50844 283 if (err == JVMTI_ERROR_NONE) {
iignatyev@50844 284 eventsExpected = sizeof(classes)/sizeof(class_info);
iignatyev@50844 285 } else {
iignatyev@50844 286 printf("Failed to enable JVMTI_EVENT_CLASS_PREPARE: %s (%d)\n",
iignatyev@50844 287 TranslateError(err), err);
iignatyev@50844 288 result = STATUS_FAILED;
iignatyev@50844 289 }
iignatyev@50844 290 }
iignatyev@50844 291
iignatyev@50844 292 JNIEXPORT jint JNICALL
iignatyev@50844 293 Java_nsk_jvmti_ClassPrepare_classprep001_check(JNIEnv *env, jclass cls) {
iignatyev@50844 294 jvmtiError err;
iignatyev@50844 295 jthread prep_thread;
iignatyev@50844 296
iignatyev@50844 297 if (jvmti == NULL) {
iignatyev@50844 298 printf("JVMTI client was not properly loaded!\n");
iignatyev@50844 299 return STATUS_FAILED;
iignatyev@50844 300 }
iignatyev@50844 301
jcbeyler@53610 302 err = jvmti->GetCurrentThread(&prep_thread);
jcbeyler@53610 303 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 304 printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
iignatyev@50844 305 return STATUS_FAILED;
iignatyev@50844 306 }
iignatyev@50844 307
iignatyev@52294 308 err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
iignatyev@50844 309 JVMTI_EVENT_CLASS_PREPARE, prep_thread);
iignatyev@50844 310 if (err != JVMTI_ERROR_NONE) {
iignatyev@50844 311 printf("Failed to disable JVMTI_EVENT_CLASS_PREPARE: %s (%d)\n",
iignatyev@50844 312 TranslateError(err), err);
iignatyev@50844 313 result = STATUS_FAILED;
iignatyev@50844 314 }
iignatyev@50844 315
iignatyev@50844 316 if (eventsCount != eventsExpected) {
iignatyev@50844 317 printf("Wrong number of class prepare events: %" PRIuPTR ", expected: %" PRIuPTR "\n",
iignatyev@50844 318 eventsCount, eventsExpected);
iignatyev@50844 319 result = STATUS_FAILED;
iignatyev@50844 320 }
iignatyev@50844 321 return result;
iignatyev@50844 322 }
iignatyev@50844 323
iignatyev@50844 324 }