1 /*
2  * Copyright (c) 2007, 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 <jni.h>
25 #include <stdio.h>
26 #include "jni_tools.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define FIND_CLASS(_class, _className)\
33     if (!NSK_JNI_VERIFY(env, (_class = \
34             NSK_CPP_STUB2(FindClass, env, _className)) != NULL))\
35         return
36 
37 #define GET_OBJECT_CLASS(_class, _obj)\
38     if (!NSK_JNI_VERIFY(env, (_class = \
39             NSK_CPP_STUB2(GetObjectClass, env, _obj)) != NULL))\
40         return
41 
42 #define GET_STATIC_FIELD_ID(_fieldID, _class, _fieldName, _fieldSig)\
43     if (!NSK_JNI_VERIFY(env, (_fieldID = \
44             NSK_CPP_STUB4(GetStaticFieldID, env, _class,\
45                 _fieldName, _fieldSig)) != NULL))\
46         return
47 
48 #define GET_STATIC_OBJ_FIELD(_value, _class, _fieldName, _fieldSig)\
49     GET_STATIC_FIELD_ID(field, _class, _fieldName, _fieldSig);\
50     _value = NSK_CPP_STUB3(GetStaticObjectField, env, _class, \
51                                 field)
52 
53 #define GET_STATIC_BOOL_FIELD(_value, _class, _fieldName)\
54     GET_STATIC_FIELD_ID(field, _class, _fieldName, "Z");\
55     _value = NSK_CPP_STUB3(GetStaticBooleanField, env, _class, field)
56 
57 #define GET_FIELD_ID(_fieldID, _class, _fieldName, _fieldSig)\
58     if (!NSK_JNI_VERIFY(env, (_fieldID = \
59             NSK_CPP_STUB4(GetFieldID, env, _class,\
60                 _fieldName, _fieldSig)) != NULL))\
61         return
62 
63 #define GET_INT_FIELD(_value, _obj, _class, _fieldName)\
64     GET_FIELD_ID(field, _class, _fieldName, "I");\
65     _value = NSK_CPP_STUB3(GetIntField, env, _obj, field)
66 
67 #define GET_LONG_FIELD(_value, _obj, _class, _fieldName)\
68     GET_FIELD_ID(field, _class, _fieldName, "J");\
69     _value = NSK_CPP_STUB3(GetLongField, env, _obj, field)
70 
71 #define GET_STATIC_INT_FIELD(_value, _class, _fieldName)\
72     GET_STATIC_FIELD_ID(field, _class, _fieldName, "I");\
73     _value = NSK_CPP_STUB3(GetStaticIntField, env, _class, field)
74 
75 #define SET_INT_FIELD(_obj, _class, _fieldName, _newValue)\
76     GET_FIELD_ID(field, _class, _fieldName, "I");\
77     NSK_CPP_STUB4(SetIntField, env, _obj, field, _newValue)
78 
79 #define GET_OBJ_FIELD(_value, _obj, _class, _fieldName, _fieldSig)\
80     GET_FIELD_ID(field, _class, _fieldName, _fieldSig);\
81     _value = NSK_CPP_STUB3(GetObjectField, env, _obj, field)
82 
83 
84 #define GET_ARR_ELEMENT(_arr, _index)\
85     NSK_CPP_STUB3(GetObjectArrayElement, env, _arr, _index)
86 
87 #define SET_ARR_ELEMENT(_arr, _index, _newValue)\
88     NSK_CPP_STUB4(SetObjectArrayElement, env, _arr, _index, _newValue)
89 
90 #define GET_STATIC_METHOD_ID(_methodID, _class, _methodName, _sig)\
91     if (!NSK_JNI_VERIFY(env, (_methodID = \
92             NSK_CPP_STUB4(GetStaticMethodID, env, _class,\
93                 _methodName, _sig)) != NULL))\
94         return
95 
96 #define GET_METHOD_ID(_methodID, _class, _methodName, _sig)\
97     if (!NSK_JNI_VERIFY(env, (_methodID = \
98             NSK_CPP_STUB4(GetMethodID, env, _class,\
99                 _methodName, _sig)) != NULL))\
100         return
101 
102 #define CALL_STATIC_VOID_NOPARAM(_class, _methodName)\
103     GET_STATIC_METHOD_ID(method, _class, _methodName, "()V");\
104     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB3(CallStaticVoidMethod, env,\
105                             _class, method)))\
106         return
107 
108 #define CALL_STATIC_VOID(_class, _methodName, _sig, _param)\
109     GET_STATIC_METHOD_ID(method, _class, _methodName, _sig);\
110     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB4(CallStaticVoidMethod, env,\
111                                                     _class, method, _param)))\
112         return
113 
114 #define CALL_VOID_NOPARAM(_obj, _class, _methodName)\
115     GET_METHOD_ID(method, _class, _methodName, "()V");\
116     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB3(CallVoidMethod, env, _obj,\
117                                                     method)))\
118         return
119 
120 #define CALL_VOID(_obj, _class, _methodName, _sig, _param)\
121     GET_METHOD_ID(method, _class, _methodName, _sig);\
122     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB4(CallVoidMethod, env, _obj,\
123                                                     method, _param)))\
124         return
125 
126 #define CALL_VOID2(_obj, _class, _methodName, _sig, _param1, _param2)\
127     GET_METHOD_ID(method, _class, _methodName, _sig);\
128     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB5(CallVoidMethod, env, _obj, \
129                                                     method, _param1, _param2)))\
130         return
131 
132 #define CALL_INT_NOPARAM(_value, _obj, _class, _methodName)\
133     GET_METHOD_ID(method, _class, _methodName, "()I");\
134     _value = NSK_CPP_STUB3(CallIntMethod, env, _obj, method)
135 
136 #define NEW_OBJ(_obj, _class, _constructorName, _sig, _params)\
137     GET_METHOD_ID(method, _class, _constructorName, _sig);\
138     if (!NSK_JNI_VERIFY(env, (_obj = \
139             NSK_CPP_STUB4(NewObject, env, _class, method, _params)) != NULL))\
140         return
141 
142 #define MONITOR_ENTER(x) \
143     NSK_JNI_VERIFY(env, NSK_CPP_STUB2(MonitorEnter, env, x) == 0)
144 
145 #define MONITOR_EXIT(x) \
146     NSK_JNI_VERIFY(env, NSK_CPP_STUB2(MonitorExit, env, x) == 0)
147 
148 #define TRACE(msg)\
149    GET_OBJ_FIELD(logger, obj, threadClass, "logger", "Lnsk/share/Log$Logger;");\
150    jmsg = NSK_CPP_STUB2(NewStringUTF, env, msg);\
151    CALL_VOID2(logger, loggerClass, "trace",\
152                            "(ILjava/lang/String;)V", 50, jmsg)
153 
154 
155 /*
156  * Class:     nsk_monitoring_share_thread_RecursiveMonitoringThread
157  * Method:    nativeRecursiveMethod
158  * Signature: (IZ)V
159  */
Java_nsk_monitoring_share_thread_RecursiveMonitoringThread_nativeRecursiveMethod(JNIEnv * env,jobject this,jint currentDepth,jboolean pureNative)160 JNIEXPORT void JNICALL Java_nsk_monitoring_share_thread_RecursiveMonitoringThread_nativeRecursiveMethod
161 (JNIEnv *env, jobject this, jint currentDepth, jboolean pureNative) {
162         jclass class;
163         jmethodID method;
164 
165         GET_OBJECT_CLASS(class, this);
166         if (currentDepth-- > 0) {
167 /*              printf("Current depth: %d\n", currentDepth); */
168                 CALL_STATIC_VOID_NOPARAM(class, "yield");
169                 if (pureNative == JNI_TRUE) {
170                         CALL_VOID2(this, class, "nativeRecursiveMethod", "(IZ)V", currentDepth, pureNative);
171                 } else {
172                         CALL_VOID(this, class, "recursiveMethod", "(I)V", currentDepth);
173                 }
174         } else {
175                 CALL_VOID_NOPARAM(this, class, "runInside");
176         }
177 }
178