1 /*
2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28 #include "JVMTITools.h"
29 
30 extern "C" {
31 
32 
33 #define PASSED 0
34 #define STATUS_FAILED 2
35 
36 typedef struct {
37     jint value;
38     const char *name;
39 } bit_info;
40 
41 static jvmtiEnv *jvmti = NULL;
42 static jint result = PASSED;
43 static jboolean printdump = JNI_FALSE;
44 static bit_info bits[] = {
45     { JVMTI_CLASS_STATUS_INITIALIZED, "JVMTI_CLASS_STATUS_INITIALIZED" },
46     { JVMTI_CLASS_STATUS_ERROR, "JVMTI_CLASS_STATUS_ERROR" }
47 };
48 
49 #ifdef STATIC_BUILD
Agent_OnLoad_getclstat006(JavaVM * jvm,char * options,void * reserved)50 JNIEXPORT jint JNICALL Agent_OnLoad_getclstat006(JavaVM *jvm, char *options, void *reserved) {
51     return Agent_Initialize(jvm, options, reserved);
52 }
Agent_OnAttach_getclstat006(JavaVM * jvm,char * options,void * reserved)53 JNIEXPORT jint JNICALL Agent_OnAttach_getclstat006(JavaVM *jvm, char *options, void *reserved) {
54     return Agent_Initialize(jvm, options, reserved);
55 }
JNI_OnLoad_getclstat006(JavaVM * jvm,char * options,void * reserved)56 JNIEXPORT jint JNI_OnLoad_getclstat006(JavaVM *jvm, char *options, void *reserved) {
57     return JNI_VERSION_1_8;
58 }
59 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)60 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
61     jint res;
62 
63     if (options != NULL && strcmp(options, "printdump") == 0) {
64         printdump = JNI_TRUE;
65     }
66 
67     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
68     if (res != JNI_OK || jvmti == NULL) {
69         printf("Wrong result of a valid call to GetEnv!\n");
70         return JNI_ERR;
71     }
72 
73     return JNI_OK;
74 }
75 
76 JNIEXPORT void JNICALL
Java_nsk_jvmti_GetClassStatus_getclstat006_check(JNIEnv * env,jclass cls,jint i,jclass clazz)77 Java_nsk_jvmti_GetClassStatus_getclstat006_check(JNIEnv *env, jclass cls, jint i, jclass clazz) {
78     jvmtiError err;
79     jint status;
80 
81     if (jvmti == NULL) {
82         printf("JVMTI client was not properly loaded!\n");
83         result = STATUS_FAILED;
84         return;
85     }
86 
87     err = jvmti->GetClassStatus(clazz, &status);
88     if (err != JVMTI_ERROR_NONE) {
89         printf("(GetClassStatus#%d) unexpected error: %s (%d)\n",
90                i, TranslateError(err), err);
91         result = STATUS_FAILED;
92         return;
93     }
94 
95     if (printdump == JNI_TRUE) {
96         printf(">>> %d: status = 0x%0x\n", i, status);
97     }
98 
99     if ((status & bits[i].value) == 0) {
100         printf("(%d) %s bit not set\n", i, bits[i].name);
101         result = STATUS_FAILED;
102     }
103 }
104 
Java_nsk_jvmti_GetClassStatus_getclstat006_getRes(JNIEnv * env,jclass cls)105 JNIEXPORT int JNICALL Java_nsk_jvmti_GetClassStatus_getclstat006_getRes(JNIEnv *env, jclass cls) {
106     return result;
107 }
108 
109 }
110