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 <stdlib.h>
  25 #include "jni_tools.h"
  26 
  27 #ifndef _IS_NSK_STRACE_DEFINED_
  28 #define _IS_NSK_STRACE_DEFINED_
  29 
  30 #define JNI_VERSION JNI_VERSION_1_1
  31 
  32 #define EXCEPTION_CLEAR NSK_CPP_STUB1(ExceptionClear, env)
  33 #define EXCEPTION_OCCURRED NSK_CPP_STUB1(ExceptionOccurred, env)
  34 
  35 // Check for pending exception of the specified type
  36 // If it's present, then clear it
  37 #define EXCEPTION_CHECK(exceptionClass, recurDepth) \
  38         if (EXCEPTION_OCCURRED != NULL) { \
  39             jobject exception = EXCEPTION_OCCURRED; \
  40             if (NSK_CPP_STUB3(IsInstanceOf, env, exception, \
  41                         exceptionClass) == JNI_TRUE) { \
  42                 EXCEPTION_CLEAR; \
  43                 NSK_DISPLAY1("StackOverflowError occurred at depth %d\n", recurDepth); \
  44             } \
  45         }
  46 
  47 #define FIND_CLASS(_class, _className)\
  48     if (!NSK_JNI_VERIFY(env, (_class = \
  49             NSK_CPP_STUB2(FindClass, env, _className)) != NULL))\
  50         exit(1)
  51 
  52 #define GET_OBJECT_CLASS(_class, _obj)\
  53     if (!NSK_JNI_VERIFY(env, (_class = \
  54             NSK_CPP_STUB2(GetObjectClass, env, _obj)) != NULL))\
  55         exit(1)
  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         exit(1)
  62 
  63 #define GET_STATIC_FIELD_ID(_fieldID, _class, _fieldName, _fieldSig)\
  64     if (!NSK_JNI_VERIFY(env, (_fieldID = \
  65             NSK_CPP_STUB4(GetStaticFieldID, env, _class,\
  66                 _fieldName, _fieldSig)) != NULL))\
  67         exit(1)
  68 
  69 #define GET_STATIC_BOOL_FIELD(_value, _class, _fieldName)\
  70     GET_STATIC_FIELD_ID(field, _class, _fieldName, "Z");\
  71     _value = NSK_CPP_STUB3(GetStaticBooleanField, env, _class, field)
  72 
  73 #define GET_STATIC_INT_FIELD(_value, _class, _fieldName)\
  74     GET_STATIC_FIELD_ID(field, _class, _fieldName, "I");\
  75     _value = NSK_CPP_STUB3(GetStaticIntField, env, _class, field)
  76 
  77 #define GET_STATIC_OBJ_FIELD(_value, _class, _fieldName, _fieldSig)\
  78     GET_STATIC_FIELD_ID(field, _class, _fieldName, _fieldSig);\
  79     _value = NSK_CPP_STUB3(GetStaticObjectField, env, _class, \
  80                                 field)
  81 
  82 #define GET_INT_FIELD(_value, _obj, _class, _fieldName)\
  83     GET_FIELD_ID(field, _class, _fieldName, "I");\
  84     _value = NSK_CPP_STUB3(GetIntField, env, _obj, field)
  85 
  86 #define SET_INT_FIELD(_obj, _class, _fieldName, _newValue)\
  87     GET_FIELD_ID(field, _class, _fieldName, "I");\
  88     NSK_CPP_STUB4(SetIntField, env, _obj, field, _newValue)
  89 
  90 #define SET_STATIC_INT_FIELD(_class, _fieldName, _newValue)\
  91     GET_STATIC_FIELD_ID(field, _class, _fieldName, "I");\
  92     NSK_CPP_STUB4(SetStaticIntField, env, _class, field, _newValue)
  93 
  94 #define GET_OBJ_FIELD(_value, _obj, _class, _fieldName, _fieldSig)\
  95     GET_FIELD_ID(field, _class, _fieldName, _fieldSig);\
  96     _value = NSK_CPP_STUB3(GetObjectField, env, _obj, field)
  97 
  98 #define GET_STATIC_METHOD_ID(_methodID, _class, _methodName, _sig)\
  99     if (!NSK_JNI_VERIFY(env, (_methodID = \
 100             NSK_CPP_STUB4(GetStaticMethodID, env, _class,\
 101                 _methodName, _sig)) != NULL))\
 102         exit(1)
 103 
 104 #define GET_METHOD_ID(_methodID, _class, _methodName, _sig)\
 105     if (!NSK_JNI_VERIFY(env, (_methodID = \
 106             NSK_CPP_STUB4(GetMethodID, env, _class,\
 107                 _methodName, _sig)) != NULL))\
 108         exit(1)
 109 
 110 #define CALL_STATIC_VOID_NOPARAM(_class, _methodName)\
 111     GET_STATIC_METHOD_ID(method, _class, _methodName, "()V");\
 112     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB3(CallStaticVoidMethod, env,\
 113                             _class, method)))\
 114         exit(1)
 115 
 116 #define CALL_STATIC_VOID(_class, _methodName, _sig, _param)\
 117     GET_STATIC_METHOD_ID(method, _class, _methodName, _sig);\
 118     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB4(CallStaticVoidMethod, env,\
 119                                                     _class, method, _param)))\
 120         exit(1)
 121 
 122 #define CALL_VOID_NOPARAM(_obj, _class, _methodName)\
 123     GET_METHOD_ID(method, _class, _methodName, "()V");\
 124     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB3(CallVoidMethod, env, _obj,\
 125                                                     method)))\
 126         exit(1)
 127 
 128 #define CALL_VOID(_obj, _class, _methodName, _sig, _param)\
 129     GET_METHOD_ID(method, _class, _methodName, _sig);\
 130     if (!NSK_JNI_VERIFY_VOID(env, NSK_CPP_STUB4(CallVoidMethod, env, _obj,\
 131                                                     method, _param)))\
 132         exit(1)
 133 
 134 #define MONITOR_ENTER(x) \
 135     NSK_JNI_VERIFY(env, NSK_CPP_STUB2(MonitorEnter, env, x) == 0)
 136 
 137 #define MONITOR_EXIT(x) \
 138     NSK_JNI_VERIFY(env, NSK_CPP_STUB2(MonitorExit, env, x) == 0)
 139 
 140 #endif /* _IS_NSK_STRACE_DEFINED_ */