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