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