1 /*
  2  * Copyright (c) 1997, 2024, 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 "jfr/jfrEvents.hpp"
 27 #include "jvm_io.h"
 28 #include "memory/allocation.inline.hpp"
 29 #include "runtime/arguments.hpp"
 30 #include "runtime/flags/jvmFlag.hpp"
 31 #include "runtime/flags/jvmFlagAccess.hpp"
 32 #include "runtime/flags/jvmFlagLookup.hpp"
 33 #include "runtime/globals_extension.hpp"
 34 #include "utilities/defaultStream.hpp"
 35 #include "utilities/stringUtils.hpp"
 36 
 37 static bool is_product_build() {
 38 #ifdef PRODUCT
 39   return true;
 40 #else
 41   return false;
 42 #endif
 43 }
 44 
 45 void JVMFlag::set_origin(JVMFlagOrigin new_origin) {
 46   int old_flags = _flags;
 47   int origin = static_cast<int>(new_origin);
 48   assert((origin & VALUE_ORIGIN_MASK) == origin, "sanity");
 49   int was_in_cmdline = (new_origin == JVMFlagOrigin::COMMAND_LINE) ? WAS_SET_ON_COMMAND_LINE : 0;
 50   _flags = Flags((_flags & ~VALUE_ORIGIN_MASK) | origin | was_in_cmdline);
 51   if ((old_flags & WAS_SET_ON_COMMAND_LINE) != 0) {
 52     assert((_flags & WAS_SET_ON_COMMAND_LINE) != 0, "once initialized, should never change");
 53   }
 54 }
 55 
 56 /**
 57  * Returns if this flag is a constant in the binary.  Right now this is
 58  * true for notproduct and develop flags in product builds.
 59  */
 60 bool JVMFlag::is_constant_in_binary() const {
 61 #ifdef PRODUCT
 62   return is_notproduct() || is_develop();
 63 #else
 64   return false;
 65 #endif
 66 }
 67 
 68 bool JVMFlag::is_unlocker() const {
 69   return strcmp(_name, "UnlockDiagnosticVMOptions") == 0 ||
 70          strcmp(_name, "UnlockExperimentalVMOptions") == 0;
 71 }
 72 
 73 bool JVMFlag::is_unlocked() const {
 74   if (is_diagnostic()) {
 75     return UnlockDiagnosticVMOptions;
 76   }
 77   if (is_experimental()) {
 78     return UnlockExperimentalVMOptions;
 79   }
 80   return true;
 81 }
 82 
 83 void JVMFlag::clear_diagnostic() {
 84   assert(is_diagnostic(), "sanity");
 85   _flags = Flags(_flags & ~KIND_DIAGNOSTIC);
 86   assert(!is_diagnostic(), "sanity");
 87 }
 88 
 89 void JVMFlag::clear_experimental() {
 90   assert(is_experimental(), "sanity");
 91   _flags = Flags(_flags & ~KIND_EXPERIMENTAL);
 92   assert(!is_experimental(), "sanity");
 93 }
 94 
 95 void JVMFlag::set_product() {
 96   assert(!is_product(), "sanity");
 97   _flags = Flags(_flags | KIND_PRODUCT);
 98   assert(is_product(), "sanity");
 99 }
