1 /*
2  * Copyright (c) 1997, 2020, 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 "memory/allocation.inline.hpp"
28 #include "runtime/arguments.hpp"
29 #include "runtime/flags/jvmFlag.hpp"
30 #include "runtime/flags/jvmFlagAccess.hpp"
31 #include "runtime/flags/jvmFlagLookup.hpp"
32 #include "runtime/globals_extension.hpp"
33 #include "utilities/defaultStream.hpp"
34 #include "utilities/stringUtils.hpp"
35 
is_product_build()36 static bool is_product_build() {
37 #ifdef PRODUCT
38   return true;
39 #else
40   return false;
41 #endif
42 }
43 
set_origin(JVMFlagOrigin new_origin)44 void JVMFlag::set_origin(JVMFlagOrigin new_origin) {
45   int old_flags = _flags;
46   int origin = static_cast<int>(new_origin);
47   assert((origin & VALUE_ORIGIN_MASK) == origin, "sanity");
48   int was_in_cmdline = (new_origin == JVMFlagOrigin::COMMAND_LINE) ? WAS_SET_ON_COMMAND_LINE : 0;
49   _flags = Flags((_flags & ~VALUE_ORIGIN_MASK) | origin | was_in_cmdline);
50   if ((old_flags & WAS_SET_ON_COMMAND_LINE) != 0) {
51     assert((_flags & WAS_SET_ON_COMMAND_LINE) != 0, "once initialized, should never change");
52   }
53 }
54 
55 /**
56  * Returns if this flag is a constant in the binary.  Right now this is
57  * true for notproduct and develop flags in product builds.
58  */
is_constant_in_binary() const59 bool JVMFlag::is_constant_in_binary() const {
60 #ifdef PRODUCT
61   return is_notproduct() || is_develop();
62 #else
63   return false;
64 #endif
65 }
66 
is_unlocker() const67 bool JVMFlag::is_unlocker() const {
68   return strcmp(_name, "UnlockDiagnosticVMOptions") == 0 ||
69          strcmp(_name, "UnlockExperimentalVMOptions") == 0;
70 }
71 
is_unlocked() const72 bool JVMFlag::is_unlocked() const {
73   if (is_diagnostic()) {
74     return UnlockDiagnosticVMOptions;
75   }
76   if (is_experimental()) {
77     return UnlockExperimentalVMOptions;
78   }
79   return true;
80 }
81 
clear_diagnostic()82 void JVMFlag::clear_diagnostic() {
83   assert(is_diagnostic(), "sanity");
84   _flags = Flags(_flags & ~KIND_DIAGNOSTIC);
85   assert(!is_diagnostic(), "sanity");
86 }
87 
clear_experimental()88 void JVMFlag::clear_experimental() {
89   assert(is_experimental(), "sanity");
90   _flags = Flags(_flags & ~KIND_EXPERIMENTAL);
91   assert(!is_experimental(), "sanity");
92 }
93 
set_product()94 void JVMFlag::set_product() {
95   assert(!is_product(), "sanity");
96   _flags = Flags(_flags | KIND_PRODUCT);
97   assert(is_product(), "sanity");
98 }
99 
100 // Get custom message for this locked flag, or NULL if
101 // none is available. Returns message type produced.
get_locked_message(char * buf,int buflen) const102 JVMFlag::MsgType JVMFlag::get_locked_message(char* buf, int buflen) const {
103   buf[0] = '\0';
104   if (is_diagnostic() && !is_unlocked()) {
105     jio_snprintf(buf, buflen,
106                  "Error: VM option '%s' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.\n"
107                  "Error: The unlock option must precede '%s'.\n",
108                  _name, _name);
109     return JVMFlag::DIAGNOSTIC_FLAG_BUT_LOCKED;
110   }
111   if (is_experimental() && !is_unlocked()) {
112     jio_snprintf(buf, buflen,
113                  "Error: VM option '%s' is experimental and must be enabled via -XX:+UnlockExperimentalVMOptions.\n"
114                  "Error: The unlock option must precede '%s'.\n",
115                  _name, _name);
116     return JVMFlag::EXPERIMENTAL_FLAG_BUT_LOCKED;
117   }
118   if (is_develop() && is_product_build()) {
119     jio_snprintf(buf, buflen, "Error: VM option '%s' is develop and is available only in debug version of VM.\n",
120                  _name);
121     return JVMFlag::DEVELOPER_FLAG_BUT_PRODUCT_BUILD;
122   }
123   if (is_notproduct() && is_product_build()) {
124     jio_snprintf(buf, buflen, "Error: VM option '%s' is notproduct and is available only in debug version of VM.\n",
125                  _name);
126     return JVMFlag::NOTPRODUCT_FLAG_BUT_PRODUCT_BUILD;
127   }
128   return JVMFlag::NONE;
129 }
130 
131 // Helper function for JVMFlag::print_on().
132 // Fills current line up to requested position.
133 // Should the current position already be past the requested position,
134 // one separator blank is enforced.
fill_to_pos(outputStream * st,unsigned int req_pos)135 void fill_to_pos(outputStream* st, unsigned int req_pos) {
136   if ((unsigned int)st->position() < req_pos) {
137     st->fill_to(req_pos);  // need to fill with blanks to reach req_pos
138   } else {
139     st->print(" ");        // enforce blank separation. Previous field too long.
140   }
141 }
142 
print_on(outputStream * st,bool withComments,bool printRanges) const143 void JVMFlag::print_on(outputStream* st, bool withComments, bool printRanges) const {
144   // Don't print notproduct and develop flags in a product build.
145   if (is_constant_in_binary()) {
146     return;
147   }
148 
149   if (!printRanges) {
150     // The command line options -XX:+PrintFlags* cause this function to be called
151     // for each existing flag to print information pertinent to this flag. The data
152     // is displayed in columnar form, with the following layout:
153     //  col1 - data type, right-justified
154     //  col2 - name,      left-justified
155     //  col3 - ' ='       double-char, leading space to align with possible '+='
156     //  col4 - value      left-justified
157     //  col5 - kind       right-justified
158     //  col6 - origin     left-justified
159     //  col7 - comments   left-justified
160     //
161     //  The column widths are fixed. They are defined such that, for most cases,
162     //  an eye-pleasing tabular output is created.
163     //
164     //  Sample output:
165     //       bool ThreadPriorityVerbose                    = false                                     {product} {default}
166     //      uintx ThresholdTolerance                       = 10                                        {product} {default}
167     //     size_t TLABSize                                 = 0                                         {product} {default}
168     //      uintx SurvivorRatio                            = 8                                         {product} {default}
169     //     double InitialRAMPercentage                     = 1.562500                                  {product} {default}
170     //      ccstr CompileCommandFile                       = MyFile.cmd                                {product} {command line}
171     //  ccstrlist CompileOnly                              = Method1
172     //            CompileOnly                             += Method2                                   {product} {command line}
173     //  |         |                                       |  |                              |                    |               |
174     //  |         |                                       |  |                              |                    |               +-- col7
175     //  |         |                                       |  |                              |                    +-- col6
176     //  |         |                                       |  |                              +-- col5
177     //  |         |                                       |  +-- col4
178     //  |         |                                       +-- col3
179     //  |         +-- col2
180     //  +-- col1
181 
182     const unsigned int col_spacing = 1;
183     const unsigned int col1_pos    = 0;
184     const unsigned int col1_width  = 9;
185     const unsigned int col2_pos    = col1_pos + col1_width + col_spacing;
186     const unsigned int col2_width  = 39;
187     const unsigned int col3_pos    = col2_pos + col2_width + col_spacing;
188     const unsigned int col3_width  = 2;
189     const unsigned int col4_pos    = col3_pos + col3_width + col_spacing;
190     const unsigned int col4_width  = 30;
191     const unsigned int col5_pos    = col4_pos + col4_width + col_spacing;
192     const unsigned int col5_width  = 20;
193     const unsigned int col6_pos    = col5_pos + col5_width + col_spacing;
194     const unsigned int col6_width  = 15;
195     const unsigned int col7_pos    = col6_pos + col6_width + col_spacing;
196     const unsigned int col7_width  = 1;
197 
198     st->fill_to(col1_pos);
199     st->print("%*s", col1_width, type_string());  // right-justified, therefore width is required.
200 
201     fill_to_pos(st, col2_pos);
202     st->print("%s", _name);
203 
204     fill_to_pos(st, col3_pos);
205     st->print(" =");  // use " =" for proper alignment with multiline ccstr output.
206 
207     fill_to_pos(st, col4_pos);
208     if (is_bool()) {
209       st->print("%s", get_bool() ? "true" : "false");
210     } else if (is_int()) {
211       st->print("%d", get_int());
212     } else if (is_uint()) {
213       st->print("%u", get_uint());
214     } else if (is_intx()) {
215       st->print(INTX_FORMAT, get_intx());
216     } else if (is_uintx()) {
217       st->print(UINTX_FORMAT, get_uintx());
218     } else if (is_uint64_t()) {
219       st->print(UINT64_FORMAT, get_uint64_t());
220     } else if (is_size_t()) {
221       st->print(SIZE_FORMAT, get_size_t());
222     } else if (is_double()) {
223       st->print("%f", get_double());
224     } else if (is_ccstr()) {
225       // Honor <newline> characters in ccstr: print multiple lines.
226       const char* cp = get_ccstr();
227       if (cp != NULL) {
228         const char* eol;
229         while ((eol = strchr(cp, '\n')) != NULL) {
230           size_t llen = pointer_delta(eol, cp, sizeof(char));
231           st->print("%.*s", (int)llen, cp);
232           st->cr();
233           cp = eol+1;
234           fill_to_pos(st, col2_pos);
235           st->print("%s", _name);
236           fill_to_pos(st, col3_pos);
237           st->print("+=");
238           fill_to_pos(st, col4_pos);
239         }
240         st->print("%s", cp);
241       }
242     } else {
243       st->print("unhandled  type %s", type_string());
244       st->cr();
245       return;
246     }
247 
248     fill_to_pos(st, col5_pos);
249     print_kind(st, col5_width);
250 
251     fill_to_pos(st, col6_pos);
252     print_origin(st, col6_width);
253 
254 #ifndef PRODUCT
255     if (withComments) {
256       fill_to_pos(st, col7_pos);
257       st->print("%s", _doc);
258     }
259 #endif
260     st->cr();
261   } else if (!is_bool() && !is_ccstr()) {
262     // The command line options -XX:+PrintFlags* cause this function to be called
263     // for each existing flag to print information pertinent to this flag. The data
264     // is displayed in columnar form, with the following layout:
265     //  col1 - data type, right-justified
266     //  col2 - name,      left-justified
267     //  col4 - range      [ min ... max]
268     //  col5 - kind       right-justified
269     //  col6 - origin     left-justified
270     //  col7 - comments   left-justified
271     //
272     //  The column widths are fixed. They are defined such that, for most cases,
273     //  an eye-pleasing tabular output is created.
274     //
275     //  Sample output:
276     //       intx MinPassesBeforeFlush                               [ 0                         ...       9223372036854775807 ]                         {diagnostic} {default}
277     //      uintx MinRAMFraction                                     [ 1                         ...      18446744073709551615 ]                            {product} {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 
print_kind(outputStream * st,unsigned int width) const331 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 
print_origin(outputStream * st,unsigned int width) const383 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 
print_as_flag(outputStream * st) const411 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 != NULL) {
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 
flag_error_str(JVMFlag::Error error)452 const char* JVMFlag::flag_error_str(JVMFlag::Error error) {
453   switch (error) {
454     case JVMFlag::MISSING_NAME: return "MISSING_NAME";
455     case JVMFlag::MISSING_VALUE: return "MISSING_VALUE";
456     case JVMFlag::NON_WRITABLE: return "NON_WRITABLE";
457     case JVMFlag::OUT_OF_BOUNDS: return "OUT_OF_BOUNDS";
458     case JVMFlag::VIOLATES_CONSTRAINT: return "VIOLATES_CONSTRAINT";
459     case JVMFlag::INVALID_FLAG: return "INVALID_FLAG";
460     case JVMFlag::ERR_OTHER: return "ERR_OTHER";
461     case JVMFlag::SUCCESS: return "SUCCESS";
462     default: ShouldNotReachHere(); return "NULL";
463   }
464 }
465 
466 //----------------------------------------------------------------------
467 // Build flagTable[]
468 
469 // Find out the number of LP64/JVMCI/COMPILER1/COMPILER1/ARCH flags,
470 // for JVMFlag::flag_group()
471 
472 #define ENUM_F(type, name, ...)  enum_##name,
473 #define IGNORE_F(...)
474 
475 //                                                  dev     dev-pd  pro     pro-pd  notpro  range     constraint
476 enum FlagCounter_LP64  { LP64_RUNTIME_FLAGS(        ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)  num_flags_LP64   };
477 enum FlagCounter_JVMCI { JVMCI_ONLY(JVMCI_FLAGS(    ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_JVMCI  };
478 enum FlagCounter_C1    { COMPILER1_PRESENT(C1_FLAGS(ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_C1     };
479 enum FlagCounter_C2    { COMPILER2_PRESENT(C2_FLAGS(ENUM_F, ENUM_F, ENUM_F, ENUM_F, ENUM_F, IGNORE_F, IGNORE_F)) num_flags_C2     };
480 enum FlagCounter_ARCH  { ARCH_FLAGS(                ENUM_F,         ENUM_F,         ENUM_F, IGNORE_F, IGNORE_F)  num_flags_ARCH   };
481 
482 const int first_flag_enum_LP64   = 0;
483 const int first_flag_enum_JVMCI  = first_flag_enum_LP64  + num_flags_LP64;
484 const int first_flag_enum_C1     = first_flag_enum_JVMCI + num_flags_JVMCI;
485 const int first_flag_enum_C2     = first_flag_enum_C1    + num_flags_C1;
486 const int first_flag_enum_ARCH   = first_flag_enum_C2    + num_flags_C2;
487 const int first_flag_enum_other  = first_flag_enum_ARCH  + num_flags_ARCH;
488 
flag_group(int flag_enum)489 static constexpr int flag_group(int flag_enum) {
490   if (flag_enum < first_flag_enum_JVMCI) return JVMFlag::KIND_LP64_PRODUCT;
491   if (flag_enum < first_flag_enum_C1)    return JVMFlag::KIND_JVMCI;
492   if (flag_enum < first_flag_enum_C2)    return JVMFlag::KIND_C1;
493   if (flag_enum < first_flag_enum_ARCH)  return JVMFlag::KIND_C2;
494   if (flag_enum < first_flag_enum_other) return JVMFlag::KIND_ARCH;
495 
496   return 0;
497 }
498 
JVMFlag(int flag_enum,FlagType type,const char * name,void * addr,int flags,int extra_flags,const char * doc)499 constexpr JVMFlag::JVMFlag(int flag_enum, FlagType type, const char* name,
500                            void* addr, int flags, int extra_flags, const char* doc) :
501   _addr(addr), _name(name), _flags(), _type(type) NOT_PRODUCT(COMMA _doc(doc)) {
502   flags = flags | extra_flags | static_cast<int>(JVMFlagOrigin::DEFAULT) | flag_group(flag_enum);
503   if ((flags & JVMFlag::KIND_PRODUCT) != 0) {
504     if (flags & (JVMFlag::KIND_DIAGNOSTIC | JVMFlag::KIND_MANAGEABLE | JVMFlag::KIND_EXPERIMENTAL)) {
505       // Backwards compatibility. This will be relaxed in JDK-7123237.
506       flags &= ~(JVMFlag::KIND_PRODUCT);
507     }
508   }
509   _flags = static_cast<Flags>(flags);
510 }
511 
JVMFlag(int flag_enum,FlagType type,const char * name,void * addr,int flags,const char * doc)512 constexpr JVMFlag::JVMFlag(int flag_enum,  FlagType type, const char* name,
513                            void* addr, int flags, const char* doc) :
514   JVMFlag(flag_enum, type, name, addr, flags, /*extra_flags*/0, doc) {}
515 
516 const int PRODUCT_KIND     = JVMFlag::KIND_PRODUCT;
517 const int PRODUCT_KIND_PD  = JVMFlag::KIND_PRODUCT | JVMFlag::KIND_PLATFORM_DEPENDENT;
518 const int DEVELOP_KIND     = JVMFlag::KIND_DEVELOP;
519 const int DEVELOP_KIND_PD  = JVMFlag::KIND_DEVELOP | JVMFlag::KIND_PLATFORM_DEPENDENT;
520 const int NOTPROD_KIND     = JVMFlag::KIND_NOT_PRODUCT;
521 
522 #define FLAG_TYPE(type) (JVMFlag::TYPE_ ## type)
523 #define INITIALIZE_DEVELOP_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, DEVELOP_KIND,    __VA_ARGS__),
524 #define INITIALIZE_DEVELOP_FLAG_PD(type, name,        ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, DEVELOP_KIND_PD, __VA_ARGS__),
525 #define INITIALIZE_PRODUCT_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, PRODUCT_KIND,    __VA_ARGS__),
526 #define INITIALIZE_PRODUCT_FLAG_PD(type, name,        ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, PRODUCT_KIND_PD, __VA_ARGS__),
527 #define INITIALIZE_NOTPROD_FLAG(   type, name, value, ...) JVMFlag(FLAG_MEMBER_ENUM(name), FLAG_TYPE(type), XSTR(name), (void*)&name, NOTPROD_KIND,    __VA_ARGS__),
528 
529 // Handy aliases to match the symbols used in the flag specification macros.
530 const int DIAGNOSTIC   = JVMFlag::KIND_DIAGNOSTIC;
531 const int MANAGEABLE   = JVMFlag::KIND_MANAGEABLE;
532 const int EXPERIMENTAL = JVMFlag::KIND_EXPERIMENTAL;
533 
534 #define MATERIALIZE_ALL_FLAGS      \
535   ALL_FLAGS(INITIALIZE_DEVELOP_FLAG,     \
536             INITIALIZE_DEVELOP_FLAG_PD,  \
537             INITIALIZE_PRODUCT_FLAG,     \
538             INITIALIZE_PRODUCT_FLAG_PD,  \
539             INITIALIZE_NOTPROD_FLAG,     \
540             IGNORE_RANGE,          \
541             IGNORE_CONSTRAINT)
542 
543 static JVMFlag flagTable[NUM_JVMFlagsEnum + 1] = {
544   MATERIALIZE_ALL_FLAGS
545   JVMFlag() // The iteration code wants a flag with a NULL name at the end of the table.
546 };
547 
548 // We want flagTable[] to be completely initialized at C++ compilation time, which requires
549 // that all arguments passed to JVMFlag() constructors be constexpr. The following line
550 // checks for this -- if any non-constexpr arguments are passed, the C++ compiler will
551 // generate an error.
552 //
553 // constexpr implies internal linkage. This means the flagTable_verify_constexpr[] variable
554 // will not be included in jvmFlag.o, so there's no footprint cost for having this variable.
555 //
556 // Note that we cannot declare flagTable[] as constexpr because JVMFlag::_flags is modified
557 // at runtime.
558 constexpr JVMFlag flagTable_verify_constexpr[] = { MATERIALIZE_ALL_FLAGS };
559 
560 JVMFlag* JVMFlag::flags = flagTable;
561 size_t JVMFlag::numFlags = (sizeof(flagTable) / sizeof(JVMFlag));
562 
563 // Search the flag table for a named flag
find_flag(const char * name,size_t length,bool allow_locked,bool return_flag)564 JVMFlag* JVMFlag::find_flag(const char* name, size_t length, bool allow_locked, bool return_flag) {
565   JVMFlag* flag = JVMFlagLookup::find(name, length);
566   if (flag != NULL) {
567     // Found a matching entry.
568     // Don't report notproduct and develop flags in product builds.
569     if (flag->is_constant_in_binary()) {
570       return (return_flag ? flag : NULL);
571     }
572     // Report locked flags only if allowed.
573     if (!(flag->is_unlocked() || flag->is_unlocker())) {
574       if (!allow_locked) {
575         // disable use of locked flags, e.g. diagnostic, experimental,
576         // etc. until they are explicitly unlocked
577         return NULL;
578       }
579     }
580     return flag;
581   }
582   // JVMFlag name is not in the flag table
583   return NULL;
584 }
585 
fuzzy_match(const char * name,size_t length,bool allow_locked)586 JVMFlag* JVMFlag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
587   float VMOptionsFuzzyMatchSimilarity = 0.7f;
588   JVMFlag* match = NULL;
589   float score;
590   float max_score = -1;
591 
592   for (JVMFlag* current = &flagTable[0]; current->_name != NULL; current++) {
593     score = StringUtils::similarity(current->_name, strlen(current->_name), name, length);
594     if (score > max_score) {
595       max_score = score;
596       match = current;
597     }
598   }
599 
600   if (match == NULL) {
601     return NULL;
602   }
603 
604   if (!(match->is_unlocked() || match->is_unlocker())) {
605     if (!allow_locked) {
606       return NULL;
607     }
608   }
609 
610   if (max_score < VMOptionsFuzzyMatchSimilarity) {
611     return NULL;
612   }
613 
614   return match;
615 }
616 
is_default(JVMFlagsEnum flag)617 bool JVMFlag::is_default(JVMFlagsEnum flag) {
618   return flag_from_enum(flag)->is_default();
619 }
620 
is_ergo(JVMFlagsEnum flag)621 bool JVMFlag::is_ergo(JVMFlagsEnum flag) {
622   return flag_from_enum(flag)->is_ergonomic();
623 }
624 
is_cmdline(JVMFlagsEnum flag)625 bool JVMFlag::is_cmdline(JVMFlagsEnum flag) {
626   return flag_from_enum(flag)->is_command_line();
627 }
628 
is_jimage_resource(JVMFlagsEnum flag)629 bool JVMFlag::is_jimage_resource(JVMFlagsEnum flag) {
630   return flag_from_enum(flag)->is_jimage_resource();
631 }
632 
setOnCmdLine(JVMFlagsEnum flag)633 void JVMFlag::setOnCmdLine(JVMFlagsEnum flag) {
634   flag_from_enum(flag)->set_command_line();
635 }
636 
637 extern "C" {
compare_flags(const void * void_a,const void * void_b)638   static int compare_flags(const void* void_a, const void* void_b) {
639     return strcmp((*((JVMFlag**) void_a))->name(), (*((JVMFlag**) void_b))->name());
640   }
641 }
642 
printSetFlags(outputStream * out)643 void JVMFlag::printSetFlags(outputStream* out) {
644   // Print which flags were set on the command line
645   // note: this method is called before the thread structure is in place
646   //       which means resource allocation cannot be used.
647 
648   // The last entry is the null entry.
649   const size_t length = JVMFlag::numFlags - 1;
650 
651   // Sort
652   JVMFlag** array = NEW_C_HEAP_ARRAY(JVMFlag*, length, mtArguments);
653   for (size_t i = 0; i < length; i++) {
654     array[i] = &flagTable[i];
655   }
656   qsort(array, length, sizeof(JVMFlag*), compare_flags);
657 
658   // Print
659   for (size_t i = 0; i < length; i++) {
660     if (array[i]->get_origin() != JVMFlagOrigin::DEFAULT) {
661       array[i]->print_as_flag(out);
662       out->print(" ");
663     }
664   }
665   out->cr();
666   FREE_C_HEAP_ARRAY(JVMFlag*, array);
667 }
668 
669 #ifndef PRODUCT
670 
verify()671 void JVMFlag::verify() {
672   assert(Arguments::check_vm_args_consistency(), "Some flag settings conflict");
673 }
674 
675 #endif // PRODUCT
676 
677 #ifdef ASSERT
678 
assert_valid_flag_enum(JVMFlagsEnum i)679 void JVMFlag::assert_valid_flag_enum(JVMFlagsEnum i) {
680   assert(0 <= int(i) && int(i) < NUM_JVMFlagsEnum, "must be");
681 }
682 
check_all_flag_declarations()683 void JVMFlag::check_all_flag_declarations() {
684   for (JVMFlag* current = &flagTable[0]; current->_name != NULL; current++) {
685     int flags = static_cast<int>(current->_flags);
686     // Backwards compatibility. This will be relaxed/removed in JDK-7123237.
687     int mask = JVMFlag::KIND_DIAGNOSTIC | JVMFlag::KIND_MANAGEABLE | JVMFlag::KIND_EXPERIMENTAL;
688     if ((flags & mask) != 0) {
689       assert((flags & mask) == JVMFlag::KIND_DIAGNOSTIC ||
690              (flags & mask) == JVMFlag::KIND_MANAGEABLE ||
691              (flags & mask) == JVMFlag::KIND_EXPERIMENTAL,
692              "%s can be declared with at most one of "
693              "DIAGNOSTIC, MANAGEABLE or EXPERIMENTAL", current->_name);
694       assert((flags & KIND_NOT_PRODUCT) == 0 &&
695              (flags & KIND_DEVELOP) == 0,
696              "%s has an optional DIAGNOSTIC, MANAGEABLE or EXPERIMENTAL "
697              "attribute; it must be declared as a product flag", current->_name);
698     }
699   }
700 }
701 
702 #endif // ASSERT
703 
printFlags(outputStream * out,bool withComments,bool printRanges,bool skipDefaults)704 void JVMFlag::printFlags(outputStream* out, bool withComments, bool printRanges, bool skipDefaults) {
705   // Print the flags sorted by name
706   // Note: This method may be called before the thread structure is in place
707   //       which means resource allocation cannot be used. Also, it may be
708   //       called as part of error reporting, so handle native OOMs gracefully.
709 
710   // The last entry is the null entry.
711   const size_t length = JVMFlag::numFlags - 1;
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 != NULL) {
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 }
743 
printError(bool verbose,const char * msg,...)744 void JVMFlag::printError(bool verbose, const char* msg, ...) {
745   if (verbose) {
746     va_list listPointer;
747     va_start(listPointer, msg);
748     jio_vfprintf(defaultStream::error_stream(), msg, listPointer);
749     va_end(listPointer);
750   }
751 }
752