1 /*
   2  * Copyright (c) 2015, 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 
  25 #include "precompiled.hpp"
  26 #include "classfile/stringTable.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "gc/shared/referenceProcessor.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/commandLineFlagRangeList.hpp"
  31 #include "runtime/os_ext.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 class CommandLineFlagRange_int : public CommandLineFlagRange {
  36   int _min;
  37   int _max;
  38 
  39 public:
  40   CommandLineFlagRange_int(const char* name, int min, int max) : CommandLineFlagRange(name) {
  41     _min=min, _max=max;
  42   }
  43 
  44   Flag::Error check_int(int value, bool verbose = true) {
  45     if ((value < _min) || (value > _max)) {
  46       if (verbose == true) {
  47         jio_fprintf(defaultStream::error_stream(),
  48                     "intx %s=%d is outside the allowed range [ %d ... %d ]\n",
  49                     _name, value, _min, _max);
  50       }
  51       return Flag::OUT_OF_BOUNDS;
  52     } else {
  53       return Flag::SUCCESS;
  54     }
  55   }
  56 
  57   void print(outputStream* st) {
  58     st->print("[ %25d ... %25d ]", _min, _max);
  59   }
  60 };
  61 
  62 class CommandLineFlagRange_intx : public CommandLineFlagRange {
  63   intx _min;
  64   intx _max;
  65 
  66 public:
  67   CommandLineFlagRange_intx(const char* name, intx min, intx max) : CommandLineFlagRange(name) {
  68     _min=min, _max=max;
  69   }
  70 
  71   Flag::Error check_intx(intx value, bool verbose = true) {
  72     if ((value < _min) || (value > _max)) {
  73       if (verbose == true) {
  74         jio_fprintf(defaultStream::error_stream(),
  75                     "intx %s="INTX_FORMAT" is outside the allowed range [ "INTX_FORMAT" ... "INTX_FORMAT" ]\n",
  76                     _name, value, _min, _max);
  77       }
  78       return Flag::OUT_OF_BOUNDS;
  79     } else {
  80       return Flag::SUCCESS;
  81     }
  82   }
  83 
  84   void print(outputStream* st) {
  85     st->print("[ "INTX_FORMAT_W(-25)" ... "INTX_FORMAT_W(25)" ]", _min, _max);
  86   }
  87 };
  88 
  89 class CommandLineFlagRange_uint : public CommandLineFlagRange {
  90   uint _min;
  91   uint _max;
  92 
  93 public:
  94   CommandLineFlagRange_uint(const char* name, uint min, uint max) : CommandLineFlagRange(name) {
  95     _min=min, _max=max;
  96   }
  97 
  98   Flag::Error check_uint(uint value, bool verbose = true) {
  99     if ((value < _min) || (value > _max)) {
 100       if (verbose == true) {
 101         jio_fprintf(defaultStream::error_stream(),
 102                     "uintx %s=%u is outside the allowed range [ %u ... %u ]\n",
 103                     _name, value, _min, _max);
 104       }
 105       return Flag::OUT_OF_BOUNDS;
 106     } else {
 107       return Flag::SUCCESS;
 108     }
 109   }
 110 
 111   void print(outputStream* st) {
 112     st->print("[ %25u ... %25u ]", _min, _max);
 113   }
 114 };
 115 
 116 class CommandLineFlagRange_uintx : public CommandLineFlagRange {
 117   uintx _min;
 118   uintx _max;
 119 
 120 public:
 121   CommandLineFlagRange_uintx(const char* name, uintx min, uintx max) : CommandLineFlagRange(name) {
 122     _min=min, _max=max;
 123   }
 124 
 125   Flag::Error check_uintx(uintx value, bool verbose = true) {
 126     if ((value < _min) || (value > _max)) {
 127       if (verbose == true) {
 128         jio_fprintf(defaultStream::error_stream(),
 129                     "uintx %s="UINTX_FORMAT" is outside the allowed range [ "UINTX_FORMAT" ... "UINTX_FORMAT" ]\n",
 130                     _name, value, _min, _max);
 131       }
 132       return Flag::OUT_OF_BOUNDS;
 133     } else {
 134       return Flag::SUCCESS;
 135     }
 136   }
 137 
 138   void print(outputStream* st) {
 139     st->print("[ "UINTX_FORMAT_W(-25)" ... "UINTX_FORMAT_W(25)" ]", _min, _max);
 140   }
 141 };
 142 
 143 class CommandLineFlagRange_uint64_t : public CommandLineFlagRange {
 144   uint64_t _min;
 145   uint64_t _max;
 146 
 147 public:
 148   CommandLineFlagRange_uint64_t(const char* name, uint64_t min, uint64_t max) : CommandLineFlagRange(name) {
 149     _min=min, _max=max;
 150   }
 151 
 152   Flag::Error check_uint64_t(uint64_t value, bool verbose = true) {
 153     if ((value < _min) || (value > _max)) {
 154       if (verbose == true) {
 155         jio_fprintf(defaultStream::error_stream(),
 156                     "uint64_t %s="UINT64_FORMAT" is outside the allowed range [ "UINT64_FORMAT" ... "UINT64_FORMAT" ]\n",
 157                     _name, value, _min, _max);
 158       }
 159       return Flag::OUT_OF_BOUNDS;
 160     } else {
 161       return Flag::SUCCESS;
 162     }
 163   }
 164 
 165   void print(outputStream* st) {
 166     st->print("[ "UINT64_FORMAT_W(-25)" ... "UINT64_FORMAT_W(25)" ]", _min, _max);
 167   }
 168 };
 169 
 170 class CommandLineFlagRange_size_t : public CommandLineFlagRange {
 171   size_t _min;
 172   size_t _max;
 173 
 174 public:
 175   CommandLineFlagRange_size_t(const char* name, size_t min, size_t max) : CommandLineFlagRange(name) {
 176     _min=min, _max=max;
 177   }
 178 
 179   Flag::Error check_size_t(size_t value, bool verbose = true) {
 180     if ((value < _min) || (value > _max)) {
 181       if (verbose == true) {
 182         jio_fprintf(defaultStream::error_stream(),
 183                     "size_t %s="SIZE_FORMAT" is outside the allowed range [ "SIZE_FORMAT" ... "SIZE_FORMAT" ]\n",
 184                     _name, value, _min, _max);
 185       }
 186       return Flag::OUT_OF_BOUNDS;
 187     } else {
 188       return Flag::SUCCESS;
 189     }
 190   }
 191 
 192   void print(outputStream* st) {
 193     st->print("[ "SIZE_FORMAT_W(-25)" ... "SIZE_FORMAT_W(25)" ]", _min, _max);
 194   }
 195 };
 196 
 197 class CommandLineFlagRange_double : public CommandLineFlagRange {
 198   double _min;
 199   double _max;
 200 
 201 public:
 202   CommandLineFlagRange_double(const char* name, double min, double max) : CommandLineFlagRange(name) {
 203     _min=min, _max=max;
 204   }
 205 
 206   Flag::Error check_double(double value, bool verbose = true) {
 207     if ((value < _min) || (value > _max)) {
 208       if (verbose == true) {
 209         jio_fprintf(defaultStream::error_stream(),
 210                     "double %s=%f is outside the allowed range [ %f ... %f ]\n",
 211                     _name, value, _min, _max);
 212       }
 213       return Flag::OUT_OF_BOUNDS;
 214     } else {
 215       return Flag::SUCCESS;
 216     }
 217   }
 218 
 219   void print(outputStream* st) {
 220     st->print("[ %-25.3f ... %25.3f ]", _min, _max);
 221   }
 222 };
 223 
 224 // No constraint emitting
 225 void emit_range_no(...)                         { /* NOP */ }
 226 
 227 // No constraint emitting if function argument is NOT provided
 228 void emit_range_bool(const char* /*name*/)      { /* NOP */ }
 229 void emit_range_ccstr(const char* /*name*/)     { /* NOP */ }
 230 void emit_range_ccstrlist(const char* /*name*/) { /* NOP */ }
 231 void emit_range_int(const char* /*name*/)       { /* NOP */ }
 232 void emit_range_intx(const char* /*name*/)      { /* NOP */ }
 233 void emit_range_uint(const char* /*name*/)      { /* NOP */ }
 234 void emit_range_uintx(const char* /*name*/)     { /* NOP */ }
 235 void emit_range_uint64_t(const char* /*name*/)  { /* NOP */ }
 236 void emit_range_size_t(const char* /*name*/)    { /* NOP */ }
 237 void emit_range_double(const char* /*name*/)    { /* NOP */ }
 238 
 239 // CommandLineFlagRange emitting code functions if function argument is provided
 240 void emit_range_int(const char* name, int min, int max) {
 241   CommandLineFlagRangeList::add(new CommandLineFlagRange_int(name, min, max));
 242 }
 243 void emit_range_intx(const char* name, intx min, intx max) {
 244   CommandLineFlagRangeList::add(new CommandLineFlagRange_intx(name, min, max));
 245 }
 246 void emit_range_uint(const char* name, uint min, uint max) {
 247   CommandLineFlagRangeList::add(new CommandLineFlagRange_uint(name, min, max));
 248 }
 249 void emit_range_uintx(const char* name, uintx min, uintx max) {
 250   CommandLineFlagRangeList::add(new CommandLineFlagRange_uintx(name, min, max));
 251 }
 252 void emit_range_uint64_t(const char* name, uint64_t min, uint64_t max) {
 253   CommandLineFlagRangeList::add(new CommandLineFlagRange_uint64_t(name, min, max));
 254 }
 255 void emit_range_size_t(const char* name, size_t min, size_t max) {
 256   CommandLineFlagRangeList::add(new CommandLineFlagRange_size_t(name, min, max));
 257 }
 258 void emit_range_double(const char* name, double min, double max) {
 259   CommandLineFlagRangeList::add(new CommandLineFlagRange_double(name, min, max));
 260 }
 261 
 262 // Generate code to call emit_range_xxx function
 263 #define EMIT_RANGE_PRODUCT_FLAG(type, name, value, doc)      ); emit_range_##type(#name
 264 #define EMIT_RANGE_COMMERCIAL_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 265 #define EMIT_RANGE_DIAGNOSTIC_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 266 #define EMIT_RANGE_EXPERIMENTAL_FLAG(type, name, value, doc) ); emit_range_##type(#name
 267 #define EMIT_RANGE_MANAGEABLE_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 268 #define EMIT_RANGE_PRODUCT_RW_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 269 #define EMIT_RANGE_PD_PRODUCT_FLAG(type, name, doc)          ); emit_range_##type(#name
 270 #define EMIT_RANGE_DEVELOPER_FLAG(type, name, value, doc)    ); emit_range_##type(#name
 271 #define EMIT_RANGE_PD_DEVELOPER_FLAG(type, name, doc)        ); emit_range_##type(#name
 272 #define EMIT_RANGE_NOTPRODUCT_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 273 #define EMIT_RANGE_LP64_PRODUCT_FLAG(type, name, value, doc) ); emit_range_##type(#name
 274 
 275 // Generate func argument to pass into emit_range_xxx functions
 276 #define EMIT_RANGE_CHECK(a, b)                               , a, b
 277 
 278 #define INITIAL_RANGES_SIZE 128
 279 GrowableArray<CommandLineFlagRange*>* CommandLineFlagRangeList::_ranges = NULL;
 280 
 281 // Check the ranges of all flags that have them
 282 void CommandLineFlagRangeList::init(void) {
 283 
 284   _ranges = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<CommandLineFlagRange*>(INITIAL_RANGES_SIZE, true);
 285 
 286   emit_range_no(NULL RUNTIME_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 287                                    EMIT_RANGE_PD_DEVELOPER_FLAG,
 288                                    EMIT_RANGE_PRODUCT_FLAG,
 289                                    EMIT_RANGE_PD_PRODUCT_FLAG,
 290                                    EMIT_RANGE_DIAGNOSTIC_FLAG,
 291                                    EMIT_RANGE_EXPERIMENTAL_FLAG,
 292                                    EMIT_RANGE_NOTPRODUCT_FLAG,
 293                                    EMIT_RANGE_MANAGEABLE_FLAG,
 294                                    EMIT_RANGE_PRODUCT_RW_FLAG,
 295                                    EMIT_RANGE_LP64_PRODUCT_FLAG,
 296                                    EMIT_RANGE_CHECK,
 297                                    IGNORE_CONSTRAINT) );
 298   
 299   EMIT_RANGES_FOR_GLOBALS_EXT
 300 
 301   emit_range_no(NULL ARCH_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 302                                      EMIT_RANGE_PRODUCT_FLAG,
 303                                      EMIT_RANGE_DIAGNOSTIC_FLAG,
 304                                      EMIT_RANGE_EXPERIMENTAL_FLAG,
 305                                      EMIT_RANGE_NOTPRODUCT_FLAG,
 306                                      EMIT_RANGE_CHECK,
 307                                      IGNORE_CONSTRAINT));
 308 
 309 #ifdef COMPILER1
 310   emit_range_no(NULL C1_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 311                                    EMIT_RANGE_PD_DEVELOPER_FLAG,
 312                                    EMIT_RANGE_PRODUCT_FLAG,
 313                                    EMIT_RANGE_PD_PRODUCT_FLAG,
 314                                    EMIT_RANGE_DIAGNOSTIC_FLAG,
 315                                    EMIT_RANGE_NOTPRODUCT_FLAG,
 316                                    EMIT_RANGE_CHECK,
 317                                    IGNORE_CONSTRAINT));
 318 #endif // COMPILER1
 319 
 320 #ifdef COMPILER2
 321   emit_range_no(NULL C2_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 322                                    EMIT_RANGE_PD_DEVELOPER_FLAG,
 323                                    EMIT_RANGE_PRODUCT_FLAG,
 324                                    EMIT_RANGE_PD_PRODUCT_FLAG,
 325                                    EMIT_RANGE_DIAGNOSTIC_FLAG,
 326                                    EMIT_RANGE_EXPERIMENTAL_FLAG,
 327                                    EMIT_RANGE_NOTPRODUCT_FLAG,
 328                                    EMIT_RANGE_CHECK,
 329                                    IGNORE_CONSTRAINT));
 330 #endif // COMPILER2
 331 
 332 #if INCLUDE_ALL_GCS
 333   emit_range_no(NULL G1_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 334                                    EMIT_RANGE_PD_DEVELOPER_FLAG,
 335                                    EMIT_RANGE_PRODUCT_FLAG,
 336                                    EMIT_RANGE_PD_PRODUCT_FLAG,
 337                                    EMIT_RANGE_DIAGNOSTIC_FLAG,
 338                                    EMIT_RANGE_EXPERIMENTAL_FLAG,
 339                                    EMIT_RANGE_NOTPRODUCT_FLAG,
 340                                    EMIT_RANGE_MANAGEABLE_FLAG,
 341                                    EMIT_RANGE_PRODUCT_RW_FLAG,
 342                                    EMIT_RANGE_CHECK,
 343                                    IGNORE_CONSTRAINT));
 344 #endif // INCLUDE_ALL_GCS
 345 }
 346 
 347 CommandLineFlagRange* CommandLineFlagRangeList::find(const char* name) {
 348   CommandLineFlagRange* found = NULL;
 349   for (int i=0; i<length(); i++) {
 350     CommandLineFlagRange* range = at(i);
 351     if (strcmp(range->_name, name) == 0) {
 352       found = range;
 353       break;
 354     }
 355   }
 356   return found;
 357 }
 358 
 359 void CommandLineFlagRangeList::print(const char* name, outputStream* st, bool unspecified) {
 360   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
 361   if (range != NULL) {
 362     range->print(st);
 363   } else if (unspecified == true) {
 364     st->print("[                           ...                           ]");
 365   }
 366 }