100 
101 // Get custom message for this locked flag, or null if
102 // none is available. Returns message type produced.
103 JVMFlag::MsgType JVMFlag::get_locked_message(char* buf, int buflen) const {
104   buf[0] = '\0';
105   if (is_diagnostic() && !is_unlocked()) {
106     jio_snprintf(buf, buflen,
107                  "Error: VM option '%s' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.\n"
108                  "Error: The unlock option must precede '%s'.\n",
109                  _name, _name);
110     return JVMFlag::DIAGNOSTIC_FLAG_BUT_LOCKED;
111   }
112   if (is_experimental() && !is_unlocked()) {
113     jio_snprintf(buf, buflen,
114                  "Error: VM option '%s' is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions.\n"
115                  "Error: The unlock option must precede '%s'.\n",
116                  _name, _name);
117     return JVMFlag::EXPERIMENTAL_FLAG_BUT_LOCKED;
118   }
119   if (is_develop() && is_product_build()) {
120     jio_snprintf(buf, buflen, "Error: VM option '%s' is develop and is available only in debug version of VM.\n",
121                  _name);
122     return JVMFlag::DEVELOPER_FLAG_BUT_PRODUCT_BUILD;
123   }
124   if (is_notproduct() && is_product_build()) {
125     jio_snprintf(buf, buflen, "Error: VM option '%s' is notproduct and is available only in debug version of VM.\n",
126                  _name);
127     return JVMFlag::NOTPRODUCT_FLAG_BUT_PRODUCT_BUILD;
128   }
129   return JVMFlag::NONE;
130 }
131 
132 // Helper function for JVMFlag::print_on().
133 // Fills current line up to requested position.
134 // Should the current position already be past the requested position,
135 // one separator blank is enforced.
136 static void fill_to_pos(outputStream* st, unsigned int req_pos) {
137   if ((unsigned int)st->position() < req_pos) {
138     st->fill_to(req_pos);  // need to fill with blanks to reach req_pos
139   } else {
140     st->print(" ");        // enforce blank separation. Previous field too long.
141   }
142 }
143 
144 void JVMFlag::print_on(outputStream* st, bool withComments, bool printRanges) const {
145   // Don't print notproduct and develop flags in a product build.
146   if (is_constant_in_binary()) {
147     return;
148   }
149 
150   if (!printRanges) {
151     // The command line options -XX:+PrintFlags* cause this function to be called
152     // for each existing flag to print information pertinent to this flag. The data
153     // is displayed in columnar form, with the following layout:
154     //  col1 - data type, right-justified
155     //  col2 - name,      left-justified
156     //  col3 - ' ='       double-char, leading space to align with possible '+='
157     //  col4 - value      left-justified
158     //  col5 - kind       right-justified
159     //  col6 - origin     left-justified
160     //  col7 - comments   left-justified
161     //
162     //  The column widths are fixed. They are defined such that, for most cases,
163     //  an eye-pleasing tabular output is created.
164     //
165     //  Sample output:
166     //       bool ThreadPriorityVerbose                    = false                                     {product} {default}
167     //      uintx ThresholdTolerance                       = 10                                        {product} {default}
168     //     size_t TLABSize                                 = 0                                         {product} {default}
169     //      uintx SurvivorRatio                            = 8                                         {product} {default}
170     //     double InitialRAMPercentage                     = 1.562500                                  {product} {default}
171     //      ccstr CompileCommandFile                       = MyFile.cmd                                {product} {command line}
172     //  ccstrlist CompileOnly                              = Method1
173     //            CompileOnly                             += Method2                                   {product} {command line}
174     //  |         |                                       |  |                              |                    |               |
175     //  |         |                                       |  |                              |                    |               +-- col7
176     //  |         |                                       |  |                              |                    +-- col6
177     //  |         |                                       |  |                              +-- col5
178     //  |         |                                       |  +-- col4
179     //  |         |                                       +-- col3
180     //  |         +-- col2
181     //  +-- col1
182 
183     const unsigned int col_spacing = 1;
184     const unsigned int col1_pos    = 0;
185     const unsigned int col1_width  = 9;
186     const unsigned int col2_pos    = col1_pos + col1_width + col_spacing;
187     const unsigned int col2_width  = 39;
188     const unsigned int col3_pos    = col2_pos + col2_width + col_spacing;
189     const unsigned int col3_width  = 2;
190     const unsigned int col4_pos    = col3_pos + col3_width + col_spacing;
191     const unsigned int col4_width  = 30;
192     const unsigned int col5_pos    = col4_pos + col4_width + col_spacing;
193     const unsigned int col5_width  = 20;
194     const unsigned int col6_pos    = col5_pos + col5_width + col_spacing;
195     const unsigned int col6_width  = 15;
196     const unsigned int col7_pos    = col6_pos + col6_width + col_spacing;
197     const unsigned int col7_width  = 1;
198 
199     st->fill_to(col1_pos);
200     st->print("%*s", col1_width, type_string());  // right-justified, therefore width is required.
201 
202     fill_to_pos(st, col2_pos);
203     st->print("%s", _name);
204 
205     fill_to_pos(st, col3_pos);
206     st->print(" =");  // use " =" for proper alignment with multiline ccstr output.
207 
208     fill_to_pos(st, col4_pos);
209     if (is_bool()) {
210       st->print("%s", get_bool() ? "true" : "false");
211     } else if (is_int()) {
212       st->print("%d", get_int());
213     } else if (is_uint()) {
214       st->print("%u", get_uint());
215     } else if (is_intx()) {
216       st->print(INTX_FORMAT, get_intx());
217     } else if (is_uintx()) {
218       st->print(UINTX_FORMAT, get_uintx());
219     } else if (is_uint64_t()) {
220       st->print(UINT64_FORMAT, get_uint64_t());
221     } else if (is_size_t()) {
222       st->print(SIZE_FORMAT, get_size_t());
223     } else if (is_double()) {
224       st->print("%f", get_double());
225     } else if (is_ccstr()) {
226       // Honor <newline> characters in ccstr: print multiple lines.
227       const char* cp = get_ccstr();
228       if (cp != nullptr) {
229         const char* eol;
230         while ((eol = strchr(cp, '\n')) != nullptr) {
231           size_t llen = pointer_delta(eol, cp, sizeof(char));
232           st->print("%.*s", (int)llen, cp);
233           st->cr();
234           cp = eol+1;
235           fill_to_pos(st, col2_pos);
236           st->print("%s", _name);
237           fill_to_pos(st, col3_pos);
238           st->print("+=");
239           fill_to_pos(st, col4_pos);
240         }
241         st->print("%s", cp);
242       }
243     } else {
244       st->print("unhandled  type %s", type_string());
245       st->cr();
246       return;
247     }
248 
249     fill_to_pos(st, col5_pos);
250     print_kind(st, col5_width);
251 
252     fill_to_pos(st, col6_pos);
253     print_origin(st, col6_width);
254 
255 #ifndef PRODUCT
256     if (withComments) {
257       fill_to_pos(st, col7_pos);
258       st->print("%s", _doc);
259     }
260 #endif
261     st->cr();
262   } else if (!is_bool() && !is_ccstr()) {
263     // The command line options -XX:+PrintFlags* cause this function to be called
264     // for each existing flag to print information pertinent to this flag. The data
265     // is displayed in columnar form, with the following layout:
266     //  col1 - data type, right-justified
267     //  col2 - name,      left-justified
268     //  col4 - range      [ min ... max]
269     //  col5 - kind       right-justified
270     //  col6 - origin     left-justified
271     //  col7 - comments   left-justified
272     //
273     //  The column widths are fixed. They are defined such that, for most cases,
274     //  an eye-pleasing tabular output is created.
275     //
276     //  Sample output:
277     //       intx MinPassesBeforeFlush                               [ 0                         ...       9223372036854775807 ]                         {diagnostic} {default}
278     //     double MinRAMPercentage                                   [ 0.000                     ...                   100.000 ]                            {product} {default}
279     //      uintx MinSurvivorRatio                                   [ 3                         ...      18446744073709551615 ]                            {product} {default}
280     //     size_t MinTLABSize                                        [ 1                         ...       9223372036854775807 ]                            {product} {default}
281     //       intx MaxInlineSize                                      [ 0                         ...                2147483647 ]                            {product} {default}
282     //  |         |                                                  |                                                           |                                    |               |
283     //  |         |                                                  |                                                           |                                    |               +-- col7
284     //  |         |                                                  |                                                           |                                    +-- col6
285     //  |         |                                                  |                                                           +-- col5
286     //  |         |                                                  +-- col4
287     //  |         +-- col2
288     //  +-- col1
289 
290     const unsigned int col_spacing = 1;
291     const unsigned int col1_pos    = 0;
292     const unsigned int col1_width  = 9;
293     const unsigned int col2_pos    = col1_pos + col1_width + col_spacing;
294     const unsigned int col2_width  = 49;
295     const unsigned int col3_pos    = col2_pos + col2_width + col_spacing;
296     const unsigned int col3_width  = 0;
297     const unsigned int col4_pos    = col3_pos + col3_width + col_spacing;
298     const unsigned int col4_width  = 60;
299     const unsigned int col5_pos    = col4_pos + col4_width + col_spacing;
300     const unsigned int col5_width  = 35;
301     const unsigned int col6_pos    = col5_pos + col5_width + col_spacing;
302     const unsigned int col6_width  = 15;
303     const unsigned int col7_pos    = col6_pos + col6_width + col_spacing;
304     const unsigned int col7_width  = 1;
305 
306     st->fill_to(col1_pos);
307     st->print("%*s", col1_width, type_string());  // right-justified, therefore width is required.
308 
309     fill_to_pos(st, col2_pos);
310     st->print("%s", _name);
311 
312     fill_to_pos(st, col4_pos);
313     JVMFlagAccess::print_range(st, this);
314 
315     fill_to_pos(st, col5_pos);
316     print_kind(st, col5_width);
317 
318     fill_to_pos(st, col6_pos);
319     print_origin(st, col6_width);
320 
321 #ifndef PRODUCT
322     if (withComments) {
323       fill_to_pos(st, col7_pos);
324       st->print("%s", _doc);
325     }
326 #endif
327     st->cr();
328   }
329 }
330 
331 void JVMFlag::print_kind(outputStream* st, unsigned int width) const {
332   struct Data {
333     int flag;
334     const char* name;
335   };
336 
337   Data data[] = {
338     { KIND_JVMCI, "JVMCI" },
339     { KIND_C1, "C1" },
340     { KIND_C2, "C2" },
341     { KIND_ARCH, "ARCH" },
342     { KIND_PLATFORM_DEPENDENT, "pd" },
343     { KIND_PRODUCT, "product" },
344     { KIND_MANAGEABLE, "manageable" },
345     { KIND_DIAGNOSTIC, "diagnostic" },
346     { KIND_EXPERIMENTAL, "experimental" },
347     { KIND_NOT_PRODUCT, "notproduct" },
348     { KIND_DEVELOP, "develop" },
349     { KIND_LP64_PRODUCT, "lp64_product" },
350     { -1, "" }
351   };
352 
353   if ((_flags & KIND_MASK) != 0) {
354     bool is_first = true;
355     const size_t buffer_size = 64;
356     size_t buffer_used = 0;
357     char kind[buffer_size];
358 
359     jio_snprintf(kind, buffer_size, "{");
360     buffer_used++;
361     for (int i = 0; data[i].flag != -1; i++) {
362       Data d = data[i];
363       if ((_flags & d.flag) != 0) {
364         if (is_first) {
365           is_first = false;
366         } else {
367           assert(buffer_used + 1 < buffer_size, "Too small buffer");
368           jio_snprintf(kind + buffer_used, buffer_size - buffer_used, " ");
369           buffer_used++;
370         }
371         size_t length = strlen(d.name);
372         assert(buffer_used + length < buffer_size, "Too small buffer");
373         jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "%s", d.name);
374         buffer_used += length;
375       }
376     }
377     assert(buffer_used + 2 <= buffer_size, "Too small buffer");
378     jio_snprintf(kind + buffer_used, buffer_size - buffer_used, "}");
379     st->print("%*s", width, kind);
380   }
381 }
382 
383 void JVMFlag::print_origin(outputStream* st, unsigned int width) const {
384   st->print("{");
385   switch(get_origin()) {
386     case JVMFlagOrigin::DEFAULT:
387       st->print("default"); break;
388     case JVMFlagOrigin::COMMAND_LINE:
389       st->print("command line"); break;
390     case JVMFlagOrigin::ENVIRON_VAR:
391       st->print("environment"); break;
392     case JVMFlagOrigin::CONFIG_FILE:
393       st->print("config file"); break;
394     case JVMFlagOrigin::MANAGEMENT:
395       st->print("management"); break;
396     case JVMFlagOrigin::ERGONOMIC:
397       if (_flags & WAS_SET_ON_COMMAND_LINE) {
398         st->print("command line, ");
399       }
400       st->print("ergonomic"); break;
401     case JVMFlagOrigin::ATTACH_ON_DEMAND:
402       st->print("attach"); break;
403     case JVMFlagOrigin::INTERNAL:
404       st->print("internal"); break;
405     case JVMFlagOrigin::JIMAGE_RESOURCE:
406       st->print("jimage"); break;
407   }
408   st->print("}");
409 }
410 
411 void JVMFlag::print_as_flag(outputStream* st) const {
412   if (is_bool()) {
413     st->print("-XX:%s%s", get_bool() ? "+" : "-", _name);
414   } else if (is_int()) {
415     st->print("-XX:%s=%d", _name, get_int());
416   } else if (is_uint()) {
417     st->print("-XX:%s=%u", _name, get_uint());
418   } else if (is_intx()) {
419     st->print("-XX:%s=" INTX_FORMAT, _name, get_intx());
420   } else if (is_uintx()) {
421     st->print("-XX:%s=" UINTX_FORMAT, _name, get_uintx());
422   } else if (is_uint64_t()) {
423     st->print("-XX:%s=" UINT64_FORMAT, _name, get_uint64_t());
424   } else if (is_size_t()) {
425     st->print("-XX:%s=" SIZE_FORMAT, _name, get_size_t());
426   } else if (is_double()) {
427     st->print("-XX:%s=%f", _name, get_double());
428   } else if (is_ccstr()) {
429     st->print("-XX:%s=", _name);
430     const char* cp = get_ccstr();
431     if (cp != nullptr) {
432       // Need to turn embedded '\n's back into separate arguments
433       // Not so efficient to print one character at a time,
434       // but the choice is to do the transformation to a buffer
435       // and print that.  And this need not be efficient.
436       for (; *cp != '\0'; cp += 1) {
437         switch (*cp) {
438           default:
439             st->print("%c", *cp);
440             break;
441           case '\n':
442             st->print(" -XX:%s=", _name);
443             break;
444         }
445       }
446     }
447   } else {
448     ShouldNotReachHere();
449   }
450 }
451 
452 //----------------------------------------------------------------------
453 // Build flagTable[]
454 
455 // Find out the number of LP64/ARCH/JVMCI/COMPILER1/COMPILER2 flags,
456 // for JVMFlag::flag_group()
457 
458 #define ENUM_F(type, name, ...)  enum_##name,
459 #define IGNORE_F(...)
460 
461 //                                                  dev     dev-pd  pro     pro-pd  notpro  range     constraint
462 enum FlagCounter_LP64  { LP64_RUNTIME_FLAGS(        ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)  num_flags_LP64   };
463 enum FlagCounter_ARCH  { ARCH_FLAGS(                ENUM_F,         ENUM_F,         ENUM_F, IGNORE_F, IGNORE_F)  num_flags_ARCH   };
464 enum FlagCounter_JVMCI { JVMCI_ONLY(JVMCI_FLAGS(    ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_JVMCI  };
465 enum FlagCounter_C1    { COMPILER1_PRESENT(C1_FLAGS(ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_C1     };
466 enum FlagCounter_C2    { COMPILER2_PRESENT(C2_FLAGS(ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_C2     };
467 
468 const int first_flag_enum_LP64   = 0;
469 const int first_flag_enum_ARCH   = first_flag_enum_LP64  + num_flags_LP64;
470 const int first_flag_enum_JVMCI  = first_flag_enum_ARCH  + num_flags_ARCH;
471 const int first_flag_enum_C1     = first_flag_enum_JVMCI + num_flags_JVMCI;
472 const int first_flag_enum_C2     = first_flag_enum_C1    + num_flags_C1;
473 const int first_flag_enum_other  = first_flag_enum_C2    + num_flags_C2;
474 
475 static constexpr int flag_group(int flag_enum) {
476   if (flag_enum < first_flag_enum_ARCH)  return JVMFlag::KIND_LP64_PRODUCT;
477   if (flag_enum < first_flag_enum_JVMCI) return JVMFlag::KIND_ARCH;
478   if (flag_enum < first_flag_enum_C1)    return JVMFlag::KIND_JVMCI;
479   if (flag_enum < first_flag_enum_C2)    return JVMFlag::KIND_C1;
480   if (flag_enum < first_flag_enum_other) return JVMFlag::KIND_C2;
481 
482   return 0;
483 }
484 
485 constexpr JVMFlag::JVMFlag(int flag_enum, FlagType type, const char* name,
486                            void* addr, int flags, int extra_flags, const char* doc) :
487   _addr(addr), _name(name), _flags(), _type(type) NOT_PRODUCT(COMMA _doc(doc)) {
488   flags = flags | extra_flags | static_cast<int>(JVMFlagOrigin::DEFAULT) | flag_group(flag_enum);
489   if ((flags & JVMFlag::KIND_PRODUCT) != 0) {
490     if (flags & (JVMFlag::KIND_DIAGNOSTIC | JVMFlag::KIND_MANAGEABLE | JVMFlag::KIND_EXPERIMENTAL)) {
491       // Backwards compatibility. This will be relaxed in JDK-7123237.
492       flags &= ~(JVMFlag::KIND_PRODUCT);
493     }
494   }
495   _flags = static_cast<Flags>(flags);
496 }
497 
498 constexpr JVMFlag::JVMFlag(int flag_enum,  FlagType type, const char* name,
499                            void* addr, int flags, const char* doc) :
500   JVMFlag(flag_enum, type, name, addr, flags, /*extra_flags*/0, doc) {}
501 
502 const int PRODUCT_KIND     = JVMFlag::KIND_PRODUCT;
503 const int PRODUCT_KIND_PD  = JVMFlag::KIND_PRODUCT | JVMFlag::KIND_PLATFORM_DEPENDENT;
504 const int DEVELOP_KIND     = JVMFlag::KIND_DEVELOP;
505 const int DEVELOP_KIND_PD  = JVMFlag::KIND_DEVELOP | JVMFlag::KIND_PLATFORM_DEPENDENT;
506 const int NOTPROD_KIND     = JVMFlag::KIND_NOT_PRODUCT;
507 
508 #define FLAG_TYPE(type) (JVMFlag::TYPE_ ## type)
509 #define INITIALIZE_DEVELOP_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, DEVELOP_KIND,    __VA_ARGS__),
510 #define INITIALIZE_DEVELOP_FLAG_PD(type, name,        ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, DEVELOP_KIND_PD, __VA_ARGS__),
511 #define INITIALIZE_PRODUCT_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, PRODUCT_KIND,    __VA_ARGS__),
512 #define INITIALIZE_PRODUCT_FLAG_PD(type, name,        ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, PRODUCT_KIND_PD, __VA_ARGS__),
513 #define INITIALIZE_NOTPROD_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, NOTPROD_KIND,    __VA_ARGS__),
514 
515 // Handy aliases to match the symbols used in the flag specification macros.
516 const int DIAGNOSTIC   = JVMFlag::KIND_DIAGNOSTIC;
517 const int MANAGEABLE   = JVMFlag::KIND_MANAGEABLE;
518 const int EXPERIMENTAL = JVMFlag::KIND_EXPERIMENTAL;
519 
520 #define MATERIALIZE_ALL_FLAGS      \
521   ALL_FLAGS(INITIALIZE_DEVELOP_FLAG,     \
522             INITIALIZE_DEVELOP_FLAG_PD,  \
523             INITIALIZE_PRODUCT_FLAG,     \
524             INITIALIZE_PRODUCT_FLAG_PD,  \
525             INITIALIZE_NOTPROD_FLAG,     \
526             IGNORE_RANGE,          \
527             IGNORE_CONSTRAINT)
528 
529 static JVMFlag flagTable[NUM_JVMFlagsEnum + 1] = {
530   MATERIALIZE_ALL_FLAGS
531   JVMFlag() // The iteration code wants a flag with a null name at the end of the table.
532 };
533 
534 // We want flagTable[] to be completely initialized at C++ compilation time, which requires
535 // that all arguments passed to JVMFlag() constructors be constexpr. The following line
536 // checks for this -- if any non-constexpr arguments are passed, the C++ compiler will
537 // generate an error.
538 //
539 // constexpr implies internal linkage. This means the flagTable_verify_constexpr[] variable
540 // will not be included in jvmFlag.o, so there's no footprint cost for having this variable.
541 //
542 // Note that we cannot declare flagTable[] as constexpr because JVMFlag::_flags is modified
543 // at runtime.
544 constexpr JVMFlag flagTable_verify_constexpr[] = { MATERIALIZE_ALL_FLAGS };
545 
546 JVMFlag* JVMFlag::flags = flagTable;
547 size_t JVMFlag::numFlags = (sizeof(flagTable) / sizeof(JVMFlag));
548 
549 #define JVM_FLAG_TYPE_SIGNATURE(t) JVMFlag::type_signature<t>(),
550 
551 const int JVMFlag::type_signatures[] = {
552   JVM_FLAG_NON_STRING_TYPES_DO(JVM_FLAG_TYPE_SIGNATURE)
553   JVMFlag::type_signature<ccstr>(),
554   JVMFlag::type_signature<ccstr>()
555 };
556 
557 // Search the flag table for a named flag
558 JVMFlag* JVMFlag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
559   JVMFlag* flag = JVMFlagLookup::find(name, length);
560   if (flag != nullptr) {
561     // Found a matching entry.
562     // Don't report notproduct and develop flags in product builds.
563     if (flag->is_constant_in_binary()) {
564       return (return_flag ? flag : nullptr);
565     }
566     // Report locked flags only if allowed.
567     if (!(flag->is_unlocked() || flag->is_unlocker())) {
568       if (!allow_locked) {
569         // disable use of locked flags, e.g. diagnostic, experimental,
570         // etc. until they are explicitly unlocked
571         return nullptr;
572       }
573     }
574     return flag;
575   }
576   // JVMFlag name is not in the flag table
577   return nullptr;
578 }
579 
580 JVMFlag* JVMFlag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
581   double VMOptionsFuzzyMatchSimilarity = 0.7;
582   JVMFlag* match = nullptr;
583   double score;
584   double max_score = -1;
585 
586   for (JVMFlag* current = &flagTable[0]; current->_name != nullptr; current++) {
587     score = StringUtils::similarity(current->_name, strlen(current->_name), name, length);
588     if (score > max_score) {
589       max_score = score;
590       match = current;
591     }
592   }
593 
594   if (match == nullptr) {
595     return nullptr;
596   }
597 
598   if (!(match->is_unlocked() || match->is_unlocker())) {
599     if (!allow_locked) {
600       return nullptr;
601     }
602   }
603 
604   if (max_score < VMOptionsFuzzyMatchSimilarity) {
605     return nullptr;
606   }
607 
608   return match;
609 }
610 
611 bool JVMFlag::is_default(JVMFlagsEnum flag) {
612   return flag_from_enum(flag)->is_default();
613 }
614 
615 bool JVMFlag::is_ergo(JVMFlagsEnum flag) {
616   return flag_from_enum(flag)->is_ergonomic();
617 }
618 
619 bool JVMFlag::is_cmdline(JVMFlagsEnum flag) {
620   return flag_from_enum(flag)->is_command_line();
621 }
622 
623 bool JVMFlag::is_jimage_resource(JVMFlagsEnum flag) {
624   return flag_from_enum(flag)->is_jimage_resource();
625 }
626 
627 void JVMFlag::setOnCmdLine(JVMFlagsEnum flag) {
628   flag_from_enum(flag)->set_command_line();
629 }
630 
631 extern "C" {
632   static int compare_flags(const void* void_a, const void* void_b) {
633     return strcmp((*((JVMFlag**) void_a))->name(), (*((JVMFlag**) void_b))->name());
634   }
635 }
636 
637 void JVMFlag::printSetFlags(outputStream* out) {
638   // Print which flags were set on the command line
639   // note: this method is called before the thread structure is in place
640   //       which means resource allocation cannot be used.
641 
642   // The last entry is the null entry.
643   const size_t length = JVMFlag::numFlags - 1;
644 
645   // Sort
646   JVMFlag** array = NEW_C_HEAP_ARRAY(JVMFlag*, length, mtArguments);
647   for (size_t i = 0; i < length; i++) {
648     array[i] = &flagTable[i];
649   }
650   qsort(array, length, sizeof(JVMFlag*), compare_flags);
651 
652   // Print
653   for (size_t i = 0; i < length; i++) {
654     if (array[i]->get_origin() != JVMFlagOrigin::DEFAULT) {
655       array[i]->print_as_flag(out);
656       out->print(" ");
657     }
658   }
659   out->cr();
660   FREE_C_HEAP_ARRAY(JVMFlag*, array);
661 }
662 
663 #ifndef PRODUCT
664 
665 void JVMFlag::verify() {
666   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
667 }
668 
669 #endif // PRODUCT
670 
671 #ifdef ASSERT
672 
673 void JVMFlag::assert_valid_flag_enum(JVMFlagsEnum i) {
674   assert(0 <= int(i) && int(i) < NUM_JVMFlagsEnum, "must be");
675 }
676 
677 void JVMFlag::check_all_flag_declarations() {
678   for (JVMFlag* current = &flagTable[0]; current->_name != nullptr; current++) {
679     int flags = static_cast<int>(current->_flags);
680     // Backwards compatibility. This will be relaxed/removed in JDK-7123237.
681     int mask = JVMFlag::KIND_DIAGNOSTIC | JVMFlag::KIND_MANAGEABLE | JVMFlag::KIND_EXPERIMENTAL;
682     if ((flags & mask) != 0) {
683       assert((flags & mask) == JVMFlag::KIND_DIAGNOSTIC ||
684              (flags & mask) == JVMFlag::KIND_MANAGEABLE ||
685              (flags & mask) == JVMFlag::KIND_EXPERIMENTAL,
686              "%s can be declared with at most one of "
687              "DIAGNOSTIC, MANAGEABLE or EXPERIMENTAL", current->_name);
688       assert((flags & KIND_NOT_PRODUCT) == 0 &&
689              (flags & KIND_DEVELOP) == 0,
690              "%s has an optional DIAGNOSTIC, MANAGEABLE or EXPERIMENTAL "
691              "attribute; it must be declared as a product flag", current->_name);
692     }
693   }
694 }
695 
696 #endif // ASSERT
697 
698 void JVMFlag::printFlags(outputStream* out, bool withComments, bool printRanges, bool skipDefaults) {
699   // Print the flags sorted by name
700   // Note: This method may be called before the thread structure is in place
701   //       which means resource allocation cannot be used. Also, it may be
702   //       called as part of error reporting, so handle native OOMs gracefully.
703 
704   // The last entry is the null entry.
705   const size_t length = JVMFlag::numFlags - 1;
706 
707   // Print
708   if (!printRanges) {
709     out->print_cr("[Global flags]");
710   } else {
711     out->print_cr("[Global flags ranges]");
712   }
713 
714   // Sort
715   JVMFlag** array = NEW_C_HEAP_ARRAY_RETURN_NULL(JVMFlag*, length, mtArguments);
716   if (array != nullptr) {
717     for (size_t i = 0; i < length; i++) {
718       array[i] = &flagTable[i];
719     }
720     qsort(array, length, sizeof(JVMFlag*), compare_flags);
721 
722     for (size_t i = 0; i < length; i++) {
723       if (array[i]->is_unlocked() && !(skipDefaults && array[i]->is_default())) {
724         array[i]->print_on(out, withComments, printRanges);
725       }
726     }
727     FREE_C_HEAP_ARRAY(JVMFlag*, array);
728   } else {
729     // OOM? Print unsorted.
730     for (size_t i = 0; i < length; i++) {
731       if (flagTable[i].is_unlocked() && !(skipDefaults && flagTable[i].is_default())) {
732         flagTable[i].print_on(out, withComments, printRanges);
733       }
734     }
735   }
736 }
737 
738 void JVMFlag::printError(bool verbose, const char* msg, ...) {
739   if (verbose) {
740     va_list listPointer;
741     va_start(listPointer, msg);
742     jio_vfprintf(defaultStream::error_stream(), msg, listPointer);
743     va_end(listPointer);
744   }
745 }