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 #ifndef NSK_TOOLS_DEFINED
  25 #define NSK_TOOLS_DEFINED
  26 
  27 /*************************************************************/
  28 
  29 #include <stdarg.h>
  30 #include <inttypes.h>
  31 
  32 /*************************************************************/
  33 
  34 #if defined(_LP64) && defined(__APPLE__)
  35 #define JLONG_FORMAT "%ld"
  36 #else  // _LP64 && __APPLE__
  37 #define JLONG_FORMAT "%" PRId64
  38 #endif // _LP64 && __APPLE__
  39 
  40 
  41 /**
  42  * Use examples:
  43  *
  44  *     jfieldID id = NSK_CPP_STUB3(jni,GetFieldID,clazz,name,sig);
  45  *
  46  *     jvmtiError code = NSK_CPP_STUB0(jvmti,DisposeEnvironment);
  47  *
  48  */
  49 
  50 #define NSK_CPP_STUB1(Func,env)  env->Func()
  51 #define NSK_CPP_STUB2(Func,env,a)  env->Func(a)
  52 #define NSK_CPP_STUB3(Func,env,a,b)  env->Func(a,b)
  53 #define NSK_CPP_STUB4(Func,env,a,b,c)  env->Func(a,b,c)
  54 #define NSK_CPP_STUB5(Func,env,a,b,c,d)  env->Func(a,b,c,d)
  55 #define NSK_CPP_STUB6(Func,env,a,b,c,d,e)  env->Func(a,b,c,d,e)
  56 #define NSK_CPP_STUB7(Func,env,a,b,c,d,e,f)  env->Func(a,b,c,d,e,f)
  57 #define NSK_CPP_STUB8(Func,env,a,b,c,d,e,f,g)  env->Func(a,b,c,d,e,f,g)
  58 #define NSK_CPP_STUB9(Func,env,a,b,c,d,e,f,g,h)  env->Func(a,b,c,d,e,f,g,h)
  59 
  60 /*************************************************************/
  61 
  62 /**
  63  * Use examples:
  64  *
  65  *     NSK_DISPLAY("Test started.\n");
  66  *     NSK_COMPLAIN("Test FAILED: %s\n",reason);
  67  *
  68  *
  69  */
  70 
  71 #define NSK_DISPLAY0(format)  nsk_ldisplay(__FILE__,__LINE__,format)
  72 #define NSK_DISPLAY1(format,a)  nsk_ldisplay(__FILE__,__LINE__,format,a)
  73 #define NSK_DISPLAY2(format,a,b)  nsk_ldisplay(__FILE__,__LINE__,format,a,b)
  74 #define NSK_DISPLAY3(format,a,b,c)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c)
  75 #define NSK_DISPLAY4(format,a,b,c,d)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d)
  76 #define NSK_DISPLAY5(format,a,b,c,d,e)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d,e)
  77 #define NSK_DISPLAY6(format,a,b,c,d,e,f)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d,e,f)
  78 #define NSK_DISPLAY7(format,a,b,c,d,e,f,g)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d,e,f,g)
  79 #define NSK_DISPLAY8(format,a,b,c,d,e,f,g,h)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d,e,f,g,h)
  80 #define NSK_DISPLAY9(format,a,b,c,d,e,f,g,h,i)  nsk_ldisplay(__FILE__,__LINE__,format,a,b,c,d,e,f,g,h,i)
  81 
  82 #define NSK_COMPLAIN0(format)  nsk_lcomplain(__FILE__,__LINE__,format)
  83 #define NSK_COMPLAIN1(format,a)  nsk_lcomplain(__FILE__,__LINE__,format,a)
  84 #define NSK_COMPLAIN2(format,a,b)  nsk_lcomplain(__FILE__,__LINE__,format,a,b)
  85 #define NSK_COMPLAIN3(format,a,b,c)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c)
  86 #define NSK_COMPLAIN4(format,a,b,c,d)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d)
  87 #define NSK_COMPLAIN5(format,a,b,c,d,e)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d,e)
  88 #define NSK_COMPLAIN6(format,a,b,c,d,e,f)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d,e,f)
  89 #define NSK_COMPLAIN7(format,a,b,c,d,e,f,g)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d,e,f,g)
  90 #define NSK_COMPLAIN8(format,a,b,c,d,e,f,g,h)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d,e,f,g,h)
  91 #define NSK_COMPLAIN9(format,a,b,c,d,e,f,g,h,i)  nsk_lcomplain(__FILE__,__LINE__,format,a,b,c,d,e,f,g,h,i)
  92 
  93 #define NSK_VERIFY(action)  (nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action), \
  94                                 nsk_lverify(!!(action),__FILE__,__LINE__,"%s\n",#action))
  95 #define NSK_TRACE(action)   {nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action); \
  96                             (void)(action); \
  97                             nsk_ltrace(NSK_TRACE_AFTER,__FILE__,__LINE__,"%s\n",#action);}
  98 #define NSK_BEFORE_TRACE(action) nsk_ltrace(NSK_TRACE_BEFORE,__FILE__,__LINE__,"%s\n",#action); \
  99                                  (void)(action)
 100 
 101 /*************************************************************/
 102 
 103 extern "C" {
 104 
 105 #define NSK_TRUE  1
 106 #define NSK_FALSE 0
 107 
 108 #define NSK_TRACE_NONE    0
 109 #define NSK_TRACE_BEFORE  1
 110 #define NSK_TRACE_AFTER   2
 111 #define NSK_TRACE_ALL     (NSK_TRACE_BEFORE | NSK_TRACE_AFTER)
 112 
 113 #define NSK_MAX_COMPLAINS_NON_VERBOSE 665
 114 
 115 /**
 116  * Mode is verbose iff "verbose" isn't NSK_FALSE.
 117  */
 118 void nsk_setVerboseMode(int verbose);
 119 int  nsk_getVerboseMode();
 120 
 121 /**
 122  * Trace mode can be any combination of NSK_TRACE_* flags.
 123  */
 124 void nsk_setTraceMode(int mode);
 125 int  nsk_getTraceMode();
 126 
 127 /**
 128  * Display the message if current mode is verbose.
 129  */
 130 void nsk_display(const char format[], ...);
 131 void nsk_vdisplay(const char format[], va_list args);
 132 /**
 133  * Add a prompt to point the file[] and line location.
 134  */
 135 void nsk_ldisplay(const char file[], int line, const char format[], ...);
 136 void nsk_lvdisplay(const char file[], int line, const char format[], va_list args);
 137 
 138 /**
 139  * Complain the error message; add an "ERROR" prompt.
 140  * No matter, is current mode verbose or not.
 141  */
 142 void nsk_complain(const char format[], ...);
 143 void nsk_vcomplain(const char format[], va_list args);
 144 /**
 145  * Add a prompt to point the file[] and line location.
 146  */
 147 void nsk_lcomplain(const char file[], int line, const char format[], ...);
 148 void nsk_lvcomplain(const char file[], int line, const char format[], va_list args);
 149 
 150 /**
 151  * Trace executable actions,
 152  */
 153 void nsk_ltrace(int mode, const char file[], int line, const char format[], ...);
 154 void nsk_lvtrace(int mode, const char file[], int line, const char format[], va_list args);
 155 
 156 /**
 157  * Complain the message as an error if value==0; return !!value.
 158  * Add a prompt to point the file[] and line location.
 159  * Display anyway if verbose.
 160  */
 161 int nsk_lverify(int value, const char file[], int line, const char format[], ...);
 162 
 163 /**
 164  * Same as printf() or vprintf(); but we may redefine this later.
 165  */
 166 int nsk_vprintf(const char format[], va_list args);
 167 int nsk_printf(const char format[], ...);
 168 
 169 /**
 170  * Print given bytes array as hex numbers in multiple strings, each
 171  * started with 'indent' prefix and offset info, followed by 'columns' bytes
 172  * as hex numbers, then followed by the same bytes as ASCII chars where
 173  * non-printable chars are replaced by '.', and terminated with new line char.
 174  * Typically columns number is 16 and should not be greater than 255.
 175  */
 176 void nsk_printHexBytes(const char indent[], int columns,
 177                                     size_t size, const unsigned char bytes[]);
 178 
 179 /*************************************************************/
 180 
 181 /**
 182  * Returns str or "<NULL>" if str is NULL; useful for printing strings.
 183  */
 184 const char* nsk_null_string(const char* str);
 185 
 186 /*************************************************************/
 187 
 188 }
 189 
 190 /*************************************************************/
 191 
 192 #endif