1 /* 2 * Copyright (c) 1997, 2023, 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 "cds/cds_globals.hpp" 27 #include "cds/filemap.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/javaAssertions.hpp" 30 #include "classfile/moduleEntry.hpp" 31 #include "classfile/stringTable.hpp" 32 #include "classfile/symbolTable.hpp" 33 #include "compiler/compilerDefinitions.hpp" 34 #include "gc/shared/gcArguments.hpp" 35 #include "gc/shared/gcConfig.hpp" 36 #include "gc/shared/stringdedup/stringDedup.hpp" 37 #include "gc/shared/tlab_globals.hpp" 38 #include "jvm.h" 39 #include "logging/log.hpp" 40 #include "logging/logConfiguration.hpp" 41 #include "logging/logStream.hpp" 42 #include "logging/logTag.hpp" 43 #include "memory/allocation.inline.hpp" 44 #include "oops/compressedKlass.hpp" 45 #include "oops/instanceKlass.hpp" 46 #include "oops/oop.inline.hpp" 47 #include "prims/jvmtiAgentList.hpp" 48 #include "prims/jvmtiExport.hpp" 49 #include "runtime/arguments.hpp" 50 #include "runtime/flags/jvmFlag.hpp" 51 #include "runtime/flags/jvmFlagAccess.hpp" 52 #include "runtime/flags/jvmFlagLimit.hpp" 53 #include "runtime/globals_extension.hpp" 54 #include "runtime/java.hpp" 55 #include "runtime/os.hpp" 56 #include "runtime/safepoint.hpp" 57 #include "runtime/safepointMechanism.hpp" 58 #include "runtime/synchronizer.hpp" 59 #include "runtime/vm_version.hpp" 60 #include "services/management.hpp" 61 #include "services/nmtCommon.hpp" 62 #include "utilities/align.hpp" 63 #include "utilities/debug.hpp" 64 #include "utilities/defaultStream.hpp" 65 #include "utilities/macros.hpp" 66 #include "utilities/parseInteger.hpp" 67 #include "utilities/powerOfTwo.hpp" 68 #include "utilities/stringUtils.hpp" 69 #include "utilities/systemMemoryBarrier.hpp" 70 #if INCLUDE_JFR 71 #include "jfr/jfr.hpp" 72 #endif 73 74 #include <limits> 75 76 static const char _default_java_launcher[] = "generic"; 77 78 #define DEFAULT_JAVA_LAUNCHER _default_java_launcher 79 80 char* Arguments::_jvm_flags_file = nullptr; 81 char** Arguments::_jvm_flags_array = nullptr; 82 int Arguments::_num_jvm_flags = 0; 83 char** Arguments::_jvm_args_array = nullptr; 84 int Arguments::_num_jvm_args = 0; 85 char* Arguments::_java_command = nullptr; 86 SystemProperty* Arguments::_system_properties = nullptr; 87 size_t Arguments::_conservative_max_heap_alignment = 0; 88 Arguments::Mode Arguments::_mode = _mixed; 89 const char* Arguments::_java_vendor_url_bug = nullptr; 90 const char* Arguments::_sun_java_launcher = DEFAULT_JAVA_LAUNCHER; 91 bool Arguments::_sun_java_launcher_is_altjvm = false; 92 93 // These parameters are reset in method parse_vm_init_args() 94 bool Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods; 95 bool Arguments::_UseOnStackReplacement = UseOnStackReplacement; 96 bool Arguments::_BackgroundCompilation = BackgroundCompilation; 97 bool Arguments::_ClipInlining = ClipInlining; 98 size_t Arguments::_default_SharedBaseAddress = SharedBaseAddress; 99 100 bool Arguments::_enable_preview = false; 101 102 char* Arguments::_default_shared_archive_path = nullptr; 103 char* Arguments::SharedArchivePath = nullptr; 104 char* Arguments::SharedDynamicArchivePath = nullptr; 105 106 LegacyGCLogging Arguments::_legacyGCLogging = { 0, 0 }; 107 108 // These are not set by the JDK's built-in launchers, but they can be set by 109 // programs that embed the JVM using JNI_CreateJavaVM. See comments around 110 // JavaVMOption in jni.h. 111 abort_hook_t Arguments::_abort_hook = nullptr; 112 exit_hook_t Arguments::_exit_hook = nullptr; 113 vfprintf_hook_t Arguments::_vfprintf_hook = nullptr; 114 115 116 SystemProperty *Arguments::_sun_boot_library_path = nullptr; 117 SystemProperty *Arguments::_java_library_path = nullptr; 118 SystemProperty *Arguments::_java_home = nullptr; 119 SystemProperty *Arguments::_java_class_path = nullptr; 120 SystemProperty *Arguments::_jdk_boot_class_path_append = nullptr; 121 SystemProperty *Arguments::_vm_info = nullptr; 122 123 GrowableArray<ModulePatchPath*> *Arguments::_patch_mod_prefix = nullptr; 124 PathString *Arguments::_boot_class_path = nullptr; 125 bool Arguments::_has_jimage = false; 126 127 char* Arguments::_ext_dirs = nullptr; 128 129 // True if -Xshare:auto option was specified. 130 static bool xshare_auto_cmd_line = false; 131 132 bool PathString::set_value(const char *value, AllocFailType alloc_failmode) { 133 char* new_value = AllocateHeap(strlen(value)+1, mtArguments, alloc_failmode); 134 if (new_value == nullptr) { 135 assert(alloc_failmode == AllocFailStrategy::RETURN_NULL, "must be"); 136 return false; 137 } 138 if (_value != nullptr) { 139 FreeHeap(_value); 140 } 141 _value = new_value; 142 strcpy(_value, value); 143 return true; 144 } 145 146 void PathString::append_value(const char *value) { 147 char *sp; 148 size_t len = 0; 149 if (value != nullptr) { 150 len = strlen(value); 151 if (_value != nullptr) { 152 len += strlen(_value); 153 } 154 sp = AllocateHeap(len+2, mtArguments); 155 assert(sp != nullptr, "Unable to allocate space for new append path value"); 156 if (sp != nullptr) { 157 if (_value != nullptr) { 158 strcpy(sp, _value); 159 strcat(sp, os::path_separator()); 160 strcat(sp, value); 161 FreeHeap(_value); 162 } else { 163 strcpy(sp, value); 164 } 165 _value = sp; 166 } 167 } 168 } 169 170 PathString::PathString(const char* value) { 171 if (value == nullptr) { 172 _value = nullptr; 173 } else { 174 _value = AllocateHeap(strlen(value)+1, mtArguments); 175 strcpy(_value, value); 176 } 177 } 178 179 PathString::~PathString() { 180 if (_value != nullptr) { 181 FreeHeap(_value); 182 _value = nullptr; 183 } 184 } 185 186 ModulePatchPath::ModulePatchPath(const char* module_name, const char* path) { 187 assert(module_name != nullptr && path != nullptr, "Invalid module name or path value"); 188 size_t len = strlen(module_name) + 1; 189 _module_name = AllocateHeap(len, mtInternal); 190 strncpy(_module_name, module_name, len); // copy the trailing null 191 _path = new PathString(path); 192 } 193 194 ModulePatchPath::~ModulePatchPath() { 195 if (_module_name != nullptr) { 196 FreeHeap(_module_name); 197 _module_name = nullptr; 198 } 199 if (_path != nullptr) { 200 delete _path; 201 _path = nullptr; 202 } 203 } 204 205 SystemProperty::SystemProperty(const char* key, const char* value, bool writeable, bool internal) : PathString(value) { 206 if (key == nullptr) { 207 _key = nullptr; 208 } else { 209 _key = AllocateHeap(strlen(key)+1, mtArguments); 210 strcpy(_key, key); 211 } 212 _next = nullptr; 213 _internal = internal; 214 _writeable = writeable; 215 } 216 217 // Check if head of 'option' matches 'name', and sets 'tail' to the remaining 218 // part of the option string. 219 static bool match_option(const JavaVMOption *option, const char* name, 220 const char** tail) { 221 size_t len = strlen(name); 222 if (strncmp(option->optionString, name, len) == 0) { 223 *tail = option->optionString + len; 224 return true; 225 } else { 226 return false; 227 } 228 } 229 230 // Check if 'option' matches 'name'. No "tail" is allowed. 231 static bool match_option(const JavaVMOption *option, const char* name) { 232 const char* tail = nullptr; 233 bool result = match_option(option, name, &tail); 234 if (tail != nullptr && *tail == '\0') { 235 return result; 236 } else { 237 return false; 238 } 239 } 240 241 // Return true if any of the strings in null-terminated array 'names' matches. 242 // If tail_allowed is true, then the tail must begin with a colon; otherwise, 243 // the option must match exactly. 244 static bool match_option(const JavaVMOption* option, const char** names, const char** tail, 245 bool tail_allowed) { 246 for (/* empty */; *names != nullptr; ++names) { 247 if (match_option(option, *names, tail)) { 248 if (**tail == '\0' || (tail_allowed && **tail == ':')) { 249 return true; 250 } 251 } 252 } 253 return false; 254 } 255 256 #if INCLUDE_JFR 257 static bool _has_jfr_option = false; // is using JFR 258 259 // return true on failure 260 static bool match_jfr_option(const JavaVMOption** option) { 261 assert((*option)->optionString != nullptr, "invariant"); 262 char* tail = nullptr; 263 if (match_option(*option, "-XX:StartFlightRecording", (const char**)&tail)) { 264 _has_jfr_option = true; 265 return Jfr::on_start_flight_recording_option(option, tail); 266 } else if (match_option(*option, "-XX:FlightRecorderOptions", (const char**)&tail)) { 267 _has_jfr_option = true; 268 return Jfr::on_flight_recorder_option(option, tail); 269 } 270 return false; 271 } 272 273 bool Arguments::has_jfr_option() { 274 return _has_jfr_option; 275 } 276 #endif 277 278 static void logOption(const char* opt) { 279 if (PrintVMOptions) { 280 jio_fprintf(defaultStream::output_stream(), "VM option '%s'\n", opt); 281 } 282 } 283 284 bool needs_module_property_warning = false; 285 286 #define MODULE_PROPERTY_PREFIX "jdk.module." 287 #define MODULE_PROPERTY_PREFIX_LEN 11 288 #define ADDEXPORTS "addexports" 289 #define ADDEXPORTS_LEN 10 290 #define ADDREADS "addreads" 291 #define ADDREADS_LEN 8 292 #define ADDOPENS "addopens" 293 #define ADDOPENS_LEN 8 294 #define PATCH "patch" 295 #define PATCH_LEN 5 296 #define ADDMODS "addmods" 297 #define ADDMODS_LEN 7 298 #define LIMITMODS "limitmods" 299 #define LIMITMODS_LEN 9 300 #define PATH "path" 301 #define PATH_LEN 4 302 #define UPGRADE_PATH "upgrade.path" 303 #define UPGRADE_PATH_LEN 12 304 #define ENABLE_NATIVE_ACCESS "enable.native.access" 305 #define ENABLE_NATIVE_ACCESS_LEN 20 306 307 // Return TRUE if option matches 'property', or 'property=', or 'property.'. 308 static bool matches_property_suffix(const char* option, const char* property, size_t len) { 309 return ((strncmp(option, property, len) == 0) && 310 (option[len] == '=' || option[len] == '.' || option[len] == '\0')); 311 } 312 313 // Return true if property starts with "jdk.module." and its ensuing chars match 314 // any of the reserved module properties. 315 // property should be passed without the leading "-D". 316 bool Arguments::is_internal_module_property(const char* property) { 317 assert((strncmp(property, "-D", 2) != 0), "Unexpected leading -D"); 318 if (strncmp(property, MODULE_PROPERTY_PREFIX, MODULE_PROPERTY_PREFIX_LEN) == 0) { 319 const char* property_suffix = property + MODULE_PROPERTY_PREFIX_LEN; 320 if (matches_property_suffix(property_suffix, ADDEXPORTS, ADDEXPORTS_LEN) || 321 matches_property_suffix(property_suffix, ADDREADS, ADDREADS_LEN) || 322 matches_property_suffix(property_suffix, ADDOPENS, ADDOPENS_LEN) || 323 matches_property_suffix(property_suffix, PATCH, PATCH_LEN) || 324 matches_property_suffix(property_suffix, ADDMODS, ADDMODS_LEN) || 325 matches_property_suffix(property_suffix, LIMITMODS, LIMITMODS_LEN) || 326 matches_property_suffix(property_suffix, PATH, PATH_LEN) || 327 matches_property_suffix(property_suffix, UPGRADE_PATH, UPGRADE_PATH_LEN) || 328 matches_property_suffix(property_suffix, ENABLE_NATIVE_ACCESS, ENABLE_NATIVE_ACCESS_LEN)) { 329 return true; 330 } 331 } 332 return false; 333 } 334 335 // Process java launcher properties. 336 void Arguments::process_sun_java_launcher_properties(JavaVMInitArgs* args) { 337 // See if sun.java.launcher or sun.java.launcher.is_altjvm is defined. 338 // Must do this before setting up other system properties, 339 // as some of them may depend on launcher type. 340 for (int index = 0; index < args->nOptions; index++) { 341 const JavaVMOption* option = args->options + index; 342 const char* tail; 343 344 if (match_option(option, "-Dsun.java.launcher=", &tail)) { 345 process_java_launcher_argument(tail, option->extraInfo); 346 continue; 347 } 348 if (match_option(option, "-Dsun.java.launcher.is_altjvm=", &tail)) { 349 if (strcmp(tail, "true") == 0) { 350 _sun_java_launcher_is_altjvm = true; 351 } 352 continue; 353 } 354 } 355 } 356 357 // Initialize system properties key and value. 358 void Arguments::init_system_properties() { 359 360 // Set up _boot_class_path which is not a property but 361 // relies heavily on argument processing and the jdk.boot.class.path.append 362 // property. It is used to store the underlying boot class path. 363 _boot_class_path = new PathString(nullptr); 364 365 PropertyList_add(&_system_properties, new SystemProperty("java.vm.specification.name", 366 "Java Virtual Machine Specification", false)); 367 PropertyList_add(&_system_properties, new SystemProperty("java.vm.version", VM_Version::vm_release(), false)); 368 PropertyList_add(&_system_properties, new SystemProperty("java.vm.name", VM_Version::vm_name(), false)); 369 PropertyList_add(&_system_properties, new SystemProperty("jdk.debug", VM_Version::jdk_debug_level(), false)); 370 371 // Initialize the vm.info now, but it will need updating after argument parsing. 372 _vm_info = new SystemProperty("java.vm.info", VM_Version::vm_info_string(), true); 373 374 // Following are JVMTI agent writable properties. 375 // Properties values are set to nullptr and they are 376 // os specific they are initialized in os::init_system_properties_values(). 377 _sun_boot_library_path = new SystemProperty("sun.boot.library.path", nullptr, true); 378 _java_library_path = new SystemProperty("java.library.path", nullptr, true); 379 _java_home = new SystemProperty("java.home", nullptr, true); 380 _java_class_path = new SystemProperty("java.class.path", "", true); 381 // jdk.boot.class.path.append is a non-writeable, internal property. 382 // It can only be set by either: 383 // - -Xbootclasspath/a: 384 // - AddToBootstrapClassLoaderSearch during JVMTI OnLoad phase 385 _jdk_boot_class_path_append = new SystemProperty("jdk.boot.class.path.append", nullptr, false, true); 386 387 // Add to System Property list. 388 PropertyList_add(&_system_properties, _sun_boot_library_path); 389 PropertyList_add(&_system_properties, _java_library_path); 390 PropertyList_add(&_system_properties, _java_home); 391 PropertyList_add(&_system_properties, _java_class_path); 392 PropertyList_add(&_system_properties, _jdk_boot_class_path_append); 393 PropertyList_add(&_system_properties, _vm_info); 394 395 // Set OS specific system properties values 396 os::init_system_properties_values(); 397 } 398 399 // Update/Initialize System properties after JDK version number is known 400 void Arguments::init_version_specific_system_properties() { 401 enum { bufsz = 16 }; 402 char buffer[bufsz]; 403 const char* spec_vendor = "Oracle Corporation"; 404 uint32_t spec_version = JDK_Version::current().major_version(); 405 406 jio_snprintf(buffer, bufsz, UINT32_FORMAT, spec_version); 407 408 PropertyList_add(&_system_properties, 409 new SystemProperty("java.vm.specification.vendor", spec_vendor, false)); 410 PropertyList_add(&_system_properties, 411 new SystemProperty("java.vm.specification.version", buffer, false)); 412 PropertyList_add(&_system_properties, 413 new SystemProperty("java.vm.vendor", VM_Version::vm_vendor(), false)); 414 } 415 416 /* 417 * -XX argument processing: 418 * 419 * -XX arguments are defined in several places, such as: 420 * globals.hpp, globals_<cpu>.hpp, globals_<os>.hpp, <compiler>_globals.hpp, or <gc>_globals.hpp. 421 * -XX arguments are parsed in parse_argument(). 422 * -XX argument bounds checking is done in check_vm_args_consistency(). 423 * 424 * Over time -XX arguments may change. There are mechanisms to handle common cases: 425 * 426 * ALIASED: An option that is simply another name for another option. This is often 427 * part of the process of deprecating a flag, but not all aliases need 428 * to be deprecated. 429 * 430 * Create an alias for an option by adding the old and new option names to the 431 * "aliased_jvm_flags" table. Delete the old variable from globals.hpp (etc). 432 * 433 * DEPRECATED: An option that is supported, but a warning is printed to let the user know that 434 * support may be removed in the future. Both regular and aliased options may be 435 * deprecated. 436 * 437 * Add a deprecation warning for an option (or alias) by adding an entry in the 438 * "special_jvm_flags" table and setting the "deprecated_in" field. 439 * Often an option "deprecated" in one major release will 440 * be made "obsolete" in the next. In this case the entry should also have its 441 * "obsolete_in" field set. 442 * 443 * OBSOLETE: An option that has been removed (and deleted from globals.hpp), but is still accepted 444 * on the command line. A warning is printed to let the user know that option might not 445 * be accepted in the future. 446 * 447 * Add an obsolete warning for an option by adding an entry in the "special_jvm_flags" 448 * table and setting the "obsolete_in" field. 449 * 450 * EXPIRED: A deprecated or obsolete option that has an "accept_until" version less than or equal 451 * to the current JDK version. The system will flatly refuse to admit the existence of 452 * the flag. This allows a flag to die automatically over JDK releases. 453 * 454 * Note that manual cleanup of expired options should be done at major JDK version upgrades: 455 * - Newly expired options should be removed from the special_jvm_flags and aliased_jvm_flags tables. 456 * - Newly obsolete or expired deprecated options should have their global variable 457 * definitions removed (from globals.hpp, etc) and related implementations removed. 458 * 459 * Recommended approach for removing options: 460 * 461 * To remove options commonly used by customers (e.g. product -XX options), use 462 * the 3-step model adding major release numbers to the deprecate, obsolete and expire columns. 463 * 464 * To remove internal options (e.g. diagnostic, experimental, develop options), use 465 * a 2-step model adding major release numbers to the obsolete and expire columns. 466 * 467 * To change the name of an option, use the alias table as well as a 2-step 468 * model adding major release numbers to the deprecate and expire columns. 469 * Think twice about aliasing commonly used customer options. 470 * 471 * There are times when it is appropriate to leave a future release number as undefined. 472 * 473 * Tests: Aliases should be tested in VMAliasOptions.java. 474 * Deprecated options should be tested in VMDeprecatedOptions.java. 475 */ 476 477 // The special_jvm_flags table declares options that are being deprecated and/or obsoleted. The 478 // "deprecated_in" or "obsolete_in" fields may be set to "undefined", but not both. 479 // When the JDK version reaches 'deprecated_in' limit, the JVM will process this flag on 480 // the command-line as usual, but will issue a warning. 481 // When the JDK version reaches 'obsolete_in' limit, the JVM will continue accepting this flag on 482 // the command-line, while issuing a warning and ignoring the flag value. 483 // Once the JDK version reaches 'expired_in' limit, the JVM will flatly refuse to admit the 484 // existence of the flag. 485 // 486 // MANUAL CLEANUP ON JDK VERSION UPDATES: 487 // This table ensures that the handling of options will update automatically when the JDK 488 // version is incremented, but the source code needs to be cleanup up manually: 489 // - As "deprecated" options age into "obsolete" or "expired" options, the associated "globals" 490 // variable should be removed, as well as users of the variable. 491 // - As "deprecated" options age into "obsolete" options, move the entry into the 492 // "Obsolete Flags" section of the table. 493 // - All expired options should be removed from the table. 494 static SpecialFlag const special_jvm_flags[] = { 495 // -------------- Deprecated Flags -------------- 496 // --- Non-alias flags - sorted by obsolete_in then expired_in: 497 { "MaxGCMinorPauseMillis", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::undefined() }, 498 { "MaxRAMFraction", JDK_Version::jdk(10), JDK_Version::undefined(), JDK_Version::undefined() }, 499 { "MinRAMFraction", JDK_Version::jdk(10), JDK_Version::undefined(), JDK_Version::undefined() }, 500 { "InitialRAMFraction", JDK_Version::jdk(10), JDK_Version::undefined(), JDK_Version::undefined() }, 501 { "AllowRedefinitionToAddDeleteMethods", JDK_Version::jdk(13), JDK_Version::undefined(), JDK_Version::undefined() }, 502 { "FlightRecorder", JDK_Version::jdk(13), JDK_Version::undefined(), JDK_Version::undefined() }, 503 { "DumpSharedSpaces", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::undefined() }, 504 { "DynamicDumpSharedSpaces", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::undefined() }, 505 { "RequireSharedSpaces", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::undefined() }, 506 { "UseSharedSpaces", JDK_Version::jdk(18), JDK_Version::jdk(19), JDK_Version::undefined() }, 507 508 // --- Deprecated alias flags (see also aliased_jvm_flags) - sorted by obsolete_in then expired_in: 509 { "DefaultMaxRAMFraction", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::undefined() }, 510 { "CreateMinidumpOnCrash", JDK_Version::jdk(9), JDK_Version::undefined(), JDK_Version::undefined() }, 511 { "TLABStats", JDK_Version::jdk(12), JDK_Version::undefined(), JDK_Version::undefined() }, 512 513 // -------------- Obsolete Flags - sorted by expired_in -------------- 514 515 { "G1ConcRefinementGreenZone", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 516 { "G1ConcRefinementYellowZone", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 517 { "G1ConcRefinementRedZone", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 518 { "G1ConcRefinementThresholdStep", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 519 { "G1UseAdaptiveConcRefinement", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 520 { "G1ConcRefinementServiceIntervalMillis", JDK_Version::undefined(), JDK_Version::jdk(20), JDK_Version::undefined() }, 521 522 { "G1ConcRSLogCacheSize", JDK_Version::undefined(), JDK_Version::jdk(21), JDK_Version::undefined() }, 523 { "G1ConcRSHotCardLimit", JDK_Version::undefined(), JDK_Version::jdk(21), JDK_Version::undefined() }, 524 { "RefDiscoveryPolicy", JDK_Version::undefined(), JDK_Version::jdk(21), JDK_Version::undefined() }, 525 { "MetaspaceReclaimPolicy", JDK_Version::undefined(), JDK_Version::jdk(21), JDK_Version::undefined() }, 526 527 #ifdef ASSERT 528 { "DummyObsoleteTestFlag", JDK_Version::undefined(), JDK_Version::jdk(18), JDK_Version::undefined() }, 529 #endif 530 531 #ifdef TEST_VERIFY_SPECIAL_JVM_FLAGS 532 // These entries will generate build errors. Their purpose is to test the macros. 533 { "dep > obs", JDK_Version::jdk(9), JDK_Version::jdk(8), JDK_Version::undefined() }, 534 { "dep > exp ", JDK_Version::jdk(9), JDK_Version::undefined(), JDK_Version::jdk(8) }, 535 { "obs > exp ", JDK_Version::undefined(), JDK_Version::jdk(9), JDK_Version::jdk(8) }, 536 { "obs > exp", JDK_Version::jdk(8), JDK_Version::undefined(), JDK_Version::jdk(10) }, 537 { "not deprecated or obsolete", JDK_Version::undefined(), JDK_Version::undefined(), JDK_Version::jdk(9) }, 538 { "dup option", JDK_Version::jdk(9), JDK_Version::undefined(), JDK_Version::undefined() }, 539 { "dup option", JDK_Version::jdk(9), JDK_Version::undefined(), JDK_Version::undefined() }, 540 #endif 541 542 { nullptr, JDK_Version(0), JDK_Version(0) } 543 }; 544 545 // Flags that are aliases for other flags. 546 typedef struct { 547 const char* alias_name; 548 const char* real_name; 549 } AliasedFlag; 550 551 static AliasedFlag const aliased_jvm_flags[] = { 552 { "DefaultMaxRAMFraction", "MaxRAMFraction" }, 553 { "CreateMinidumpOnCrash", "CreateCoredumpOnCrash" }, 554 { nullptr, nullptr} 555 }; 556 557 // Return true if "v" is less than "other", where "other" may be "undefined". 558 static bool version_less_than(JDK_Version v, JDK_Version other) { 559 assert(!v.is_undefined(), "must be defined"); 560 if (!other.is_undefined() && v.compare(other) >= 0) { 561 return false; 562 } else { 563 return true; 564 } 565 } 566 567 static bool lookup_special_flag(const char *flag_name, SpecialFlag& flag) { 568 for (size_t i = 0; special_jvm_flags[i].name != nullptr; i++) { 569 if ((strcmp(special_jvm_flags[i].name, flag_name) == 0)) { 570 flag = special_jvm_flags[i]; 571 return true; 572 } 573 } 574 return false; 575 } 576 577 bool Arguments::is_obsolete_flag(const char *flag_name, JDK_Version* version) { 578 assert(version != nullptr, "Must provide a version buffer"); 579 SpecialFlag flag; 580 if (lookup_special_flag(flag_name, flag)) { 581 if (!flag.obsolete_in.is_undefined()) { 582 if (!version_less_than(JDK_Version::current(), flag.obsolete_in)) { 583 *version = flag.obsolete_in; 584 // This flag may have been marked for obsoletion in this version, but we may not 585 // have actually removed it yet. Rather than ignoring it as soon as we reach 586 // this version we allow some time for the removal to happen. So if the flag 587 // still actually exists we process it as normal, but issue an adjusted warning. 588 const JVMFlag *real_flag = JVMFlag::find_declared_flag(flag_name); 589 if (real_flag != nullptr) { 590 char version_str[256]; 591 version->to_string(version_str, sizeof(version_str)); 592 warning("Temporarily processing option %s; support is scheduled for removal in %s", 593 flag_name, version_str); 594 return false; 595 } 596 return true; 597 } 598 } 599 } 600 return false; 601 } 602 603 int Arguments::is_deprecated_flag(const char *flag_name, JDK_Version* version) { 604 assert(version != nullptr, "Must provide a version buffer"); 605 SpecialFlag flag; 606 if (lookup_special_flag(flag_name, flag)) { 607 if (!flag.deprecated_in.is_undefined()) { 608 if (version_less_than(JDK_Version::current(), flag.obsolete_in) && 609 version_less_than(JDK_Version::current(), flag.expired_in)) { 610 *version = flag.deprecated_in; 611 return 1; 612 } else { 613 return -1; 614 } 615 } 616 } 617 return 0; 618 } 619 620 const char* Arguments::real_flag_name(const char *flag_name) { 621 for (size_t i = 0; aliased_jvm_flags[i].alias_name != nullptr; i++) { 622 const AliasedFlag& flag_status = aliased_jvm_flags[i]; 623 if (strcmp(flag_status.alias_name, flag_name) == 0) { 624 return flag_status.real_name; 625 } 626 } 627 return flag_name; 628 } 629 630 #ifdef ASSERT 631 static bool lookup_special_flag(const char *flag_name, size_t skip_index) { 632 for (size_t i = 0; special_jvm_flags[i].name != nullptr; i++) { 633 if ((i != skip_index) && (strcmp(special_jvm_flags[i].name, flag_name) == 0)) { 634 return true; 635 } 636 } 637 return false; 638 } 639 640 // Verifies the correctness of the entries in the special_jvm_flags table. 641 // If there is a semantic error (i.e. a bug in the table) such as the obsoletion 642 // version being earlier than the deprecation version, then a warning is issued 643 // and verification fails - by returning false. If it is detected that the table 644 // is out of date, with respect to the current version, then ideally a warning is 645 // issued but verification does not fail. This allows the VM to operate when the 646 // version is first updated, without needing to update all the impacted flags at 647 // the same time. In practice we can't issue the warning immediately when the version 648 // is updated as it occurs for every test and some tests are not prepared to handle 649 // unexpected output - see 8196739. Instead we only check if the table is up-to-date 650 // if the check_globals flag is true, and in addition allow a grace period and only 651 // check for stale flags when we hit build 25 (which is far enough into the 6 month 652 // release cycle that all flag updates should have been processed, whilst still 653 // leaving time to make the change before RDP2). 654 // We use a gtest to call this, passing true, so that we can detect stale flags before 655 // the end of the release cycle. 656 657 static const int SPECIAL_FLAG_VALIDATION_BUILD = 25; 658 659 bool Arguments::verify_special_jvm_flags(bool check_globals) { 660 bool success = true; 661 for (size_t i = 0; special_jvm_flags[i].name != nullptr; i++) { 662 const SpecialFlag& flag = special_jvm_flags[i]; 663 if (lookup_special_flag(flag.name, i)) { 664 warning("Duplicate special flag declaration \"%s\"", flag.name); 665 success = false; 666 } 667 if (flag.deprecated_in.is_undefined() && 668 flag.obsolete_in.is_undefined()) { 669 warning("Special flag entry \"%s\" must declare version deprecated and/or obsoleted in.", flag.name); 670 success = false; 671 } 672 673 if (!flag.deprecated_in.is_undefined()) { 674 if (!version_less_than(flag.deprecated_in, flag.obsolete_in)) { 675 warning("Special flag entry \"%s\" must be deprecated before obsoleted.", flag.name); 676 success = false; 677 } 678 679 if (!version_less_than(flag.deprecated_in, flag.expired_in)) { 680 warning("Special flag entry \"%s\" must be deprecated before expired.", flag.name); 681 success = false; 682 } 683 } 684 685 if (!flag.obsolete_in.is_undefined()) { 686 if (!version_less_than(flag.obsolete_in, flag.expired_in)) { 687 warning("Special flag entry \"%s\" must be obsoleted before expired.", flag.name); 688 success = false; 689 } 690 691 // if flag has become obsolete it should not have a "globals" flag defined anymore. 692 if (check_globals && VM_Version::vm_build_number() >= SPECIAL_FLAG_VALIDATION_BUILD && 693 !version_less_than(JDK_Version::current(), flag.obsolete_in)) { 694 if (JVMFlag::find_declared_flag(flag.name) != nullptr) { 695 warning("Global variable for obsolete special flag entry \"%s\" should be removed", flag.name); 696 success = false; 697 } 698 } 699 700 } else if (!flag.expired_in.is_undefined()) { 701 warning("Special flag entry \"%s\" must be explicitly obsoleted before expired.", flag.name); 702 success = false; 703 } 704 705 if (!flag.expired_in.is_undefined()) { 706 // if flag has become expired it should not have a "globals" flag defined anymore. 707 if (check_globals && VM_Version::vm_build_number() >= SPECIAL_FLAG_VALIDATION_BUILD && 708 !version_less_than(JDK_Version::current(), flag.expired_in)) { 709 if (JVMFlag::find_declared_flag(flag.name) != nullptr) { 710 warning("Global variable for expired flag entry \"%s\" should be removed", flag.name); 711 success = false; 712 } 713 } 714 } 715 } 716 return success; 717 } 718 #endif 719 720 bool Arguments::atojulong(const char *s, julong* result) { 721 return parse_integer(s, result); 722 } 723 724 Arguments::ArgsRange Arguments::check_memory_size(julong size, julong min_size, julong max_size) { 725 if (size < min_size) return arg_too_small; 726 if (size > max_size) return arg_too_big; 727 return arg_in_range; 728 } 729 730 // Describe an argument out of range error 731 void Arguments::describe_range_error(ArgsRange errcode) { 732 switch(errcode) { 733 case arg_too_big: 734 jio_fprintf(defaultStream::error_stream(), 735 "The specified size exceeds the maximum " 736 "representable size.\n"); 737 break; 738 case arg_too_small: 739 case arg_unreadable: 740 case arg_in_range: 741 // do nothing for now 742 break; 743 default: 744 ShouldNotReachHere(); 745 } 746 } 747 748 static bool set_bool_flag(JVMFlag* flag, bool value, JVMFlagOrigin origin) { 749 if (JVMFlagAccess::set_bool(flag, &value, origin) == JVMFlag::SUCCESS) { 750 return true; 751 } else { 752 return false; 753 } 754 } 755 756 static bool set_fp_numeric_flag(JVMFlag* flag, const char* value, JVMFlagOrigin origin) { 757 // strtod allows leading whitespace, but our flag format does not. 758 if (*value == '\0' || isspace(*value)) { 759 return false; 760 } 761 char* end; 762 errno = 0; 763 double v = strtod(value, &end); 764 if ((errno != 0) || (*end != 0)) { 765 return false; 766 } 767 if (g_isnan(v) || !g_isfinite(v)) { 768 // Currently we cannot handle these special values. 769 return false; 770 } 771 772 if (JVMFlagAccess::set_double(flag, &v, origin) == JVMFlag::SUCCESS) { 773 return true; 774 } 775 return false; 776 } 777 778 static bool set_numeric_flag(JVMFlag* flag, const char* value, JVMFlagOrigin origin) { 779 JVMFlag::Error result = JVMFlag::WRONG_FORMAT; 780 781 if (flag->is_int()) { 782 int v; 783 if (parse_integer(value, &v)) { 784 result = JVMFlagAccess::set_int(flag, &v, origin); 785 } 786 } else if (flag->is_uint()) { 787 uint v; 788 if (parse_integer(value, &v)) { 789 result = JVMFlagAccess::set_uint(flag, &v, origin); 790 } 791 } else if (flag->is_intx()) { 792 intx v; 793 if (parse_integer(value, &v)) { 794 result = JVMFlagAccess::set_intx(flag, &v, origin); 795 } 796 } else if (flag->is_uintx()) { 797 uintx v; 798 if (parse_integer(value, &v)) { 799 result = JVMFlagAccess::set_uintx(flag, &v, origin); 800 } 801 } else if (flag->is_uint64_t()) { 802 uint64_t v; 803 if (parse_integer(value, &v)) { 804 result = JVMFlagAccess::set_uint64_t(flag, &v, origin); 805 } 806 } else if (flag->is_size_t()) { 807 size_t v; 808 if (parse_integer(value, &v)) { 809 result = JVMFlagAccess::set_size_t(flag, &v, origin); 810 } 811 } 812 813 return result == JVMFlag::SUCCESS; 814 } 815 816 static bool set_string_flag(JVMFlag* flag, const char* value, JVMFlagOrigin origin) { 817 if (value[0] == '\0') { 818 value = nullptr; 819 } 820 if (JVMFlagAccess::set_ccstr(flag, &value, origin) != JVMFlag::SUCCESS) return false; 821 // Contract: JVMFlag always returns a pointer that needs freeing. 822 FREE_C_HEAP_ARRAY(char, value); 823 return true; 824 } 825 826 static bool append_to_string_flag(JVMFlag* flag, const char* new_value, JVMFlagOrigin origin) { 827 const char* old_value = ""; 828 if (JVMFlagAccess::get_ccstr(flag, &old_value) != JVMFlag::SUCCESS) return false; 829 size_t old_len = old_value != nullptr ? strlen(old_value) : 0; 830 size_t new_len = strlen(new_value); 831 const char* value; 832 char* free_this_too = nullptr; 833 if (old_len == 0) { 834 value = new_value; 835 } else if (new_len == 0) { 836 value = old_value; 837 } else { 838 size_t length = old_len + 1 + new_len + 1; 839 char* buf = NEW_C_HEAP_ARRAY(char, length, mtArguments); 840 // each new setting adds another LINE to the switch: 841 jio_snprintf(buf, length, "%s\n%s", old_value, new_value); 842 value = buf; 843 free_this_too = buf; 844 } 845 (void) JVMFlagAccess::set_ccstr(flag, &value, origin); 846 // JVMFlag always returns a pointer that needs freeing. 847 FREE_C_HEAP_ARRAY(char, value); 848 // JVMFlag made its own copy, so I must delete my own temp. buffer. 849 FREE_C_HEAP_ARRAY(char, free_this_too); 850 return true; 851 } 852 853 const char* Arguments::handle_aliases_and_deprecation(const char* arg) { 854 const char* real_name = real_flag_name(arg); 855 JDK_Version since = JDK_Version(); 856 switch (is_deprecated_flag(arg, &since)) { 857 case -1: { 858 // Obsolete or expired, so don't process normally, 859 // but allow for an obsolete flag we're still 860 // temporarily allowing. 861 if (!is_obsolete_flag(arg, &since)) { 862 return real_name; 863 } 864 // Note if we're not considered obsolete then we can't be expired either 865 // as obsoletion must come first. 866 return nullptr; 867 } 868 case 0: 869 return real_name; 870 case 1: { 871 char version[256]; 872 since.to_string(version, sizeof(version)); 873 if (real_name != arg) { 874 warning("Option %s was deprecated in version %s and will likely be removed in a future release. Use option %s instead.", 875 arg, version, real_name); 876 } else { 877 warning("Option %s was deprecated in version %s and will likely be removed in a future release.", 878 arg, version); 879 } 880 return real_name; 881 } 882 } 883 ShouldNotReachHere(); 884 return nullptr; 885 } 886 887 #define BUFLEN 255 888 889 JVMFlag* Arguments::find_jvm_flag(const char* name, size_t name_length) { 890 char name_copied[BUFLEN+1]; 891 if (name[name_length] != 0) { 892 if (name_length > BUFLEN) { 893 return nullptr; 894 } else { 895 strncpy(name_copied, name, name_length); 896 name_copied[name_length] = '\0'; 897 name = name_copied; 898 } 899 } 900 901 const char* real_name = Arguments::handle_aliases_and_deprecation(name); 902 if (real_name == nullptr) { 903 return nullptr; 904 } 905 JVMFlag* flag = JVMFlag::find_flag(real_name); 906 return flag; 907 } 908 909 bool Arguments::parse_argument(const char* arg, JVMFlagOrigin origin) { 910 bool is_bool = false; 911 bool bool_val = false; 912 char c = *arg; 913 if (c == '+' || c == '-') { 914 is_bool = true; 915 bool_val = (c == '+'); 916 arg++; 917 } 918 919 const char* name = arg; 920 while (true) { 921 c = *arg; 922 if (isalnum(c) || (c == '_')) { 923 ++arg; 924 } else { 925 break; 926 } 927 } 928 929 size_t name_len = size_t(arg - name); 930 if (name_len == 0) { 931 return false; 932 } 933 934 JVMFlag* flag = find_jvm_flag(name, name_len); 935 if (flag == nullptr) { 936 return false; 937 } 938 939 if (is_bool) { 940 if (*arg != 0) { 941 // Error -- extra characters such as -XX:+BoolFlag=123 942 return false; 943 } 944 return set_bool_flag(flag, bool_val, origin); 945 } 946 947 if (arg[0] == '=') { 948 const char* value = arg + 1; 949 if (flag->is_ccstr()) { 950 if (flag->ccstr_accumulates()) { 951 return append_to_string_flag(flag, value, origin); 952 } else { 953 return set_string_flag(flag, value, origin); 954 } 955 } else if (flag->is_double()) { 956 return set_fp_numeric_flag(flag, value, origin); 957 } else { 958 return set_numeric_flag(flag, value, origin); 959 } 960 } 961 962 if (arg[0] == ':' && arg[1] == '=') { 963 // -XX:Foo:=xxx will reset the string flag to the given value. 964 const char* value = arg + 2; 965 return set_string_flag(flag, value, origin); 966 } 967 968 return false; 969 } 970 971 void Arguments::add_string(char*** bldarray, int* count, const char* arg) { 972 assert(bldarray != nullptr, "illegal argument"); 973 974 if (arg == nullptr) { 975 return; 976 } 977 978 int new_count = *count + 1; 979 980 // expand the array and add arg to the last element 981 if (*bldarray == nullptr) { 982 *bldarray = NEW_C_HEAP_ARRAY(char*, new_count, mtArguments); 983 } else { 984 *bldarray = REALLOC_C_HEAP_ARRAY(char*, *bldarray, new_count, mtArguments); 985 } 986 (*bldarray)[*count] = os::strdup_check_oom(arg); 987 *count = new_count; 988 } 989 990 void Arguments::build_jvm_args(const char* arg) { 991 add_string(&_jvm_args_array, &_num_jvm_args, arg); 992 } 993 994 void Arguments::build_jvm_flags(const char* arg) { 995 add_string(&_jvm_flags_array, &_num_jvm_flags, arg); 996 } 997 998 // utility function to return a string that concatenates all 999 // strings in a given char** array 1000 const char* Arguments::build_resource_string(char** args, int count) { 1001 if (args == nullptr || count == 0) { 1002 return nullptr; 1003 } 1004 size_t length = 0; 1005 for (int i = 0; i < count; i++) { 1006 length += strlen(args[i]) + 1; // add 1 for a space or null terminating character 1007 } 1008 char* s = NEW_RESOURCE_ARRAY(char, length); 1009 char* dst = s; 1010 for (int j = 0; j < count; j++) { 1011 size_t offset = strlen(args[j]) + 1; // add 1 for a space or null terminating character 1012 jio_snprintf(dst, length, "%s ", args[j]); // jio_snprintf will replace the last space character with null character 1013 dst += offset; 1014 length -= offset; 1015 } 1016 return (const char*) s; 1017 } 1018 1019 void Arguments::print_on(outputStream* st) { 1020 st->print_cr("VM Arguments:"); 1021 if (num_jvm_flags() > 0) { 1022 st->print("jvm_flags: "); print_jvm_flags_on(st); 1023 st->cr(); 1024 } 1025 if (num_jvm_args() > 0) { 1026 st->print("jvm_args: "); print_jvm_args_on(st); 1027 st->cr(); 1028 } 1029 st->print_cr("java_command: %s", java_command() ? java_command() : "<unknown>"); 1030 if (_java_class_path != nullptr) { 1031 char* path = _java_class_path->value(); 1032 size_t len = strlen(path); 1033 st->print("java_class_path (initial): "); 1034 // Avoid using st->print_cr() because path length maybe longer than O_BUFLEN. 1035 if (len == 0) { 1036 st->print_raw_cr("<not set>"); 1037 } else { 1038 st->print_raw_cr(path, len); 1039 } 1040 } 1041 st->print_cr("Launcher Type: %s", _sun_java_launcher); 1042 } 1043 1044 void Arguments::print_summary_on(outputStream* st) { 1045 // Print the command line. Environment variables that are helpful for 1046 // reproducing the problem are written later in the hs_err file. 1047 // flags are from setting file 1048 if (num_jvm_flags() > 0) { 1049 st->print_raw("Settings File: "); 1050 print_jvm_flags_on(st); 1051 st->cr(); 1052 } 1053 // args are the command line and environment variable arguments. 1054 st->print_raw("Command Line: "); 1055 if (num_jvm_args() > 0) { 1056 print_jvm_args_on(st); 1057 } 1058 // this is the classfile and any arguments to the java program 1059 if (java_command() != nullptr) { 1060 st->print("%s", java_command()); 1061 } 1062 st->cr(); 1063 } 1064 1065 void Arguments::print_jvm_flags_on(outputStream* st) { 1066 if (_num_jvm_flags > 0) { 1067 for (int i=0; i < _num_jvm_flags; i++) { 1068 st->print("%s ", _jvm_flags_array[i]); 1069 } 1070 } 1071 } 1072 1073 void Arguments::print_jvm_args_on(outputStream* st) { 1074 if (_num_jvm_args > 0) { 1075 for (int i=0; i < _num_jvm_args; i++) { 1076 st->print("%s ", _jvm_args_array[i]); 1077 } 1078 } 1079 } 1080 1081 bool Arguments::process_argument(const char* arg, 1082 jboolean ignore_unrecognized, 1083 JVMFlagOrigin origin) { 1084 JDK_Version since = JDK_Version(); 1085 1086 if (parse_argument(arg, origin)) { 1087 return true; 1088 } 1089 1090 // Determine if the flag has '+', '-', or '=' characters. 1091 bool has_plus_minus = (*arg == '+' || *arg == '-'); 1092 const char* const argname = has_plus_minus ? arg + 1 : arg; 1093 1094 size_t arg_len; 1095 const char* equal_sign = strchr(argname, '='); 1096 if (equal_sign == nullptr) { 1097 arg_len = strlen(argname); 1098 } else { 1099 arg_len = equal_sign - argname; 1100 } 1101 1102 // Only make the obsolete check for valid arguments. 1103 if (arg_len <= BUFLEN) { 1104 // Construct a string which consists only of the argument name without '+', '-', or '='. 1105 char stripped_argname[BUFLEN+1]; // +1 for '\0' 1106 jio_snprintf(stripped_argname, arg_len+1, "%s", argname); // +1 for '\0' 1107 if (is_obsolete_flag(stripped_argname, &since)) { 1108 char version[256]; 1109 since.to_string(version, sizeof(version)); 1110 warning("Ignoring option %s; support was removed in %s", stripped_argname, version); 1111 return true; 1112 } 1113 } 1114 1115 // For locked flags, report a custom error message if available. 1116 // Otherwise, report the standard unrecognized VM option. 1117 const JVMFlag* found_flag = JVMFlag::find_declared_flag((const char*)argname, arg_len); 1118 if (found_flag != nullptr) { 1119 char locked_message_buf[BUFLEN]; 1120 JVMFlag::MsgType msg_type = found_flag->get_locked_message(locked_message_buf, BUFLEN); 1121 if (strlen(locked_message_buf) == 0) { 1122 if (found_flag->is_bool() && !has_plus_minus) { 1123 jio_fprintf(defaultStream::error_stream(), 1124 "Missing +/- setting for VM option '%s'\n", argname); 1125 } else if (!found_flag->is_bool() && has_plus_minus) { 1126 jio_fprintf(defaultStream::error_stream(), 1127 "Unexpected +/- setting in VM option '%s'\n", argname); 1128 } else { 1129 jio_fprintf(defaultStream::error_stream(), 1130 "Improperly specified VM option '%s'\n", argname); 1131 } 1132 } else { 1133 #ifdef PRODUCT 1134 bool mismatched = ((msg_type == JVMFlag::NOTPRODUCT_FLAG_BUT_PRODUCT_BUILD) || 1135 (msg_type == JVMFlag::DEVELOPER_FLAG_BUT_PRODUCT_BUILD)); 1136 if (ignore_unrecognized && mismatched) { 1137 return true; 1138 } 1139 #endif 1140 jio_fprintf(defaultStream::error_stream(), "%s", locked_message_buf); 1141 } 1142 } else { 1143 if (ignore_unrecognized) { 1144 return true; 1145 } 1146 jio_fprintf(defaultStream::error_stream(), 1147 "Unrecognized VM option '%s'\n", argname); 1148 JVMFlag* fuzzy_matched = JVMFlag::fuzzy_match((const char*)argname, arg_len, true); 1149 if (fuzzy_matched != nullptr) { 1150 jio_fprintf(defaultStream::error_stream(), 1151 "Did you mean '%s%s%s'?\n", 1152 (fuzzy_matched->is_bool()) ? "(+/-)" : "", 1153 fuzzy_matched->name(), 1154 (fuzzy_matched->is_bool()) ? "" : "=<value>"); 1155 } 1156 } 1157 1158 // allow for commandline "commenting out" options like -XX:#+Verbose 1159 return arg[0] == '#'; 1160 } 1161 1162 bool Arguments::process_settings_file(const char* file_name, bool should_exist, jboolean ignore_unrecognized) { 1163 FILE* stream = os::fopen(file_name, "rb"); 1164 if (stream == nullptr) { 1165 if (should_exist) { 1166 jio_fprintf(defaultStream::error_stream(), 1167 "Could not open settings file %s\n", file_name); 1168 return false; 1169 } else { 1170 return true; 1171 } 1172 } 1173 1174 char token[1024]; 1175 int pos = 0; 1176 1177 bool in_white_space = true; 1178 bool in_comment = false; 1179 bool in_quote = false; 1180 int quote_c = 0; 1181 bool result = true; 1182 1183 int c = getc(stream); 1184 while(c != EOF && pos < (int)(sizeof(token)-1)) { 1185 if (in_white_space) { 1186 if (in_comment) { 1187 if (c == '\n') in_comment = false; 1188 } else { 1189 if (c == '#') in_comment = true; 1190 else if (!isspace(c)) { 1191 in_white_space = false; 1192 token[pos++] = checked_cast<char>(c); 1193 } 1194 } 1195 } else { 1196 if (c == '\n' || (!in_quote && isspace(c))) { 1197 // token ends at newline, or at unquoted whitespace 1198 // this allows a way to include spaces in string-valued options 1199 token[pos] = '\0'; 1200 logOption(token); 1201 result &= process_argument(token, ignore_unrecognized, JVMFlagOrigin::CONFIG_FILE); 1202 build_jvm_flags(token); 1203 pos = 0; 1204 in_white_space = true; 1205 in_quote = false; 1206 } else if (!in_quote && (c == '\'' || c == '"')) { 1207 in_quote = true; 1208 quote_c = c; 1209 } else if (in_quote && (c == quote_c)) { 1210 in_quote = false; 1211 } else { 1212 token[pos++] = checked_cast<char>(c); 1213 } 1214 } 1215 c = getc(stream); 1216 } 1217 if (pos > 0) { 1218 token[pos] = '\0'; 1219 result &= process_argument(token, ignore_unrecognized, JVMFlagOrigin::CONFIG_FILE); 1220 build_jvm_flags(token); 1221 } 1222 fclose(stream); 1223 return result; 1224 } 1225 1226 //============================================================================================================= 1227 // Parsing of properties (-D) 1228 1229 const char* Arguments::get_property(const char* key) { 1230 return PropertyList_get_value(system_properties(), key); 1231 } 1232 1233 bool Arguments::add_property(const char* prop, PropertyWriteable writeable, PropertyInternal internal) { 1234 const char* eq = strchr(prop, '='); 1235 const char* key; 1236 const char* value = ""; 1237 1238 if (eq == nullptr) { 1239 // property doesn't have a value, thus use passed string 1240 key = prop; 1241 } else { 1242 // property have a value, thus extract it and save to the 1243 // allocated string 1244 size_t key_len = eq - prop; 1245 char* tmp_key = AllocateHeap(key_len + 1, mtArguments); 1246 1247 jio_snprintf(tmp_key, key_len + 1, "%s", prop); 1248 key = tmp_key; 1249 1250 value = &prop[key_len + 1]; 1251 } 1252 1253 #if INCLUDE_CDS 1254 if (is_internal_module_property(key) || 1255 strcmp(key, "jdk.module.main") == 0) { 1256 MetaspaceShared::disable_optimized_module_handling(); 1257 log_info(cds)("optimized module handling: disabled due to incompatible property: %s=%s", key, value); 1258 } 1259 if (strcmp(key, "jdk.module.showModuleResolution") == 0 || 1260 strcmp(key, "jdk.module.validation") == 0 || 1261 strcmp(key, "java.system.class.loader") == 0) { 1262 MetaspaceShared::disable_full_module_graph(); 1263 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value); 1264 } 1265 #endif 1266 1267 if (strcmp(key, "java.compiler") == 0) { 1268 // we no longer support java.compiler system property, log a warning and let it get 1269 // passed to Java, like any other system property 1270 if (strlen(value) == 0 || strcasecmp(value, "NONE") == 0) { 1271 // for applications using NONE or empty value, log a more informative message 1272 warning("The java.compiler system property is obsolete and no longer supported, use -Xint"); 1273 } else { 1274 warning("The java.compiler system property is obsolete and no longer supported."); 1275 } 1276 } else if (strcmp(key, "sun.java.launcher.is_altjvm") == 0) { 1277 // sun.java.launcher.is_altjvm property is 1278 // private and is processed in process_sun_java_launcher_properties(); 1279 // the sun.java.launcher property is passed on to the java application 1280 } else if (strcmp(key, "sun.boot.library.path") == 0) { 1281 // append is true, writable is true, internal is false 1282 PropertyList_unique_add(&_system_properties, key, value, AppendProperty, 1283 WriteableProperty, ExternalProperty); 1284 } else { 1285 if (strcmp(key, "sun.java.command") == 0) { 1286 char *old_java_command = _java_command; 1287 _java_command = os::strdup_check_oom(value, mtArguments); 1288 if (old_java_command != nullptr) { 1289 os::free(old_java_command); 1290 } 1291 } else if (strcmp(key, "java.vendor.url.bug") == 0) { 1292 // If this property is set on the command line then its value will be 1293 // displayed in VM error logs as the URL at which to submit such logs. 1294 // Normally the URL displayed in error logs is different from the value 1295 // of this system property, so a different property should have been 1296 // used here, but we leave this as-is in case someone depends upon it. 1297 const char* old_java_vendor_url_bug = _java_vendor_url_bug; 1298 // save it in _java_vendor_url_bug, so JVM fatal error handler can access 1299 // its value without going through the property list or making a Java call. 1300 _java_vendor_url_bug = os::strdup_check_oom(value, mtArguments); 1301 if (old_java_vendor_url_bug != nullptr) { 1302 os::free((void *)old_java_vendor_url_bug); 1303 } 1304 } 1305 1306 // Create new property and add at the end of the list 1307 PropertyList_unique_add(&_system_properties, key, value, AddProperty, writeable, internal); 1308 } 1309 1310 if (key != prop) { 1311 // SystemProperty copy passed value, thus free previously allocated 1312 // memory 1313 FreeHeap((void *)key); 1314 } 1315 1316 return true; 1317 } 1318 1319 #if INCLUDE_CDS 1320 const char* unsupported_properties[] = { "jdk.module.limitmods", 1321 "jdk.module.upgrade.path", 1322 "jdk.module.patch.0" }; 1323 const char* unsupported_options[] = { "--limit-modules", 1324 "--upgrade-module-path", 1325 "--patch-module" 1326 }; 1327 void Arguments::check_unsupported_dumping_properties() { 1328 assert(is_dumping_archive(), 1329 "this function is only used with CDS dump time"); 1330 assert(ARRAY_SIZE(unsupported_properties) == ARRAY_SIZE(unsupported_options), "must be"); 1331 // If a vm option is found in the unsupported_options array, vm will exit with an error message. 1332 SystemProperty* sp = system_properties(); 1333 while (sp != nullptr) { 1334 for (uint i = 0; i < ARRAY_SIZE(unsupported_properties); i++) { 1335 if (strcmp(sp->key(), unsupported_properties[i]) == 0) { 1336 vm_exit_during_initialization( 1337 "Cannot use the following option when dumping the shared archive", unsupported_options[i]); 1338 } 1339 } 1340 sp = sp->next(); 1341 } 1342 1343 // Check for an exploded module build in use with -Xshare:dump. 1344 if (!has_jimage()) { 1345 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build"); 1346 } 1347 } 1348 1349 bool Arguments::check_unsupported_cds_runtime_properties() { 1350 assert(UseSharedSpaces, "this function is only used with -Xshare:{on,auto}"); 1351 assert(ARRAY_SIZE(unsupported_properties) == ARRAY_SIZE(unsupported_options), "must be"); 1352 if (ArchiveClassesAtExit != nullptr) { 1353 // dynamic dumping, just return false for now. 1354 // check_unsupported_dumping_properties() will be called later to check the same set of 1355 // properties, and will exit the VM with the correct error message if the unsupported properties 1356 // are used. 1357 return false; 1358 } 1359 for (uint i = 0; i < ARRAY_SIZE(unsupported_properties); i++) { 1360 if (get_property(unsupported_properties[i]) != nullptr) { 1361 if (RequireSharedSpaces) { 1362 warning("CDS is disabled when the %s option is specified.", unsupported_options[i]); 1363 } else { 1364 log_info(cds)("CDS is disabled when the %s option is specified.", unsupported_options[i]); 1365 } 1366 return true; 1367 } 1368 } 1369 return false; 1370 } 1371 #endif 1372 1373 //=========================================================================================================== 1374 // Setting int/mixed/comp mode flags 1375 1376 void Arguments::set_mode_flags(Mode mode) { 1377 // Set up default values for all flags. 1378 // If you add a flag to any of the branches below, 1379 // add a default value for it here. 1380 _mode = mode; 1381 1382 // Ensure Agent_OnLoad has the correct initial values. 1383 // This may not be the final mode; mode may change later in onload phase. 1384 PropertyList_unique_add(&_system_properties, "java.vm.info", 1385 VM_Version::vm_info_string(), AddProperty, UnwriteableProperty, ExternalProperty); 1386 1387 UseInterpreter = true; 1388 UseCompiler = true; 1389 UseLoopCounter = true; 1390 1391 // Default values may be platform/compiler dependent - 1392 // use the saved values 1393 ClipInlining = Arguments::_ClipInlining; 1394 AlwaysCompileLoopMethods = Arguments::_AlwaysCompileLoopMethods; 1395 UseOnStackReplacement = Arguments::_UseOnStackReplacement; 1396 BackgroundCompilation = Arguments::_BackgroundCompilation; 1397 1398 // Change from defaults based on mode 1399 switch (mode) { 1400 default: 1401 ShouldNotReachHere(); 1402 break; 1403 case _int: 1404 UseCompiler = false; 1405 UseLoopCounter = false; 1406 AlwaysCompileLoopMethods = false; 1407 UseOnStackReplacement = false; 1408 break; 1409 case _mixed: 1410 // same as default 1411 break; 1412 case _comp: 1413 UseInterpreter = false; 1414 BackgroundCompilation = false; 1415 ClipInlining = false; 1416 break; 1417 } 1418 } 1419 1420 // Conflict: required to use shared spaces (-Xshare:on), but 1421 // incompatible command line options were chosen. 1422 static void no_shared_spaces(const char* message) { 1423 if (RequireSharedSpaces) { 1424 jio_fprintf(defaultStream::error_stream(), 1425 "Class data sharing is inconsistent with other specified options.\n"); 1426 vm_exit_during_initialization("Unable to use shared archive", message); 1427 } else { 1428 log_info(cds)("Unable to use shared archive: %s", message); 1429 UseSharedSpaces = false; 1430 } 1431 } 1432 1433 void set_object_alignment() { 1434 // Object alignment. 1435 assert(is_power_of_2(ObjectAlignmentInBytes), "ObjectAlignmentInBytes must be power of 2"); 1436 MinObjAlignmentInBytes = ObjectAlignmentInBytes; 1437 assert(MinObjAlignmentInBytes >= HeapWordsPerLong * HeapWordSize, "ObjectAlignmentInBytes value is too small"); 1438 MinObjAlignment = MinObjAlignmentInBytes / HeapWordSize; 1439 assert(MinObjAlignmentInBytes == MinObjAlignment * HeapWordSize, "ObjectAlignmentInBytes value is incorrect"); 1440 MinObjAlignmentInBytesMask = MinObjAlignmentInBytes - 1; 1441 1442 LogMinObjAlignmentInBytes = exact_log2(ObjectAlignmentInBytes); 1443 LogMinObjAlignment = LogMinObjAlignmentInBytes - LogHeapWordSize; 1444 1445 // Oop encoding heap max 1446 OopEncodingHeapMax = (uint64_t(max_juint) + 1) << LogMinObjAlignmentInBytes; 1447 } 1448 1449 size_t Arguments::max_heap_for_compressed_oops() { 1450 // Avoid sign flip. 1451 assert(OopEncodingHeapMax > (uint64_t)os::vm_page_size(), "Unusual page size"); 1452 // We need to fit both the null page and the heap into the memory budget, while 1453 // keeping alignment constraints of the heap. To guarantee the latter, as the 1454 // null page is located before the heap, we pad the null page to the conservative 1455 // maximum alignment that the GC may ever impose upon the heap. 1456 size_t displacement_due_to_null_page = align_up(os::vm_page_size(), 1457 _conservative_max_heap_alignment); 1458 1459 LP64_ONLY(return OopEncodingHeapMax - displacement_due_to_null_page); 1460 NOT_LP64(ShouldNotReachHere(); return 0); 1461 } 1462 1463 void Arguments::set_use_compressed_oops() { 1464 #ifdef _LP64 1465 // MaxHeapSize is not set up properly at this point, but 1466 // the only value that can override MaxHeapSize if we are 1467 // to use UseCompressedOops are InitialHeapSize and MinHeapSize. 1468 size_t max_heap_size = MAX3(MaxHeapSize, InitialHeapSize, MinHeapSize); 1469 1470 if (max_heap_size <= max_heap_for_compressed_oops()) { 1471 if (FLAG_IS_DEFAULT(UseCompressedOops)) { 1472 FLAG_SET_ERGO(UseCompressedOops, true); 1473 } 1474 } else { 1475 if (UseCompressedOops && !FLAG_IS_DEFAULT(UseCompressedOops)) { 1476 warning("Max heap size too large for Compressed Oops"); 1477 FLAG_SET_DEFAULT(UseCompressedOops, false); 1478 } 1479 } 1480 #endif // _LP64 1481 } 1482 1483 void Arguments::set_use_compressed_klass_ptrs() { 1484 #ifdef _LP64 1485 assert(!UseCompressedClassPointers || CompressedClassSpaceSize <= KlassEncodingMetaspaceMax, 1486 "CompressedClassSpaceSize is too large for UseCompressedClassPointers"); 1487 #endif // _LP64 1488 } 1489 1490 void Arguments::set_conservative_max_heap_alignment() { 1491 // The conservative maximum required alignment for the heap is the maximum of 1492 // the alignments imposed by several sources: any requirements from the heap 1493 // itself and the maximum page size we may run the VM with. 1494 size_t heap_alignment = GCConfig::arguments()->conservative_max_heap_alignment(); 1495 _conservative_max_heap_alignment = MAX4(heap_alignment, 1496 os::vm_allocation_granularity(), 1497 os::max_page_size(), 1498 GCArguments::compute_heap_alignment()); 1499 } 1500 1501 jint Arguments::set_ergonomics_flags() { 1502 GCConfig::initialize(); 1503 1504 set_conservative_max_heap_alignment(); 1505 1506 #ifdef _LP64 1507 set_use_compressed_oops(); 1508 set_use_compressed_klass_ptrs(); 1509 1510 // Also checks that certain machines are slower with compressed oops 1511 // in vm_version initialization code. 1512 #endif // _LP64 1513 1514 return JNI_OK; 1515 } 1516 1517 size_t Arguments::limit_heap_by_allocatable_memory(size_t limit) { 1518 size_t max_allocatable; 1519 size_t result = limit; 1520 if (os::has_allocatable_memory_limit(&max_allocatable)) { 1521 // The AggressiveHeap check is a temporary workaround to avoid calling 1522 // GCarguments::heap_virtual_to_physical_ratio() before a GC has been 1523 // selected. This works because AggressiveHeap implies UseParallelGC 1524 // where we know the ratio will be 1. Once the AggressiveHeap option is 1525 // removed, this can be cleaned up. 1526 size_t heap_virtual_to_physical_ratio = (AggressiveHeap ? 1 : GCConfig::arguments()->heap_virtual_to_physical_ratio()); 1527 size_t fraction = MaxVirtMemFraction * heap_virtual_to_physical_ratio; 1528 result = MIN2(result, max_allocatable / fraction); 1529 } 1530 return result; 1531 } 1532 1533 // Use static initialization to get the default before parsing 1534 static const size_t DefaultHeapBaseMinAddress = HeapBaseMinAddress; 1535 1536 void Arguments::set_heap_size() { 1537 julong phys_mem; 1538 1539 // If the user specified one of these options, they 1540 // want specific memory sizing so do not limit memory 1541 // based on compressed oops addressability. 1542 // Also, memory limits will be calculated based on 1543 // available os physical memory, not our MaxRAM limit, 1544 // unless MaxRAM is also specified. 1545 bool override_coop_limit = (!FLAG_IS_DEFAULT(MaxRAMPercentage) || 1546 !FLAG_IS_DEFAULT(MaxRAMFraction) || 1547 !FLAG_IS_DEFAULT(MinRAMPercentage) || 1548 !FLAG_IS_DEFAULT(MinRAMFraction) || 1549 !FLAG_IS_DEFAULT(InitialRAMPercentage) || 1550 !FLAG_IS_DEFAULT(InitialRAMFraction) || 1551 !FLAG_IS_DEFAULT(MaxRAM)); 1552 if (override_coop_limit) { 1553 if (FLAG_IS_DEFAULT(MaxRAM)) { 1554 phys_mem = os::physical_memory(); 1555 FLAG_SET_ERGO(MaxRAM, (uint64_t)phys_mem); 1556 } else { 1557 phys_mem = (julong)MaxRAM; 1558 } 1559 } else { 1560 phys_mem = FLAG_IS_DEFAULT(MaxRAM) ? MIN2(os::physical_memory(), (julong)MaxRAM) 1561 : (julong)MaxRAM; 1562 } 1563 1564 1565 // Convert deprecated flags 1566 if (FLAG_IS_DEFAULT(MaxRAMPercentage) && 1567 !FLAG_IS_DEFAULT(MaxRAMFraction)) 1568 MaxRAMPercentage = 100.0 / (double)MaxRAMFraction; 1569 1570 if (FLAG_IS_DEFAULT(MinRAMPercentage) && 1571 !FLAG_IS_DEFAULT(MinRAMFraction)) 1572 MinRAMPercentage = 100.0 / (double)MinRAMFraction; 1573 1574 if (FLAG_IS_DEFAULT(InitialRAMPercentage) && 1575 !FLAG_IS_DEFAULT(InitialRAMFraction)) 1576 InitialRAMPercentage = 100.0 / (double)InitialRAMFraction; 1577 1578 // If the maximum heap size has not been set with -Xmx, 1579 // then set it as fraction of the size of physical memory, 1580 // respecting the maximum and minimum sizes of the heap. 1581 if (FLAG_IS_DEFAULT(MaxHeapSize)) { 1582 julong reasonable_max = (julong)(((double)phys_mem * MaxRAMPercentage) / 100); 1583 const julong reasonable_min = (julong)(((double)phys_mem * MinRAMPercentage) / 100); 1584 if (reasonable_min < MaxHeapSize) { 1585 // Small physical memory, so use a minimum fraction of it for the heap 1586 reasonable_max = reasonable_min; 1587 } else { 1588 // Not-small physical memory, so require a heap at least 1589 // as large as MaxHeapSize 1590 reasonable_max = MAX2(reasonable_max, (julong)MaxHeapSize); 1591 } 1592 1593 if (!FLAG_IS_DEFAULT(ErgoHeapSizeLimit) && ErgoHeapSizeLimit != 0) { 1594 // Limit the heap size to ErgoHeapSizeLimit 1595 reasonable_max = MIN2(reasonable_max, (julong)ErgoHeapSizeLimit); 1596 } 1597 1598 reasonable_max = limit_heap_by_allocatable_memory(reasonable_max); 1599 1600 if (!FLAG_IS_DEFAULT(InitialHeapSize)) { 1601 // An initial heap size was specified on the command line, 1602 // so be sure that the maximum size is consistent. Done 1603 // after call to limit_heap_by_allocatable_memory because that 1604 // method might reduce the allocation size. 1605 reasonable_max = MAX2(reasonable_max, (julong)InitialHeapSize); 1606 } else if (!FLAG_IS_DEFAULT(MinHeapSize)) { 1607 reasonable_max = MAX2(reasonable_max, (julong)MinHeapSize); 1608 } 1609 1610 #ifdef _LP64 1611 if (UseCompressedOops || UseCompressedClassPointers) { 1612 // HeapBaseMinAddress can be greater than default but not less than. 1613 if (!FLAG_IS_DEFAULT(HeapBaseMinAddress)) { 1614 if (HeapBaseMinAddress < DefaultHeapBaseMinAddress) { 1615 // matches compressed oops printing flags 1616 log_debug(gc, heap, coops)("HeapBaseMinAddress must be at least " SIZE_FORMAT 1617 " (" SIZE_FORMAT "G) which is greater than value given " SIZE_FORMAT, 1618 DefaultHeapBaseMinAddress, 1619 DefaultHeapBaseMinAddress/G, 1620 HeapBaseMinAddress); 1621 FLAG_SET_ERGO(HeapBaseMinAddress, DefaultHeapBaseMinAddress); 1622 } 1623 } 1624 } 1625 if (UseCompressedOops) { 1626 // Limit the heap size to the maximum possible when using compressed oops 1627 julong max_coop_heap = (julong)max_heap_for_compressed_oops(); 1628 1629 if (HeapBaseMinAddress + MaxHeapSize < max_coop_heap) { 1630 // Heap should be above HeapBaseMinAddress to get zero based compressed oops 1631 // but it should be not less than default MaxHeapSize. 1632 max_coop_heap -= HeapBaseMinAddress; 1633 } 1634 1635 // If user specified flags prioritizing os physical 1636 // memory limits, then disable compressed oops if 1637 // limits exceed max_coop_heap and UseCompressedOops 1638 // was not specified. 1639 if (reasonable_max > max_coop_heap) { 1640 if (FLAG_IS_ERGO(UseCompressedOops) && override_coop_limit) { 1641 log_info(cds)("UseCompressedOops and UseCompressedClassPointers have been disabled due to" 1642 " max heap " SIZE_FORMAT " > compressed oop heap " SIZE_FORMAT ". " 1643 "Please check the setting of MaxRAMPercentage %5.2f." 1644 ,(size_t)reasonable_max, (size_t)max_coop_heap, MaxRAMPercentage); 1645 FLAG_SET_ERGO(UseCompressedOops, false); 1646 } else { 1647 reasonable_max = MIN2(reasonable_max, max_coop_heap); 1648 } 1649 } 1650 } 1651 #endif // _LP64 1652 1653 log_trace(gc, heap)(" Maximum heap size " SIZE_FORMAT, (size_t) reasonable_max); 1654 FLAG_SET_ERGO(MaxHeapSize, (size_t)reasonable_max); 1655 } 1656 1657 // If the minimum or initial heap_size have not been set or requested to be set 1658 // ergonomically, set them accordingly. 1659 if (InitialHeapSize == 0 || MinHeapSize == 0) { 1660 julong reasonable_minimum = (julong)(OldSize + NewSize); 1661 1662 reasonable_minimum = MIN2(reasonable_minimum, (julong)MaxHeapSize); 1663 1664 reasonable_minimum = limit_heap_by_allocatable_memory(reasonable_minimum); 1665 1666 if (InitialHeapSize == 0) { 1667 julong reasonable_initial = (julong)(((double)phys_mem * InitialRAMPercentage) / 100); 1668 reasonable_initial = limit_heap_by_allocatable_memory(reasonable_initial); 1669 1670 reasonable_initial = MAX3(reasonable_initial, reasonable_minimum, (julong)MinHeapSize); 1671 reasonable_initial = MIN2(reasonable_initial, (julong)MaxHeapSize); 1672 1673 FLAG_SET_ERGO(InitialHeapSize, (size_t)reasonable_initial); 1674 log_trace(gc, heap)(" Initial heap size " SIZE_FORMAT, InitialHeapSize); 1675 } 1676 // If the minimum heap size has not been set (via -Xms or -XX:MinHeapSize), 1677 // synchronize with InitialHeapSize to avoid errors with the default value. 1678 if (MinHeapSize == 0) { 1679 FLAG_SET_ERGO(MinHeapSize, MIN2((size_t)reasonable_minimum, InitialHeapSize)); 1680 log_trace(gc, heap)(" Minimum heap size " SIZE_FORMAT, MinHeapSize); 1681 } 1682 } 1683 } 1684 1685 // This option inspects the machine and attempts to set various 1686 // parameters to be optimal for long-running, memory allocation 1687 // intensive jobs. It is intended for machines with large 1688 // amounts of cpu and memory. 1689 jint Arguments::set_aggressive_heap_flags() { 1690 // initHeapSize is needed since _initial_heap_size is 4 bytes on a 32 bit 1691 // VM, but we may not be able to represent the total physical memory 1692 // available (like having 8gb of memory on a box but using a 32bit VM). 1693 // Thus, we need to make sure we're using a julong for intermediate 1694 // calculations. 1695 julong initHeapSize; 1696 julong total_memory = os::physical_memory(); 1697 1698 if (total_memory < (julong) 256 * M) { 1699 jio_fprintf(defaultStream::error_stream(), 1700 "You need at least 256mb of memory to use -XX:+AggressiveHeap\n"); 1701 vm_exit(1); 1702 } 1703 1704 // The heap size is half of available memory, or (at most) 1705 // all of possible memory less 160mb (leaving room for the OS 1706 // when using ISM). This is the maximum; because adaptive sizing 1707 // is turned on below, the actual space used may be smaller. 1708 1709 initHeapSize = MIN2(total_memory / (julong) 2, 1710 total_memory - (julong) 160 * M); 1711 1712 initHeapSize = limit_heap_by_allocatable_memory(initHeapSize); 1713 1714 if (FLAG_IS_DEFAULT(MaxHeapSize)) { 1715 if (FLAG_SET_CMDLINE(MaxHeapSize, initHeapSize) != JVMFlag::SUCCESS) { 1716 return JNI_EINVAL; 1717 } 1718 if (FLAG_SET_CMDLINE(InitialHeapSize, initHeapSize) != JVMFlag::SUCCESS) { 1719 return JNI_EINVAL; 1720 } 1721 if (FLAG_SET_CMDLINE(MinHeapSize, initHeapSize) != JVMFlag::SUCCESS) { 1722 return JNI_EINVAL; 1723 } 1724 } 1725 if (FLAG_IS_DEFAULT(NewSize)) { 1726 // Make the young generation 3/8ths of the total heap. 1727 if (FLAG_SET_CMDLINE(NewSize, 1728 ((julong) MaxHeapSize / (julong) 8) * (julong) 3) != JVMFlag::SUCCESS) { 1729 return JNI_EINVAL; 1730 } 1731 if (FLAG_SET_CMDLINE(MaxNewSize, NewSize) != JVMFlag::SUCCESS) { 1732 return JNI_EINVAL; 1733 } 1734 } 1735 1736 #if !defined(_ALLBSD_SOURCE) && !defined(AIX) // UseLargePages is not yet supported on BSD and AIX. 1737 FLAG_SET_DEFAULT(UseLargePages, true); 1738 #endif 1739 1740 // Increase some data structure sizes for efficiency 1741 if (FLAG_SET_CMDLINE(BaseFootPrintEstimate, MaxHeapSize) != JVMFlag::SUCCESS) { 1742 return JNI_EINVAL; 1743 } 1744 if (FLAG_SET_CMDLINE(ResizeTLAB, false) != JVMFlag::SUCCESS) { 1745 return JNI_EINVAL; 1746 } 1747 if (FLAG_SET_CMDLINE(TLABSize, 256 * K) != JVMFlag::SUCCESS) { 1748 return JNI_EINVAL; 1749 } 1750 1751 // See the OldPLABSize comment below, but replace 'after promotion' 1752 // with 'after copying'. YoungPLABSize is the size of the survivor 1753 // space per-gc-thread buffers. The default is 4kw. 1754 if (FLAG_SET_CMDLINE(YoungPLABSize, 256 * K) != JVMFlag::SUCCESS) { // Note: this is in words 1755 return JNI_EINVAL; 1756 } 1757 1758 // OldPLABSize is the size of the buffers in the old gen that 1759 // UseParallelGC uses to promote live data that doesn't fit in the 1760 // survivor spaces. At any given time, there's one for each gc thread. 1761 // The default size is 1kw. These buffers are rarely used, since the 1762 // survivor spaces are usually big enough. For specjbb, however, there 1763 // are occasions when there's lots of live data in the young gen 1764 // and we end up promoting some of it. We don't have a definite 1765 // explanation for why bumping OldPLABSize helps, but the theory 1766 // is that a bigger PLAB results in retaining something like the 1767 // original allocation order after promotion, which improves mutator 1768 // locality. A minor effect may be that larger PLABs reduce the 1769 // number of PLAB allocation events during gc. The value of 8kw 1770 // was arrived at by experimenting with specjbb. 1771 if (FLAG_SET_CMDLINE(OldPLABSize, 8 * K) != JVMFlag::SUCCESS) { // Note: this is in words 1772 return JNI_EINVAL; 1773 } 1774 1775 // Enable parallel GC and adaptive generation sizing 1776 if (FLAG_SET_CMDLINE(UseParallelGC, true) != JVMFlag::SUCCESS) { 1777 return JNI_EINVAL; 1778 } 1779 1780 // Encourage steady state memory management 1781 if (FLAG_SET_CMDLINE(ThresholdTolerance, 100) != JVMFlag::SUCCESS) { 1782 return JNI_EINVAL; 1783 } 1784 1785 // This appears to improve mutator locality 1786 if (FLAG_SET_CMDLINE(ScavengeBeforeFullGC, false) != JVMFlag::SUCCESS) { 1787 return JNI_EINVAL; 1788 } 1789 1790 return JNI_OK; 1791 } 1792 1793 // This must be called after ergonomics. 1794 void Arguments::set_bytecode_flags() { 1795 if (!RewriteBytecodes) { 1796 FLAG_SET_DEFAULT(RewriteFrequentPairs, false); 1797 } 1798 } 1799 1800 // Aggressive optimization flags 1801 jint Arguments::set_aggressive_opts_flags() { 1802 #ifdef COMPILER2 1803 if (AggressiveUnboxing) { 1804 if (FLAG_IS_DEFAULT(EliminateAutoBox)) { 1805 FLAG_SET_DEFAULT(EliminateAutoBox, true); 1806 } else if (!EliminateAutoBox) { 1807 // warning("AggressiveUnboxing is disabled because EliminateAutoBox is disabled"); 1808 AggressiveUnboxing = false; 1809 } 1810 if (FLAG_IS_DEFAULT(DoEscapeAnalysis)) { 1811 FLAG_SET_DEFAULT(DoEscapeAnalysis, true); 1812 } else if (!DoEscapeAnalysis) { 1813 // warning("AggressiveUnboxing is disabled because DoEscapeAnalysis is disabled"); 1814 AggressiveUnboxing = false; 1815 } 1816 } 1817 if (!FLAG_IS_DEFAULT(AutoBoxCacheMax)) { 1818 if (FLAG_IS_DEFAULT(EliminateAutoBox)) { 1819 FLAG_SET_DEFAULT(EliminateAutoBox, true); 1820 } 1821 // Feed the cache size setting into the JDK 1822 char buffer[1024]; 1823 jio_snprintf(buffer, 1024, "java.lang.Integer.IntegerCache.high=" INTX_FORMAT, AutoBoxCacheMax); 1824 if (!add_property(buffer)) { 1825 return JNI_ENOMEM; 1826 } 1827 } 1828 #endif 1829 1830 return JNI_OK; 1831 } 1832 1833 //=========================================================================================================== 1834 1835 void Arguments::process_java_launcher_argument(const char* launcher, void* extra_info) { 1836 if (_sun_java_launcher != _default_java_launcher) { 1837 os::free(const_cast<char*>(_sun_java_launcher)); 1838 } 1839 _sun_java_launcher = os::strdup_check_oom(launcher); 1840 } 1841 1842 bool Arguments::created_by_java_launcher() { 1843 assert(_sun_java_launcher != nullptr, "property must have value"); 1844 return strcmp(DEFAULT_JAVA_LAUNCHER, _sun_java_launcher) != 0; 1845 } 1846 1847 bool Arguments::sun_java_launcher_is_altjvm() { 1848 return _sun_java_launcher_is_altjvm; 1849 } 1850 1851 //=========================================================================================================== 1852 // Parsing of main arguments 1853 1854 unsigned int addreads_count = 0; 1855 unsigned int addexports_count = 0; 1856 unsigned int addopens_count = 0; 1857 unsigned int addmods_count = 0; 1858 unsigned int patch_mod_count = 0; 1859 unsigned int enable_native_access_count = 0; 1860 1861 // Check the consistency of vm_init_args 1862 bool Arguments::check_vm_args_consistency() { 1863 // Method for adding checks for flag consistency. 1864 // The intent is to warn the user of all possible conflicts, 1865 // before returning an error. 1866 // Note: Needs platform-dependent factoring. 1867 bool status = true; 1868 1869 if (TLABRefillWasteFraction == 0) { 1870 jio_fprintf(defaultStream::error_stream(), 1871 "TLABRefillWasteFraction should be a denominator, " 1872 "not " SIZE_FORMAT "\n", 1873 TLABRefillWasteFraction); 1874 status = false; 1875 } 1876 1877 status = CompilerConfig::check_args_consistency(status); 1878 #if INCLUDE_JVMCI 1879 if (status && EnableJVMCI) { 1880 PropertyList_unique_add(&_system_properties, "jdk.internal.vm.ci.enabled", "true", 1881 AddProperty, UnwriteableProperty, InternalProperty); 1882 if (ClassLoader::is_module_observable("jdk.internal.vm.ci")) { 1883 if (!create_numbered_module_property("jdk.module.addmods", "jdk.internal.vm.ci", addmods_count++)) { 1884 return false; 1885 } 1886 } 1887 } 1888 #endif 1889 1890 #if INCLUDE_JFR 1891 if (status && (FlightRecorderOptions || StartFlightRecording)) { 1892 if (!create_numbered_module_property("jdk.module.addmods", "jdk.jfr", addmods_count++)) { 1893 return false; 1894 } 1895 } 1896 #endif 1897 1898 #ifndef SUPPORT_RESERVED_STACK_AREA 1899 if (StackReservedPages != 0) { 1900 FLAG_SET_CMDLINE(StackReservedPages, 0); 1901 warning("Reserved Stack Area not supported on this platform"); 1902 } 1903 #endif 1904 1905 1906 #if !defined(X86) && !defined(AARCH64) && !defined(RISCV64) && !defined(ARM) && !defined(PPC64) 1907 if (LockingMode == LM_LIGHTWEIGHT) { 1908 FLAG_SET_CMDLINE(LockingMode, LM_LEGACY); 1909 warning("New lightweight locking not supported on this platform"); 1910 } 1911 #endif 1912 1913 if (UseHeavyMonitors) { 1914 if (FLAG_IS_CMDLINE(LockingMode) && LockingMode != LM_MONITOR) { 1915 jio_fprintf(defaultStream::error_stream(), 1916 "Conflicting -XX:+UseHeavyMonitors and -XX:LockingMode=%d flags\n", LockingMode); 1917 return false; 1918 } 1919 FLAG_SET_CMDLINE(LockingMode, LM_MONITOR); 1920 } 1921 1922 #if !defined(X86) && !defined(AARCH64) && !defined(PPC64) && !defined(RISCV64) && !defined(S390) 1923 if (LockingMode == LM_MONITOR) { 1924 jio_fprintf(defaultStream::error_stream(), 1925 "LockingMode == 0 (LM_MONITOR) is not fully implemented on this architecture\n"); 1926 return false; 1927 } 1928 #endif 1929 #if defined(X86) && !defined(ZERO) 1930 if (LockingMode == LM_MONITOR && UseRTMForStackLocks) { 1931 jio_fprintf(defaultStream::error_stream(), 1932 "LockingMode == 0 (LM_MONITOR) and -XX:+UseRTMForStackLocks are mutually exclusive\n"); 1933 1934 return false; 1935 } 1936 #endif 1937 if (VerifyHeavyMonitors && LockingMode != LM_MONITOR) { 1938 jio_fprintf(defaultStream::error_stream(), 1939 "-XX:+VerifyHeavyMonitors requires LockingMode == 0 (LM_MONITOR)\n"); 1940 return false; 1941 } 1942 return status; 1943 } 1944 1945 bool Arguments::is_bad_option(const JavaVMOption* option, jboolean ignore, 1946 const char* option_type) { 1947 if (ignore) return false; 1948 1949 const char* spacer = " "; 1950 if (option_type == nullptr) { 1951 option_type = ++spacer; // Set both to the empty string. 1952 } 1953 1954 jio_fprintf(defaultStream::error_stream(), 1955 "Unrecognized %s%soption: %s\n", option_type, spacer, 1956 option->optionString); 1957 return true; 1958 } 1959 1960 static const char* user_assertion_options[] = { 1961 "-da", "-ea", "-disableassertions", "-enableassertions", 0 1962 }; 1963 1964 static const char* system_assertion_options[] = { 1965 "-dsa", "-esa", "-disablesystemassertions", "-enablesystemassertions", 0 1966 }; 1967 1968 bool Arguments::parse_uint(const char* value, 1969 uint* uint_arg, 1970 uint min_size) { 1971 uint n; 1972 if (!parse_integer(value, &n)) { 1973 return false; 1974 } 1975 if (n >= min_size) { 1976 *uint_arg = n; 1977 return true; 1978 } else { 1979 return false; 1980 } 1981 } 1982 1983 bool Arguments::create_module_property(const char* prop_name, const char* prop_value, PropertyInternal internal) { 1984 assert(is_internal_module_property(prop_name), "unknown module property: '%s'", prop_name); 1985 size_t prop_len = strlen(prop_name) + strlen(prop_value) + 2; 1986 char* property = AllocateHeap(prop_len, mtArguments); 1987 int ret = jio_snprintf(property, prop_len, "%s=%s", prop_name, prop_value); 1988 if (ret < 0 || ret >= (int)prop_len) { 1989 FreeHeap(property); 1990 return false; 1991 } 1992 // These are not strictly writeable properties as they cannot be set via -Dprop=val. But that 1993 // is enforced by checking is_internal_module_property(). We need the property to be writeable so 1994 // that multiple occurrences of the associated flag just causes the existing property value to be 1995 // replaced ("last option wins"). Otherwise we would need to keep track of the flags and only convert 1996 // to a property after we have finished flag processing. 1997 bool added = add_property(property, WriteableProperty, internal); 1998 FreeHeap(property); 1999 return added; 2000 } 2001 2002 bool Arguments::create_numbered_module_property(const char* prop_base_name, const char* prop_value, unsigned int count) { 2003 assert(is_internal_module_property(prop_base_name), "unknown module property: '%s'", prop_base_name); 2004 const unsigned int props_count_limit = 1000; 2005 const int max_digits = 3; 2006 const int extra_symbols_count = 3; // includes '.', '=', '\0' 2007 2008 // Make sure count is < props_count_limit. Otherwise, memory allocation will be too small. 2009 if (count < props_count_limit) { 2010 size_t prop_len = strlen(prop_base_name) + strlen(prop_value) + max_digits + extra_symbols_count; 2011 char* property = AllocateHeap(prop_len, mtArguments); 2012 int ret = jio_snprintf(property, prop_len, "%s.%d=%s", prop_base_name, count, prop_value); 2013 if (ret < 0 || ret >= (int)prop_len) { 2014 FreeHeap(property); 2015 jio_fprintf(defaultStream::error_stream(), "Failed to create property %s.%d=%s\n", prop_base_name, count, prop_value); 2016 return false; 2017 } 2018 bool added = add_property(property, UnwriteableProperty, InternalProperty); 2019 FreeHeap(property); 2020 return added; 2021 } 2022 2023 jio_fprintf(defaultStream::error_stream(), "Property count limit exceeded: %s, limit=%d\n", prop_base_name, props_count_limit); 2024 return false; 2025 } 2026 2027 Arguments::ArgsRange Arguments::parse_memory_size(const char* s, 2028 julong* long_arg, 2029 julong min_size, 2030 julong max_size) { 2031 if (!parse_integer(s, long_arg)) return arg_unreadable; 2032 return check_memory_size(*long_arg, min_size, max_size); 2033 } 2034 2035 // Parse JavaVMInitArgs structure 2036 2037 jint Arguments::parse_vm_init_args(const JavaVMInitArgs *vm_options_args, 2038 const JavaVMInitArgs *java_tool_options_args, 2039 const JavaVMInitArgs *java_options_args, 2040 const JavaVMInitArgs *cmd_line_args) { 2041 bool patch_mod_javabase = false; 2042 2043 // Save default settings for some mode flags 2044 Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods; 2045 Arguments::_UseOnStackReplacement = UseOnStackReplacement; 2046 Arguments::_ClipInlining = ClipInlining; 2047 Arguments::_BackgroundCompilation = BackgroundCompilation; 2048 2049 // Remember the default value of SharedBaseAddress. 2050 Arguments::_default_SharedBaseAddress = SharedBaseAddress; 2051 2052 // Setup flags for mixed which is the default 2053 set_mode_flags(_mixed); 2054 2055 // Parse args structure generated from java.base vm options resource 2056 jint result = parse_each_vm_init_arg(vm_options_args, &patch_mod_javabase, JVMFlagOrigin::JIMAGE_RESOURCE); 2057 if (result != JNI_OK) { 2058 return result; 2059 } 2060 2061 // Parse args structure generated from JAVA_TOOL_OPTIONS environment 2062 // variable (if present). 2063 result = parse_each_vm_init_arg(java_tool_options_args, &patch_mod_javabase, JVMFlagOrigin::ENVIRON_VAR); 2064 if (result != JNI_OK) { 2065 return result; 2066 } 2067 2068 // Parse args structure generated from the command line flags. 2069 result = parse_each_vm_init_arg(cmd_line_args, &patch_mod_javabase, JVMFlagOrigin::COMMAND_LINE); 2070 if (result != JNI_OK) { 2071 return result; 2072 } 2073 2074 // Parse args structure generated from the _JAVA_OPTIONS environment 2075 // variable (if present) (mimics classic VM) 2076 result = parse_each_vm_init_arg(java_options_args, &patch_mod_javabase, JVMFlagOrigin::ENVIRON_VAR); 2077 if (result != JNI_OK) { 2078 return result; 2079 } 2080 2081 // Disable CDS for exploded image 2082 if (!has_jimage()) { 2083 no_shared_spaces("CDS disabled on exploded JDK"); 2084 } 2085 2086 // We need to ensure processor and memory resources have been properly 2087 // configured - which may rely on arguments we just processed - before 2088 // doing the final argument processing. Any argument processing that 2089 // needs to know about processor and memory resources must occur after 2090 // this point. 2091 2092 os::init_container_support(); 2093 2094 SystemMemoryBarrier::initialize(); 2095 2096 // Do final processing now that all arguments have been parsed 2097 result = finalize_vm_init_args(patch_mod_javabase); 2098 if (result != JNI_OK) { 2099 return result; 2100 } 2101 2102 return JNI_OK; 2103 } 2104 2105 // Checks if name in command-line argument -agent{lib,path}:name[=options] 2106 // represents a valid JDWP agent. is_path==true denotes that we 2107 // are dealing with -agentpath (case where name is a path), otherwise with 2108 // -agentlib 2109 bool valid_jdwp_agent(char *name, bool is_path) { 2110 char *_name; 2111 const char *_jdwp = "jdwp"; 2112 size_t _len_jdwp, _len_prefix; 2113 2114 if (is_path) { 2115 if ((_name = strrchr(name, (int) *os::file_separator())) == nullptr) { 2116 return false; 2117 } 2118 2119 _name++; // skip past last path separator 2120 _len_prefix = strlen(JNI_LIB_PREFIX); 2121 2122 if (strncmp(_name, JNI_LIB_PREFIX, _len_prefix) != 0) { 2123 return false; 2124 } 2125 2126 _name += _len_prefix; 2127 _len_jdwp = strlen(_jdwp); 2128 2129 if (strncmp(_name, _jdwp, _len_jdwp) == 0) { 2130 _name += _len_jdwp; 2131 } 2132 else { 2133 return false; 2134 } 2135 2136 if (strcmp(_name, JNI_LIB_SUFFIX) != 0) { 2137 return false; 2138 } 2139 2140 return true; 2141 } 2142 2143 if (strcmp(name, _jdwp) == 0) { 2144 return true; 2145 } 2146 2147 return false; 2148 } 2149 2150 int Arguments::process_patch_mod_option(const char* patch_mod_tail, bool* patch_mod_javabase) { 2151 // --patch-module=<module>=<file>(<pathsep><file>)* 2152 assert(patch_mod_tail != nullptr, "Unexpected null patch-module value"); 2153 // Find the equal sign between the module name and the path specification 2154 const char* module_equal = strchr(patch_mod_tail, '='); 2155 if (module_equal == nullptr) { 2156 jio_fprintf(defaultStream::output_stream(), "Missing '=' in --patch-module specification\n"); 2157 return JNI_ERR; 2158 } else { 2159 // Pick out the module name 2160 size_t module_len = module_equal - patch_mod_tail; 2161 char* module_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, module_len+1, mtArguments); 2162 if (module_name != nullptr) { 2163 memcpy(module_name, patch_mod_tail, module_len); 2164 *(module_name + module_len) = '\0'; 2165 // The path piece begins one past the module_equal sign 2166 add_patch_mod_prefix(module_name, module_equal + 1, patch_mod_javabase); 2167 FREE_C_HEAP_ARRAY(char, module_name); 2168 if (!create_numbered_module_property("jdk.module.patch", patch_mod_tail, patch_mod_count++)) { 2169 return JNI_ENOMEM; 2170 } 2171 } else { 2172 return JNI_ENOMEM; 2173 } 2174 } 2175 return JNI_OK; 2176 } 2177 2178 // Parse -Xss memory string parameter and convert to ThreadStackSize in K. 2179 jint Arguments::parse_xss(const JavaVMOption* option, const char* tail, intx* out_ThreadStackSize) { 2180 // The min and max sizes match the values in globals.hpp, but scaled 2181 // with K. The values have been chosen so that alignment with page 2182 // size doesn't change the max value, which makes the conversions 2183 // back and forth between Xss value and ThreadStackSize value easier. 2184 // The values have also been chosen to fit inside a 32-bit signed type. 2185 const julong min_ThreadStackSize = 0; 2186 const julong max_ThreadStackSize = 1 * M; 2187 2188 // Make sure the above values match the range set in globals.hpp 2189 const JVMTypedFlagLimit<intx>* limit = JVMFlagLimit::get_range_at(FLAG_MEMBER_ENUM(ThreadStackSize))->cast<intx>(); 2190 assert(min_ThreadStackSize == static_cast<julong>(limit->min()), "must be"); 2191 assert(max_ThreadStackSize == static_cast<julong>(limit->max()), "must be"); 2192 2193 const julong min_size = min_ThreadStackSize * K; 2194 const julong max_size = max_ThreadStackSize * K; 2195 2196 assert(is_aligned(max_size, os::vm_page_size()), "Implementation assumption"); 2197 2198 julong size = 0; 2199 ArgsRange errcode = parse_memory_size(tail, &size, min_size, max_size); 2200 if (errcode != arg_in_range) { 2201 bool silent = (option == nullptr); // Allow testing to silence error messages 2202 if (!silent) { 2203 jio_fprintf(defaultStream::error_stream(), 2204 "Invalid thread stack size: %s\n", option->optionString); 2205 describe_range_error(errcode); 2206 } 2207 return JNI_EINVAL; 2208 } 2209 2210 // Internally track ThreadStackSize in units of 1024 bytes. 2211 const julong size_aligned = align_up(size, K); 2212 assert(size <= size_aligned, 2213 "Overflow: " JULONG_FORMAT " " JULONG_FORMAT, 2214 size, size_aligned); 2215 2216 const julong size_in_K = size_aligned / K; 2217 assert(size_in_K < (julong)max_intx, 2218 "size_in_K doesn't fit in the type of ThreadStackSize: " JULONG_FORMAT, 2219 size_in_K); 2220 2221 // Check that code expanding ThreadStackSize to a page aligned number of bytes won't overflow. 2222 const julong max_expanded = align_up(size_in_K * K, os::vm_page_size()); 2223 assert(max_expanded < max_uintx && max_expanded >= size_in_K, 2224 "Expansion overflowed: " JULONG_FORMAT " " JULONG_FORMAT, 2225 max_expanded, size_in_K); 2226 2227 *out_ThreadStackSize = (intx)size_in_K; 2228 2229 return JNI_OK; 2230 } 2231 2232 jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_mod_javabase, JVMFlagOrigin origin) { 2233 // For match_option to return remaining or value part of option string 2234 const char* tail; 2235 2236 // iterate over arguments 2237 for (int index = 0; index < args->nOptions; index++) { 2238 bool is_absolute_path = false; // for -agentpath vs -agentlib 2239 2240 const JavaVMOption* option = args->options + index; 2241 2242 if (!match_option(option, "-Djava.class.path", &tail) && 2243 !match_option(option, "-Dsun.java.command", &tail) && 2244 !match_option(option, "-Dsun.java.launcher", &tail)) { 2245 2246 // add all jvm options to the jvm_args string. This string 2247 // is used later to set the java.vm.args PerfData string constant. 2248 // the -Djava.class.path and the -Dsun.java.command options are 2249 // omitted from jvm_args string as each have their own PerfData 2250 // string constant object. 2251 build_jvm_args(option->optionString); 2252 } 2253 2254 // -verbose:[class/module/gc/jni] 2255 if (match_option(option, "-verbose", &tail)) { 2256 if (!strcmp(tail, ":class") || !strcmp(tail, "")) { 2257 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, load)); 2258 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, unload)); 2259 } else if (!strcmp(tail, ":module")) { 2260 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(module, load)); 2261 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(module, unload)); 2262 } else if (!strcmp(tail, ":gc")) { 2263 if (_legacyGCLogging.lastFlag == 0) { 2264 _legacyGCLogging.lastFlag = 1; 2265 } 2266 } else if (!strcmp(tail, ":jni")) { 2267 LogConfiguration::configure_stdout(LogLevel::Debug, true, LOG_TAGS(jni, resolve)); 2268 } 2269 // -da / -ea / -disableassertions / -enableassertions 2270 // These accept an optional class/package name separated by a colon, e.g., 2271 // -da:java.lang.Thread. 2272 } else if (match_option(option, user_assertion_options, &tail, true)) { 2273 bool enable = option->optionString[1] == 'e'; // char after '-' is 'e' 2274 if (*tail == '\0') { 2275 JavaAssertions::setUserClassDefault(enable); 2276 } else { 2277 assert(*tail == ':', "bogus match by match_option()"); 2278 JavaAssertions::addOption(tail + 1, enable); 2279 } 2280 // -dsa / -esa / -disablesystemassertions / -enablesystemassertions 2281 } else if (match_option(option, system_assertion_options, &tail, false)) { 2282 bool enable = option->optionString[1] == 'e'; // char after '-' is 'e' 2283 JavaAssertions::setSystemClassDefault(enable); 2284 // -bootclasspath: 2285 } else if (match_option(option, "-Xbootclasspath:", &tail)) { 2286 jio_fprintf(defaultStream::output_stream(), 2287 "-Xbootclasspath is no longer a supported option.\n"); 2288 return JNI_EINVAL; 2289 // -bootclasspath/a: 2290 } else if (match_option(option, "-Xbootclasspath/a:", &tail)) { 2291 Arguments::append_sysclasspath(tail); 2292 #if INCLUDE_CDS 2293 MetaspaceShared::disable_optimized_module_handling(); 2294 log_info(cds)("optimized module handling: disabled because bootclasspath was appended"); 2295 #endif 2296 // -bootclasspath/p: 2297 } else if (match_option(option, "-Xbootclasspath/p:", &tail)) { 2298 jio_fprintf(defaultStream::output_stream(), 2299 "-Xbootclasspath/p is no longer a supported option.\n"); 2300 return JNI_EINVAL; 2301 // -Xrun 2302 } else if (match_option(option, "-Xrun", &tail)) { 2303 if (tail != nullptr) { 2304 const char* pos = strchr(tail, ':'); 2305 size_t len = (pos == nullptr) ? strlen(tail) : pos - tail; 2306 char* name = NEW_C_HEAP_ARRAY(char, len + 1, mtArguments); 2307 jio_snprintf(name, len + 1, "%s", tail); 2308 2309 char *options = nullptr; 2310 if(pos != nullptr) { 2311 size_t len2 = strlen(pos+1) + 1; // options start after ':'. Final zero must be copied. 2312 options = (char*)memcpy(NEW_C_HEAP_ARRAY(char, len2, mtArguments), pos+1, len2); 2313 } 2314 #if !INCLUDE_JVMTI 2315 if (strcmp(name, "jdwp") == 0) { 2316 jio_fprintf(defaultStream::error_stream(), 2317 "Debugging agents are not supported in this VM\n"); 2318 return JNI_ERR; 2319 } 2320 #endif // !INCLUDE_JVMTI 2321 JvmtiAgentList::add_xrun(name, options, false); 2322 FREE_C_HEAP_ARRAY(char, name); 2323 FREE_C_HEAP_ARRAY(char, options); 2324 } 2325 } else if (match_option(option, "--add-reads=", &tail)) { 2326 if (!create_numbered_module_property("jdk.module.addreads", tail, addreads_count++)) { 2327 return JNI_ENOMEM; 2328 } 2329 } else if (match_option(option, "--add-exports=", &tail)) { 2330 if (!create_numbered_module_property("jdk.module.addexports", tail, addexports_count++)) { 2331 return JNI_ENOMEM; 2332 } 2333 } else if (match_option(option, "--add-opens=", &tail)) { 2334 if (!create_numbered_module_property("jdk.module.addopens", tail, addopens_count++)) { 2335 return JNI_ENOMEM; 2336 } 2337 } else if (match_option(option, "--add-modules=", &tail)) { 2338 if (!create_numbered_module_property("jdk.module.addmods", tail, addmods_count++)) { 2339 return JNI_ENOMEM; 2340 } 2341 } else if (match_option(option, "--enable-native-access=", &tail)) { 2342 if (!create_numbered_module_property("jdk.module.enable.native.access", tail, enable_native_access_count++)) { 2343 return JNI_ENOMEM; 2344 } 2345 } else if (match_option(option, "--limit-modules=", &tail)) { 2346 if (!create_module_property("jdk.module.limitmods", tail, InternalProperty)) { 2347 return JNI_ENOMEM; 2348 } 2349 } else if (match_option(option, "--module-path=", &tail)) { 2350 if (!create_module_property("jdk.module.path", tail, ExternalProperty)) { 2351 return JNI_ENOMEM; 2352 } 2353 } else if (match_option(option, "--upgrade-module-path=", &tail)) { 2354 if (!create_module_property("jdk.module.upgrade.path", tail, ExternalProperty)) { 2355 return JNI_ENOMEM; 2356 } 2357 } else if (match_option(option, "--patch-module=", &tail)) { 2358 // --patch-module=<module>=<file>(<pathsep><file>)* 2359 int res = process_patch_mod_option(tail, patch_mod_javabase); 2360 if (res != JNI_OK) { 2361 return res; 2362 } 2363 } else if (match_option(option, "--illegal-access=", &tail)) { 2364 char version[256]; 2365 JDK_Version::jdk(17).to_string(version, sizeof(version)); 2366 warning("Ignoring option %s; support was removed in %s", option->optionString, version); 2367 // -agentlib and -agentpath 2368 } else if (match_option(option, "-agentlib:", &tail) || 2369 (is_absolute_path = match_option(option, "-agentpath:", &tail))) { 2370 if(tail != nullptr) { 2371 const char* pos = strchr(tail, '='); 2372 char* name; 2373 if (pos == nullptr) { 2374 name = os::strdup_check_oom(tail, mtArguments); 2375 } else { 2376 size_t len = pos - tail; 2377 name = NEW_C_HEAP_ARRAY(char, len + 1, mtArguments); 2378 memcpy(name, tail, len); 2379 name[len] = '\0'; 2380 } 2381 2382 char *options = nullptr; 2383 if(pos != nullptr) { 2384 options = os::strdup_check_oom(pos + 1, mtArguments); 2385 } 2386 #if !INCLUDE_JVMTI 2387 if (valid_jdwp_agent(name, is_absolute_path)) { 2388 jio_fprintf(defaultStream::error_stream(), 2389 "Debugging agents are not supported in this VM\n"); 2390 return JNI_ERR; 2391 } 2392 #endif // !INCLUDE_JVMTI 2393 JvmtiAgentList::add(name, options, is_absolute_path); 2394 os::free(name); 2395 os::free(options); 2396 } 2397 // -javaagent 2398 } else if (match_option(option, "-javaagent:", &tail)) { 2399 #if !INCLUDE_JVMTI 2400 jio_fprintf(defaultStream::error_stream(), 2401 "Instrumentation agents are not supported in this VM\n"); 2402 return JNI_ERR; 2403 #else 2404 if (tail != nullptr) { 2405 size_t length = strlen(tail) + 1; 2406 char *options = NEW_C_HEAP_ARRAY(char, length, mtArguments); 2407 jio_snprintf(options, length, "%s", tail); 2408 JvmtiAgentList::add("instrument", options, false); 2409 FREE_C_HEAP_ARRAY(char, options); 2410 2411 // java agents need module java.instrument 2412 if (!create_numbered_module_property("jdk.module.addmods", "java.instrument", addmods_count++)) { 2413 return JNI_ENOMEM; 2414 } 2415 } 2416 #endif // !INCLUDE_JVMTI 2417 // --enable_preview 2418 } else if (match_option(option, "--enable-preview")) { 2419 set_enable_preview(); 2420 // -Xnoclassgc 2421 } else if (match_option(option, "-Xnoclassgc")) { 2422 if (FLAG_SET_CMDLINE(ClassUnloading, false) != JVMFlag::SUCCESS) { 2423 return JNI_EINVAL; 2424 } 2425 // -Xbatch 2426 } else if (match_option(option, "-Xbatch")) { 2427 if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) { 2428 return JNI_EINVAL; 2429 } 2430 // -Xmn for compatibility with other JVM vendors 2431 } else if (match_option(option, "-Xmn", &tail)) { 2432 julong long_initial_young_size = 0; 2433 ArgsRange errcode = parse_memory_size(tail, &long_initial_young_size, 1); 2434 if (errcode != arg_in_range) { 2435 jio_fprintf(defaultStream::error_stream(), 2436 "Invalid initial young generation size: %s\n", option->optionString); 2437 describe_range_error(errcode); 2438 return JNI_EINVAL; 2439 } 2440 if (FLAG_SET_CMDLINE(MaxNewSize, (size_t)long_initial_young_size) != JVMFlag::SUCCESS) { 2441 return JNI_EINVAL; 2442 } 2443 if (FLAG_SET_CMDLINE(NewSize, (size_t)long_initial_young_size) != JVMFlag::SUCCESS) { 2444 return JNI_EINVAL; 2445 } 2446 // -Xms 2447 } else if (match_option(option, "-Xms", &tail)) { 2448 julong size = 0; 2449 // an initial heap size of 0 means automatically determine 2450 ArgsRange errcode = parse_memory_size(tail, &size, 0); 2451 if (errcode != arg_in_range) { 2452 jio_fprintf(defaultStream::error_stream(), 2453 "Invalid initial heap size: %s\n", option->optionString); 2454 describe_range_error(errcode); 2455 return JNI_EINVAL; 2456 } 2457 if (FLAG_SET_CMDLINE(MinHeapSize, (size_t)size) != JVMFlag::SUCCESS) { 2458 return JNI_EINVAL; 2459 } 2460 if (FLAG_SET_CMDLINE(InitialHeapSize, (size_t)size) != JVMFlag::SUCCESS) { 2461 return JNI_EINVAL; 2462 } 2463 // -Xmx 2464 } else if (match_option(option, "-Xmx", &tail) || match_option(option, "-XX:MaxHeapSize=", &tail)) { 2465 julong long_max_heap_size = 0; 2466 ArgsRange errcode = parse_memory_size(tail, &long_max_heap_size, 1); 2467 if (errcode != arg_in_range) { 2468 jio_fprintf(defaultStream::error_stream(), 2469 "Invalid maximum heap size: %s\n", option->optionString); 2470 describe_range_error(errcode); 2471 return JNI_EINVAL; 2472 } 2473 if (FLAG_SET_CMDLINE(MaxHeapSize, (size_t)long_max_heap_size) != JVMFlag::SUCCESS) { 2474 return JNI_EINVAL; 2475 } 2476 // Xmaxf 2477 } else if (match_option(option, "-Xmaxf", &tail)) { 2478 char* err; 2479 int maxf = (int)(strtod(tail, &err) * 100); 2480 if (*err != '\0' || *tail == '\0') { 2481 jio_fprintf(defaultStream::error_stream(), 2482 "Bad max heap free percentage size: %s\n", 2483 option->optionString); 2484 return JNI_EINVAL; 2485 } else { 2486 if (FLAG_SET_CMDLINE(MaxHeapFreeRatio, maxf) != JVMFlag::SUCCESS) { 2487 return JNI_EINVAL; 2488 } 2489 } 2490 // Xminf 2491 } else if (match_option(option, "-Xminf", &tail)) { 2492 char* err; 2493 int minf = (int)(strtod(tail, &err) * 100); 2494 if (*err != '\0' || *tail == '\0') { 2495 jio_fprintf(defaultStream::error_stream(), 2496 "Bad min heap free percentage size: %s\n", 2497 option->optionString); 2498 return JNI_EINVAL; 2499 } else { 2500 if (FLAG_SET_CMDLINE(MinHeapFreeRatio, minf) != JVMFlag::SUCCESS) { 2501 return JNI_EINVAL; 2502 } 2503 } 2504 // -Xss 2505 } else if (match_option(option, "-Xss", &tail)) { 2506 intx value = 0; 2507 jint err = parse_xss(option, tail, &value); 2508 if (err != JNI_OK) { 2509 return err; 2510 } 2511 if (FLAG_SET_CMDLINE(ThreadStackSize, value) != JVMFlag::SUCCESS) { 2512 return JNI_EINVAL; 2513 } 2514 } else if (match_option(option, "-Xmaxjitcodesize", &tail) || 2515 match_option(option, "-XX:ReservedCodeCacheSize=", &tail)) { 2516 julong long_ReservedCodeCacheSize = 0; 2517 2518 ArgsRange errcode = parse_memory_size(tail, &long_ReservedCodeCacheSize, 1); 2519 if (errcode != arg_in_range) { 2520 jio_fprintf(defaultStream::error_stream(), 2521 "Invalid maximum code cache size: %s.\n", option->optionString); 2522 return JNI_EINVAL; 2523 } 2524 if (FLAG_SET_CMDLINE(ReservedCodeCacheSize, (uintx)long_ReservedCodeCacheSize) != JVMFlag::SUCCESS) { 2525 return JNI_EINVAL; 2526 } 2527 // -green 2528 } else if (match_option(option, "-green")) { 2529 jio_fprintf(defaultStream::error_stream(), 2530 "Green threads support not available\n"); 2531 return JNI_EINVAL; 2532 // -native 2533 } else if (match_option(option, "-native")) { 2534 // HotSpot always uses native threads, ignore silently for compatibility 2535 // -Xrs 2536 } else if (match_option(option, "-Xrs")) { 2537 // Classic/EVM option, new functionality 2538 if (FLAG_SET_CMDLINE(ReduceSignalUsage, true) != JVMFlag::SUCCESS) { 2539 return JNI_EINVAL; 2540 } 2541 // -Xprof 2542 } else if (match_option(option, "-Xprof")) { 2543 char version[256]; 2544 // Obsolete in JDK 10 2545 JDK_Version::jdk(10).to_string(version, sizeof(version)); 2546 warning("Ignoring option %s; support was removed in %s", option->optionString, version); 2547 // -Xinternalversion 2548 } else if (match_option(option, "-Xinternalversion")) { 2549 jio_fprintf(defaultStream::output_stream(), "%s\n", 2550 VM_Version::internal_vm_info_string()); 2551 vm_exit(0); 2552 #ifndef PRODUCT 2553 // -Xprintflags 2554 } else if (match_option(option, "-Xprintflags")) { 2555 JVMFlag::printFlags(tty, false); 2556 vm_exit(0); 2557 #endif 2558 // -D 2559 } else if (match_option(option, "-D", &tail)) { 2560 const char* value; 2561 if (match_option(option, "-Djava.endorsed.dirs=", &value) && 2562 *value!= '\0' && strcmp(value, "\"\"") != 0) { 2563 // abort if -Djava.endorsed.dirs is set 2564 jio_fprintf(defaultStream::output_stream(), 2565 "-Djava.endorsed.dirs=%s is not supported. Endorsed standards and standalone APIs\n" 2566 "in modular form will be supported via the concept of upgradeable modules.\n", value); 2567 return JNI_EINVAL; 2568 } 2569 if (match_option(option, "-Djava.ext.dirs=", &value) && 2570 *value != '\0' && strcmp(value, "\"\"") != 0) { 2571 // abort if -Djava.ext.dirs is set 2572 jio_fprintf(defaultStream::output_stream(), 2573 "-Djava.ext.dirs=%s is not supported. Use -classpath instead.\n", value); 2574 return JNI_EINVAL; 2575 } 2576 // Check for module related properties. They must be set using the modules 2577 // options. For example: use "--add-modules=java.sql", not 2578 // "-Djdk.module.addmods=java.sql" 2579 if (is_internal_module_property(option->optionString + 2)) { 2580 needs_module_property_warning = true; 2581 continue; 2582 } 2583 if (!add_property(tail)) { 2584 return JNI_ENOMEM; 2585 } 2586 // Out of the box management support 2587 if (match_option(option, "-Dcom.sun.management", &tail)) { 2588 #if INCLUDE_MANAGEMENT 2589 if (FLAG_SET_CMDLINE(ManagementServer, true) != JVMFlag::SUCCESS) { 2590 return JNI_EINVAL; 2591 } 2592 // management agent in module jdk.management.agent 2593 if (!create_numbered_module_property("jdk.module.addmods", "jdk.management.agent", addmods_count++)) { 2594 return JNI_ENOMEM; 2595 } 2596 #else 2597 jio_fprintf(defaultStream::output_stream(), 2598 "-Dcom.sun.management is not supported in this VM.\n"); 2599 return JNI_ERR; 2600 #endif 2601 } 2602 // -Xint 2603 } else if (match_option(option, "-Xint")) { 2604 set_mode_flags(_int); 2605 // -Xmixed 2606 } else if (match_option(option, "-Xmixed")) { 2607 set_mode_flags(_mixed); 2608 // -Xcomp 2609 } else if (match_option(option, "-Xcomp")) { 2610 // for testing the compiler; turn off all flags that inhibit compilation 2611 set_mode_flags(_comp); 2612 // -Xshare:dump 2613 } else if (match_option(option, "-Xshare:dump")) { 2614 DumpSharedSpaces = true; 2615 // -Xshare:on 2616 } else if (match_option(option, "-Xshare:on")) { 2617 UseSharedSpaces = true; 2618 RequireSharedSpaces = true; 2619 // -Xshare:auto || -XX:ArchiveClassesAtExit=<archive file> 2620 } else if (match_option(option, "-Xshare:auto")) { 2621 UseSharedSpaces = true; 2622 RequireSharedSpaces = false; 2623 xshare_auto_cmd_line = true; 2624 // -Xshare:off 2625 } else if (match_option(option, "-Xshare:off")) { 2626 UseSharedSpaces = false; 2627 RequireSharedSpaces = false; 2628 // -Xverify 2629 } else if (match_option(option, "-Xverify", &tail)) { 2630 if (strcmp(tail, ":all") == 0 || strcmp(tail, "") == 0) { 2631 if (FLAG_SET_CMDLINE(BytecodeVerificationLocal, true) != JVMFlag::SUCCESS) { 2632 return JNI_EINVAL; 2633 } 2634 if (FLAG_SET_CMDLINE(BytecodeVerificationRemote, true) != JVMFlag::SUCCESS) { 2635 return JNI_EINVAL; 2636 } 2637 } else if (strcmp(tail, ":remote") == 0) { 2638 if (FLAG_SET_CMDLINE(BytecodeVerificationLocal, false) != JVMFlag::SUCCESS) { 2639 return JNI_EINVAL; 2640 } 2641 if (FLAG_SET_CMDLINE(BytecodeVerificationRemote, true) != JVMFlag::SUCCESS) { 2642 return JNI_EINVAL; 2643 } 2644 } else if (strcmp(tail, ":none") == 0) { 2645 if (FLAG_SET_CMDLINE(BytecodeVerificationLocal, false) != JVMFlag::SUCCESS) { 2646 return JNI_EINVAL; 2647 } 2648 if (FLAG_SET_CMDLINE(BytecodeVerificationRemote, false) != JVMFlag::SUCCESS) { 2649 return JNI_EINVAL; 2650 } 2651 warning("Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release."); 2652 } else if (is_bad_option(option, args->ignoreUnrecognized, "verification")) { 2653 return JNI_EINVAL; 2654 } 2655 // -Xdebug 2656 } else if (match_option(option, "-Xdebug")) { 2657 warning("Option -Xdebug was deprecated in JDK 22 and will likely be removed in a future release."); 2658 // -Xnoagent 2659 } else if (match_option(option, "-Xnoagent")) { 2660 warning("Option -Xnoagent was deprecated in JDK 22 and will likely be removed in a future release."); 2661 } else if (match_option(option, "-Xloggc:", &tail)) { 2662 // Deprecated flag to redirect GC output to a file. -Xloggc:<filename> 2663 log_warning(gc)("-Xloggc is deprecated. Will use -Xlog:gc:%s instead.", tail); 2664 _legacyGCLogging.lastFlag = 2; 2665 _legacyGCLogging.file = os::strdup_check_oom(tail); 2666 } else if (match_option(option, "-Xlog", &tail)) { 2667 bool ret = false; 2668 if (strcmp(tail, ":help") == 0) { 2669 fileStream stream(defaultStream::output_stream()); 2670 LogConfiguration::print_command_line_help(&stream); 2671 vm_exit(0); 2672 } else if (strcmp(tail, ":disable") == 0) { 2673 LogConfiguration::disable_logging(); 2674 ret = true; 2675 } else if (strcmp(tail, ":async") == 0) { 2676 LogConfiguration::set_async_mode(true); 2677 ret = true; 2678 } else if (*tail == '\0') { 2679 ret = LogConfiguration::parse_command_line_arguments(); 2680 assert(ret, "-Xlog without arguments should never fail to parse"); 2681 } else if (*tail == ':') { 2682 ret = LogConfiguration::parse_command_line_arguments(tail + 1); 2683 } 2684 if (ret == false) { 2685 jio_fprintf(defaultStream::error_stream(), 2686 "Invalid -Xlog option '-Xlog%s', see error log for details.\n", 2687 tail); 2688 return JNI_EINVAL; 2689 } 2690 // JNI hooks 2691 } else if (match_option(option, "-Xcheck", &tail)) { 2692 if (!strcmp(tail, ":jni")) { 2693 #if !INCLUDE_JNI_CHECK 2694 warning("JNI CHECKING is not supported in this VM"); 2695 #else 2696 CheckJNICalls = true; 2697 #endif // INCLUDE_JNI_CHECK 2698 } else if (is_bad_option(option, args->ignoreUnrecognized, 2699 "check")) { 2700 return JNI_EINVAL; 2701 } 2702 } else if (match_option(option, "vfprintf")) { 2703 _vfprintf_hook = CAST_TO_FN_PTR(vfprintf_hook_t, option->extraInfo); 2704 } else if (match_option(option, "exit")) { 2705 _exit_hook = CAST_TO_FN_PTR(exit_hook_t, option->extraInfo); 2706 } else if (match_option(option, "abort")) { 2707 _abort_hook = CAST_TO_FN_PTR(abort_hook_t, option->extraInfo); 2708 // Need to keep consistency of MaxTenuringThreshold and AlwaysTenure/NeverTenure; 2709 // and the last option wins. 2710 } else if (match_option(option, "-XX:+NeverTenure")) { 2711 if (FLAG_SET_CMDLINE(NeverTenure, true) != JVMFlag::SUCCESS) { 2712 return JNI_EINVAL; 2713 } 2714 if (FLAG_SET_CMDLINE(AlwaysTenure, false) != JVMFlag::SUCCESS) { 2715 return JNI_EINVAL; 2716 } 2717 if (FLAG_SET_CMDLINE(MaxTenuringThreshold, markWord::max_age + 1) != JVMFlag::SUCCESS) { 2718 return JNI_EINVAL; 2719 } 2720 } else if (match_option(option, "-XX:+AlwaysTenure")) { 2721 if (FLAG_SET_CMDLINE(NeverTenure, false) != JVMFlag::SUCCESS) { 2722 return JNI_EINVAL; 2723 } 2724 if (FLAG_SET_CMDLINE(AlwaysTenure, true) != JVMFlag::SUCCESS) { 2725 return JNI_EINVAL; 2726 } 2727 if (FLAG_SET_CMDLINE(MaxTenuringThreshold, 0) != JVMFlag::SUCCESS) { 2728 return JNI_EINVAL; 2729 } 2730 } else if (match_option(option, "-XX:MaxTenuringThreshold=", &tail)) { 2731 uint max_tenuring_thresh = 0; 2732 if (!parse_uint(tail, &max_tenuring_thresh, 0)) { 2733 jio_fprintf(defaultStream::error_stream(), 2734 "Improperly specified VM option \'MaxTenuringThreshold=%s\'\n", tail); 2735 return JNI_EINVAL; 2736 } 2737 2738 if (FLAG_SET_CMDLINE(MaxTenuringThreshold, max_tenuring_thresh) != JVMFlag::SUCCESS) { 2739 return JNI_EINVAL; 2740 } 2741 2742 if (MaxTenuringThreshold == 0) { 2743 if (FLAG_SET_CMDLINE(NeverTenure, false) != JVMFlag::SUCCESS) { 2744 return JNI_EINVAL; 2745 } 2746 if (FLAG_SET_CMDLINE(AlwaysTenure, true) != JVMFlag::SUCCESS) { 2747 return JNI_EINVAL; 2748 } 2749 } else { 2750 if (FLAG_SET_CMDLINE(NeverTenure, false) != JVMFlag::SUCCESS) { 2751 return JNI_EINVAL; 2752 } 2753 if (FLAG_SET_CMDLINE(AlwaysTenure, false) != JVMFlag::SUCCESS) { 2754 return JNI_EINVAL; 2755 } 2756 } 2757 } else if (match_option(option, "-XX:+DisplayVMOutputToStderr")) { 2758 if (FLAG_SET_CMDLINE(DisplayVMOutputToStdout, false) != JVMFlag::SUCCESS) { 2759 return JNI_EINVAL; 2760 } 2761 if (FLAG_SET_CMDLINE(DisplayVMOutputToStderr, true) != JVMFlag::SUCCESS) { 2762 return JNI_EINVAL; 2763 } 2764 } else if (match_option(option, "-XX:+DisplayVMOutputToStdout")) { 2765 if (FLAG_SET_CMDLINE(DisplayVMOutputToStderr, false) != JVMFlag::SUCCESS) { 2766 return JNI_EINVAL; 2767 } 2768 if (FLAG_SET_CMDLINE(DisplayVMOutputToStdout, true) != JVMFlag::SUCCESS) { 2769 return JNI_EINVAL; 2770 } 2771 } else if (match_option(option, "-XX:+ErrorFileToStderr")) { 2772 if (FLAG_SET_CMDLINE(ErrorFileToStdout, false) != JVMFlag::SUCCESS) { 2773 return JNI_EINVAL; 2774 } 2775 if (FLAG_SET_CMDLINE(ErrorFileToStderr, true) != JVMFlag::SUCCESS) { 2776 return JNI_EINVAL; 2777 } 2778 } else if (match_option(option, "-XX:+ErrorFileToStdout")) { 2779 if (FLAG_SET_CMDLINE(ErrorFileToStderr, false) != JVMFlag::SUCCESS) { 2780 return JNI_EINVAL; 2781 } 2782 if (FLAG_SET_CMDLINE(ErrorFileToStdout, true) != JVMFlag::SUCCESS) { 2783 return JNI_EINVAL; 2784 } 2785 } else if (match_option(option, "--finalization=", &tail)) { 2786 if (strcmp(tail, "enabled") == 0) { 2787 InstanceKlass::set_finalization_enabled(true); 2788 } else if (strcmp(tail, "disabled") == 0) { 2789 InstanceKlass::set_finalization_enabled(false); 2790 } else { 2791 jio_fprintf(defaultStream::error_stream(), 2792 "Invalid finalization value '%s', must be 'disabled' or 'enabled'.\n", 2793 tail); 2794 return JNI_EINVAL; 2795 } 2796 #if !defined(DTRACE_ENABLED) 2797 } else if (match_option(option, "-XX:+DTraceMethodProbes")) { 2798 jio_fprintf(defaultStream::error_stream(), 2799 "DTraceMethodProbes flag is not applicable for this configuration\n"); 2800 return JNI_EINVAL; 2801 } else if (match_option(option, "-XX:+DTraceAllocProbes")) { 2802 jio_fprintf(defaultStream::error_stream(), 2803 "DTraceAllocProbes flag is not applicable for this configuration\n"); 2804 return JNI_EINVAL; 2805 } else if (match_option(option, "-XX:+DTraceMonitorProbes")) { 2806 jio_fprintf(defaultStream::error_stream(), 2807 "DTraceMonitorProbes flag is not applicable for this configuration\n"); 2808 return JNI_EINVAL; 2809 #endif // !defined(DTRACE_ENABLED) 2810 #ifdef ASSERT 2811 } else if (match_option(option, "-XX:+FullGCALot")) { 2812 if (FLAG_SET_CMDLINE(FullGCALot, true) != JVMFlag::SUCCESS) { 2813 return JNI_EINVAL; 2814 } 2815 // disable scavenge before parallel mark-compact 2816 if (FLAG_SET_CMDLINE(ScavengeBeforeFullGC, false) != JVMFlag::SUCCESS) { 2817 return JNI_EINVAL; 2818 } 2819 #endif 2820 #if !INCLUDE_MANAGEMENT 2821 } else if (match_option(option, "-XX:+ManagementServer")) { 2822 jio_fprintf(defaultStream::error_stream(), 2823 "ManagementServer is not supported in this VM.\n"); 2824 return JNI_ERR; 2825 #endif // INCLUDE_MANAGEMENT 2826 #if INCLUDE_JVMCI 2827 } else if (match_option(option, "-XX:-EnableJVMCIProduct") || match_option(option, "-XX:-UseGraalJIT")) { 2828 if (EnableJVMCIProduct) { 2829 jio_fprintf(defaultStream::error_stream(), 2830 "-XX:-EnableJVMCIProduct or -XX:-UseGraalJIT cannot come after -XX:+EnableJVMCIProduct or -XX:+UseGraalJIT\n"); 2831 return JNI_EINVAL; 2832 } 2833 } else if (match_option(option, "-XX:+EnableJVMCIProduct") || match_option(option, "-XX:+UseGraalJIT")) { 2834 bool use_graal_jit = match_option(option, "-XX:+UseGraalJIT"); 2835 if (use_graal_jit) { 2836 const char* jvmci_compiler = get_property("jvmci.Compiler"); 2837 if (jvmci_compiler != nullptr) { 2838 if (strncmp(jvmci_compiler, "graal", strlen("graal")) != 0) { 2839 jio_fprintf(defaultStream::error_stream(), 2840 "Value of jvmci.Compiler incompatible with +UseGraalJIT: %s\n", jvmci_compiler); 2841 return JNI_ERR; 2842 } 2843 } else if (!add_property("jvmci.Compiler=graal")) { 2844 return JNI_ENOMEM; 2845 } 2846 } 2847 2848 // Just continue, since "-XX:+EnableJVMCIProduct" or "-XX:+UseGraalJIT" has been specified before 2849 if (EnableJVMCIProduct) { 2850 continue; 2851 } 2852 JVMFlag *jvmciFlag = JVMFlag::find_flag("EnableJVMCIProduct"); 2853 // Allow this flag if it has been unlocked. 2854 if (jvmciFlag != nullptr && jvmciFlag->is_unlocked()) { 2855 if (!JVMCIGlobals::enable_jvmci_product_mode(origin, use_graal_jit)) { 2856 jio_fprintf(defaultStream::error_stream(), 2857 "Unable to enable JVMCI in product mode\n"); 2858 return JNI_ERR; 2859 } 2860 } 2861 // The flag was locked so process normally to report that error 2862 else if (!process_argument(use_graal_jit ? "UseGraalJIT" : "EnableJVMCIProduct", args->ignoreUnrecognized, origin)) { 2863 return JNI_EINVAL; 2864 } 2865 #endif // INCLUDE_JVMCI 2866 #if INCLUDE_JFR 2867 } else if (match_jfr_option(&option)) { 2868 return JNI_EINVAL; 2869 #endif 2870 } else if (match_option(option, "-XX:", &tail)) { // -XX:xxxx 2871 // Skip -XX:Flags= and -XX:VMOptionsFile= since those cases have 2872 // already been handled 2873 if ((strncmp(tail, "Flags=", strlen("Flags=")) != 0) && 2874 (strncmp(tail, "VMOptionsFile=", strlen("VMOptionsFile=")) != 0)) { 2875 if (!process_argument(tail, args->ignoreUnrecognized, origin)) { 2876 return JNI_EINVAL; 2877 } 2878 } 2879 // Unknown option 2880 } else if (is_bad_option(option, args->ignoreUnrecognized)) { 2881 return JNI_ERR; 2882 } 2883 } 2884 2885 // PrintSharedArchiveAndExit will turn on 2886 // -Xshare:on 2887 // -Xlog:class+path=info 2888 if (PrintSharedArchiveAndExit) { 2889 UseSharedSpaces = true; 2890 RequireSharedSpaces = true; 2891 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, path)); 2892 } 2893 2894 fix_appclasspath(); 2895 2896 return JNI_OK; 2897 } 2898 2899 void Arguments::add_patch_mod_prefix(const char* module_name, const char* path, bool* patch_mod_javabase) { 2900 // For java.base check for duplicate --patch-module options being specified on the command line. 2901 // This check is only required for java.base, all other duplicate module specifications 2902 // will be checked during module system initialization. The module system initialization 2903 // will throw an ExceptionInInitializerError if this situation occurs. 2904 if (strcmp(module_name, JAVA_BASE_NAME) == 0) { 2905 if (*patch_mod_javabase) { 2906 vm_exit_during_initialization("Cannot specify " JAVA_BASE_NAME " more than once to --patch-module"); 2907 } else { 2908 *patch_mod_javabase = true; 2909 } 2910 } 2911 2912 // Create GrowableArray lazily, only if --patch-module has been specified 2913 if (_patch_mod_prefix == nullptr) { 2914 _patch_mod_prefix = new (mtArguments) GrowableArray<ModulePatchPath*>(10, mtArguments); 2915 } 2916 2917 _patch_mod_prefix->push(new ModulePatchPath(module_name, path)); 2918 } 2919 2920 // Remove all empty paths from the app classpath (if IgnoreEmptyClassPaths is enabled) 2921 // 2922 // This is necessary because some apps like to specify classpath like -cp foo.jar:${XYZ}:bar.jar 2923 // in their start-up scripts. If XYZ is empty, the classpath will look like "-cp foo.jar::bar.jar". 2924 // Java treats such empty paths as if the user specified "-cp foo.jar:.:bar.jar". I.e., an empty 2925 // path is treated as the current directory. 2926 // 2927 // This causes problems with CDS, which requires that all directories specified in the classpath 2928 // must be empty. In most cases, applications do NOT want to load classes from the current 2929 // directory anyway. Adding -XX:+IgnoreEmptyClassPaths will make these applications' start-up 2930 // scripts compatible with CDS. 2931 void Arguments::fix_appclasspath() { 2932 if (IgnoreEmptyClassPaths) { 2933 const char separator = *os::path_separator(); 2934 const char* src = _java_class_path->value(); 2935 2936 // skip over all the leading empty paths 2937 while (*src == separator) { 2938 src ++; 2939 } 2940 2941 char* copy = os::strdup_check_oom(src, mtArguments); 2942 2943 // trim all trailing empty paths 2944 for (char* tail = copy + strlen(copy) - 1; tail >= copy && *tail == separator; tail--) { 2945 *tail = '\0'; 2946 } 2947 2948 char from[3] = {separator, separator, '\0'}; 2949 char to [2] = {separator, '\0'}; 2950 while (StringUtils::replace_no_expand(copy, from, to) > 0) { 2951 // Keep replacing "::" -> ":" until we have no more "::" (non-windows) 2952 // Keep replacing ";;" -> ";" until we have no more ";;" (windows) 2953 } 2954 2955 _java_class_path->set_writeable_value(copy); 2956 FreeHeap(copy); // a copy was made by set_value, so don't need this anymore 2957 } 2958 } 2959 2960 jint Arguments::finalize_vm_init_args(bool patch_mod_javabase) { 2961 // check if the default lib/endorsed directory exists; if so, error 2962 char path[JVM_MAXPATHLEN]; 2963 const char* fileSep = os::file_separator(); 2964 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sendorsed", Arguments::get_java_home(), fileSep, fileSep); 2965 2966 DIR* dir = os::opendir(path); 2967 if (dir != nullptr) { 2968 jio_fprintf(defaultStream::output_stream(), 2969 "<JAVA_HOME>/lib/endorsed is not supported. Endorsed standards and standalone APIs\n" 2970 "in modular form will be supported via the concept of upgradeable modules.\n"); 2971 os::closedir(dir); 2972 return JNI_ERR; 2973 } 2974 2975 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sext", Arguments::get_java_home(), fileSep, fileSep); 2976 dir = os::opendir(path); 2977 if (dir != nullptr) { 2978 jio_fprintf(defaultStream::output_stream(), 2979 "<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; " 2980 "Use -classpath instead.\n."); 2981 os::closedir(dir); 2982 return JNI_ERR; 2983 } 2984 2985 // This must be done after all arguments have been processed 2986 // and the container support has been initialized since AggressiveHeap 2987 // relies on the amount of total memory available. 2988 if (AggressiveHeap) { 2989 jint result = set_aggressive_heap_flags(); 2990 if (result != JNI_OK) { 2991 return result; 2992 } 2993 } 2994 2995 // CompileThresholdScaling == 0.0 is same as -Xint: Disable compilation (enable interpreter-only mode), 2996 // but like -Xint, leave compilation thresholds unaffected. 2997 // With tiered compilation disabled, setting CompileThreshold to 0 disables compilation as well. 2998 if ((CompileThresholdScaling == 0.0) || (!TieredCompilation && CompileThreshold == 0)) { 2999 set_mode_flags(_int); 3000 } 3001 3002 #ifdef ZERO 3003 // Zero always runs in interpreted mode 3004 set_mode_flags(_int); 3005 #endif 3006 3007 // eventually fix up InitialTenuringThreshold if only MaxTenuringThreshold is set 3008 if (FLAG_IS_DEFAULT(InitialTenuringThreshold) && (InitialTenuringThreshold > MaxTenuringThreshold)) { 3009 FLAG_SET_ERGO(InitialTenuringThreshold, MaxTenuringThreshold); 3010 } 3011 3012 #if !COMPILER2_OR_JVMCI 3013 // Don't degrade server performance for footprint 3014 if (FLAG_IS_DEFAULT(UseLargePages) && 3015 MaxHeapSize < LargePageHeapSizeThreshold) { 3016 // No need for large granularity pages w/small heaps. 3017 // Note that large pages are enabled/disabled for both the 3018 // Java heap and the code cache. 3019 FLAG_SET_DEFAULT(UseLargePages, false); 3020 } 3021 3022 UNSUPPORTED_OPTION(ProfileInterpreter); 3023 #endif 3024 3025 // Parse the CompilationMode flag 3026 if (!CompilationModeFlag::initialize()) { 3027 return JNI_ERR; 3028 } 3029 3030 if (!check_vm_args_consistency()) { 3031 return JNI_ERR; 3032 } 3033 3034 #if INCLUDE_CDS 3035 if (DumpSharedSpaces) { 3036 // Compiler threads may concurrently update the class metadata (such as method entries), so it's 3037 // unsafe with -Xshare:dump (which modifies the class metadata in place). Let's disable 3038 // compiler just to be safe. 3039 // 3040 // Note: this is not a concern for dynamically dumping shared spaces, which makes a copy of the 3041 // class metadata instead of modifying them in place. The copy is inaccessible to the compiler. 3042 // TODO: revisit the following for the static archive case. 3043 set_mode_flags(_int); 3044 3045 // String deduplication may cause CDS to iterate the strings in different order from one 3046 // run to another which resulting in non-determinstic CDS archives. 3047 // Disable UseStringDeduplication while dumping CDS archive. 3048 UseStringDeduplication = false; 3049 } 3050 3051 // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit 3052 if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) { 3053 jio_fprintf(defaultStream::output_stream(), 3054 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n"); 3055 return JNI_ERR; 3056 } 3057 3058 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) { 3059 DynamicDumpSharedSpaces = false; 3060 } else { 3061 DynamicDumpSharedSpaces = true; 3062 } 3063 3064 if (AutoCreateSharedArchive) { 3065 if (SharedArchiveFile == nullptr) { 3066 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile"); 3067 return JNI_ERR; 3068 } 3069 if (ArchiveClassesAtExit != nullptr) { 3070 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit"); 3071 return JNI_ERR; 3072 } 3073 } 3074 3075 if (UseSharedSpaces && patch_mod_javabase) { 3076 no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched."); 3077 } 3078 if (UseSharedSpaces && !DumpSharedSpaces && check_unsupported_cds_runtime_properties()) { 3079 UseSharedSpaces = false; 3080 } 3081 3082 if (DumpSharedSpaces || DynamicDumpSharedSpaces) { 3083 // Always verify non-system classes during CDS dump 3084 if (!BytecodeVerificationRemote) { 3085 BytecodeVerificationRemote = true; 3086 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time."); 3087 } 3088 } 3089 #endif 3090 3091 #ifndef CAN_SHOW_REGISTERS_ON_ASSERT 3092 UNSUPPORTED_OPTION(ShowRegistersOnAssert); 3093 #endif // CAN_SHOW_REGISTERS_ON_ASSERT 3094 3095 #ifdef _LP64 3096 if (UseCompactObjectHeaders && UseZGC) { 3097 warning("ZGC does not work with compact object headers, disabling UseCompactObjectHeaders"); 3098 FLAG_SET_DEFAULT(UseCompactObjectHeaders, false); 3099 } 3100 3101 if (UseCompactObjectHeaders && FLAG_IS_CMDLINE(UseCompressedClassPointers) && !UseCompressedClassPointers) { 3102 // If user specifies -UseCompressedClassPointers, disable compact headers with a warning. 3103 warning("Compact object headers require compressed class pointers. Disabling compact object headers."); 3104 FLAG_SET_DEFAULT(UseCompactObjectHeaders, false); 3105 } 3106 3107 if (UseCompactObjectHeaders && LockingMode == LM_LEGACY) { 3108 FLAG_SET_DEFAULT(LockingMode, LM_LIGHTWEIGHT); 3109 } 3110 #endif 3111 3112 return JNI_OK; 3113 } 3114 3115 // Helper class for controlling the lifetime of JavaVMInitArgs 3116 // objects. The contents of the JavaVMInitArgs are guaranteed to be 3117 // deleted on the destruction of the ScopedVMInitArgs object. 3118 class ScopedVMInitArgs : public StackObj { 3119 private: 3120 JavaVMInitArgs _args; 3121 char* _container_name; 3122 bool _is_set; 3123 char* _vm_options_file_arg; 3124 3125 public: 3126 ScopedVMInitArgs(const char *container_name) { 3127 _args.version = JNI_VERSION_1_2; 3128 _args.nOptions = 0; 3129 _args.options = nullptr; 3130 _args.ignoreUnrecognized = false; 3131 _container_name = (char *)container_name; 3132 _is_set = false; 3133 _vm_options_file_arg = nullptr; 3134 } 3135 3136 // Populates the JavaVMInitArgs object represented by this 3137 // ScopedVMInitArgs object with the arguments in options. The 3138 // allocated memory is deleted by the destructor. If this method 3139 // returns anything other than JNI_OK, then this object is in a 3140 // partially constructed state, and should be abandoned. 3141 jint set_args(const GrowableArrayView<JavaVMOption>* options) { 3142 _is_set = true; 3143 JavaVMOption* options_arr = NEW_C_HEAP_ARRAY_RETURN_NULL( 3144 JavaVMOption, options->length(), mtArguments); 3145 if (options_arr == nullptr) { 3146 return JNI_ENOMEM; 3147 } 3148 _args.options = options_arr; 3149 3150 for (int i = 0; i < options->length(); i++) { 3151 options_arr[i] = options->at(i); 3152 options_arr[i].optionString = os::strdup(options_arr[i].optionString); 3153 if (options_arr[i].optionString == nullptr) { 3154 // Rely on the destructor to do cleanup. 3155 _args.nOptions = i; 3156 return JNI_ENOMEM; 3157 } 3158 } 3159 3160 _args.nOptions = options->length(); 3161 _args.ignoreUnrecognized = IgnoreUnrecognizedVMOptions; 3162 return JNI_OK; 3163 } 3164 3165 JavaVMInitArgs* get() { return &_args; } 3166 char* container_name() { return _container_name; } 3167 bool is_set() { return _is_set; } 3168 bool found_vm_options_file_arg() { return _vm_options_file_arg != nullptr; } 3169 char* vm_options_file_arg() { return _vm_options_file_arg; } 3170 3171 void set_vm_options_file_arg(const char *vm_options_file_arg) { 3172 if (_vm_options_file_arg != nullptr) { 3173 os::free(_vm_options_file_arg); 3174 } 3175 _vm_options_file_arg = os::strdup_check_oom(vm_options_file_arg); 3176 } 3177 3178 ~ScopedVMInitArgs() { 3179 if (_vm_options_file_arg != nullptr) { 3180 os::free(_vm_options_file_arg); 3181 } 3182 if (_args.options == nullptr) return; 3183 for (int i = 0; i < _args.nOptions; i++) { 3184 os::free(_args.options[i].optionString); 3185 } 3186 FREE_C_HEAP_ARRAY(JavaVMOption, _args.options); 3187 } 3188 3189 // Insert options into this option list, to replace option at 3190 // vm_options_file_pos (-XX:VMOptionsFile) 3191 jint insert(const JavaVMInitArgs* args, 3192 const JavaVMInitArgs* args_to_insert, 3193 const int vm_options_file_pos) { 3194 assert(_args.options == nullptr, "shouldn't be set yet"); 3195 assert(args_to_insert->nOptions != 0, "there should be args to insert"); 3196 assert(vm_options_file_pos != -1, "vm_options_file_pos should be set"); 3197 3198 int length = args->nOptions + args_to_insert->nOptions - 1; 3199 // Construct new option array 3200 GrowableArrayCHeap<JavaVMOption, mtArguments> options(length); 3201 for (int i = 0; i < args->nOptions; i++) { 3202 if (i == vm_options_file_pos) { 3203 // insert the new options starting at the same place as the 3204 // -XX:VMOptionsFile option 3205 for (int j = 0; j < args_to_insert->nOptions; j++) { 3206 options.push(args_to_insert->options[j]); 3207 } 3208 } else { 3209 options.push(args->options[i]); 3210 } 3211 } 3212 // make into options array 3213 return set_args(&options); 3214 } 3215 }; 3216 3217 jint Arguments::parse_java_options_environment_variable(ScopedVMInitArgs* args) { 3218 return parse_options_environment_variable("_JAVA_OPTIONS", args); 3219 } 3220 3221 jint Arguments::parse_java_tool_options_environment_variable(ScopedVMInitArgs* args) { 3222 return parse_options_environment_variable("JAVA_TOOL_OPTIONS", args); 3223 } 3224 3225 jint Arguments::parse_options_environment_variable(const char* name, 3226 ScopedVMInitArgs* vm_args) { 3227 char *buffer = ::getenv(name); 3228 3229 // Don't check this environment variable if user has special privileges 3230 // (e.g. unix su command). 3231 if (buffer == nullptr || os::have_special_privileges()) { 3232 return JNI_OK; 3233 } 3234 3235 if ((buffer = os::strdup(buffer)) == nullptr) { 3236 return JNI_ENOMEM; 3237 } 3238 3239 jio_fprintf(defaultStream::error_stream(), 3240 "Picked up %s: %s\n", name, buffer); 3241 3242 int retcode = parse_options_buffer(name, buffer, strlen(buffer), vm_args); 3243 3244 os::free(buffer); 3245 return retcode; 3246 } 3247 3248 jint Arguments::parse_vm_options_file(const char* file_name, ScopedVMInitArgs* vm_args) { 3249 // read file into buffer 3250 int fd = ::open(file_name, O_RDONLY); 3251 if (fd < 0) { 3252 jio_fprintf(defaultStream::error_stream(), 3253 "Could not open options file '%s'\n", 3254 file_name); 3255 return JNI_ERR; 3256 } 3257 3258 struct stat stbuf; 3259 int retcode = os::stat(file_name, &stbuf); 3260 if (retcode != 0) { 3261 jio_fprintf(defaultStream::error_stream(), 3262 "Could not stat options file '%s'\n", 3263 file_name); 3264 ::close(fd); 3265 return JNI_ERR; 3266 } 3267 3268 if (stbuf.st_size == 0) { 3269 // tell caller there is no option data and that is ok 3270 ::close(fd); 3271 return JNI_OK; 3272 } 3273 3274 // '+ 1' for null termination even with max bytes 3275 size_t bytes_alloc = stbuf.st_size + 1; 3276 3277 char *buf = NEW_C_HEAP_ARRAY_RETURN_NULL(char, bytes_alloc, mtArguments); 3278 if (nullptr == buf) { 3279 jio_fprintf(defaultStream::error_stream(), 3280 "Could not allocate read buffer for options file parse\n"); 3281 ::close(fd); 3282 return JNI_ENOMEM; 3283 } 3284 3285 memset(buf, 0, bytes_alloc); 3286 3287 // Fill buffer 3288 ssize_t bytes_read = ::read(fd, (void *)buf, (unsigned)bytes_alloc); 3289 ::close(fd); 3290 if (bytes_read < 0) { 3291 FREE_C_HEAP_ARRAY(char, buf); 3292 jio_fprintf(defaultStream::error_stream(), 3293 "Could not read options file '%s'\n", file_name); 3294 return JNI_ERR; 3295 } 3296 3297 if (bytes_read == 0) { 3298 // tell caller there is no option data and that is ok 3299 FREE_C_HEAP_ARRAY(char, buf); 3300 return JNI_OK; 3301 } 3302 3303 retcode = parse_options_buffer(file_name, buf, bytes_read, vm_args); 3304 3305 FREE_C_HEAP_ARRAY(char, buf); 3306 return retcode; 3307 } 3308 3309 jint Arguments::parse_options_buffer(const char* name, char* buffer, const size_t buf_len, ScopedVMInitArgs* vm_args) { 3310 // Construct option array 3311 GrowableArrayCHeap<JavaVMOption, mtArguments> options(2); 3312 3313 // some pointers to help with parsing 3314 char *buffer_end = buffer + buf_len; 3315 char *opt_hd = buffer; 3316 char *wrt = buffer; 3317 char *rd = buffer; 3318 3319 // parse all options 3320 while (rd < buffer_end) { 3321 // skip leading white space from the input string 3322 while (rd < buffer_end && isspace(*rd)) { 3323 rd++; 3324 } 3325 3326 if (rd >= buffer_end) { 3327 break; 3328 } 3329 3330 // Remember this is where we found the head of the token. 3331 opt_hd = wrt; 3332 3333 // Tokens are strings of non white space characters separated 3334 // by one or more white spaces. 3335 while (rd < buffer_end && !isspace(*rd)) { 3336 if (*rd == '\'' || *rd == '"') { // handle a quoted string 3337 int quote = *rd; // matching quote to look for 3338 rd++; // don't copy open quote 3339 while (rd < buffer_end && *rd != quote) { 3340 // include everything (even spaces) 3341 // up until the close quote 3342 *wrt++ = *rd++; // copy to option string 3343 } 3344 3345 if (rd < buffer_end) { 3346 rd++; // don't copy close quote 3347 } else { 3348 // did not see closing quote 3349 jio_fprintf(defaultStream::error_stream(), 3350 "Unmatched quote in %s\n", name); 3351 return JNI_ERR; 3352 } 3353 } else { 3354 *wrt++ = *rd++; // copy to option string 3355 } 3356 } 3357 3358 // steal a white space character and set it to null 3359 *wrt++ = '\0'; 3360 // We now have a complete token 3361 3362 JavaVMOption option; 3363 option.optionString = opt_hd; 3364 option.extraInfo = nullptr; 3365 3366 options.append(option); // Fill in option 3367 3368 rd++; // Advance to next character 3369 } 3370 3371 // Fill out JavaVMInitArgs structure. 3372 return vm_args->set_args(&options); 3373 } 3374 3375 void Arguments::set_shared_spaces_flags_and_archive_paths() { 3376 if (DumpSharedSpaces) { 3377 if (RequireSharedSpaces) { 3378 warning("Cannot dump shared archive while using shared archive"); 3379 } 3380 UseSharedSpaces = false; 3381 } 3382 #if INCLUDE_CDS 3383 // Initialize shared archive paths which could include both base and dynamic archive paths 3384 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly. 3385 // 3386 // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid. 3387 if (DumpSharedSpaces || UseSharedSpaces) { 3388 init_shared_archive_paths(); 3389 } 3390 #endif // INCLUDE_CDS 3391 } 3392 3393 #if INCLUDE_CDS 3394 // Sharing support 3395 // Construct the path to the archive 3396 char* Arguments::get_default_shared_archive_path() { 3397 if (_default_shared_archive_path == nullptr) { 3398 char jvm_path[JVM_MAXPATHLEN]; 3399 os::jvm_path(jvm_path, sizeof(jvm_path)); 3400 char *end = strrchr(jvm_path, *os::file_separator()); 3401 if (end != nullptr) *end = '\0'; 3402 size_t jvm_path_len = strlen(jvm_path); 3403 size_t file_sep_len = strlen(os::file_separator()); 3404 const size_t len = jvm_path_len + file_sep_len + 20; 3405 _default_shared_archive_path = NEW_C_HEAP_ARRAY(char, len, mtArguments); 3406 jio_snprintf(_default_shared_archive_path, len, 3407 LP64_ONLY(!UseCompressedOops ? "%s%sclasses_nocoops.jsa":) "%s%sclasses.jsa", 3408 jvm_path, os::file_separator()); 3409 } 3410 return _default_shared_archive_path; 3411 } 3412 3413 int Arguments::num_archives(const char* archive_path) { 3414 if (archive_path == nullptr) { 3415 return 0; 3416 } 3417 int npaths = 1; 3418 char* p = (char*)archive_path; 3419 while (*p != '\0') { 3420 if (*p == os::path_separator()[0]) { 3421 npaths++; 3422 } 3423 p++; 3424 } 3425 return npaths; 3426 } 3427 3428 void Arguments::extract_shared_archive_paths(const char* archive_path, 3429 char** base_archive_path, 3430 char** top_archive_path) { 3431 char* begin_ptr = (char*)archive_path; 3432 char* end_ptr = strchr((char*)archive_path, os::path_separator()[0]); 3433 if (end_ptr == nullptr || end_ptr == begin_ptr) { 3434 vm_exit_during_initialization("Base archive was not specified", archive_path); 3435 } 3436 size_t len = end_ptr - begin_ptr; 3437 char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 3438 strncpy(cur_path, begin_ptr, len); 3439 cur_path[len] = '\0'; 3440 *base_archive_path = cur_path; 3441 3442 begin_ptr = ++end_ptr; 3443 if (*begin_ptr == '\0') { 3444 vm_exit_during_initialization("Top archive was not specified", archive_path); 3445 } 3446 end_ptr = strchr(begin_ptr, '\0'); 3447 assert(end_ptr != nullptr, "sanity"); 3448 len = end_ptr - begin_ptr; 3449 cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); 3450 strncpy(cur_path, begin_ptr, len + 1); 3451 *top_archive_path = cur_path; 3452 } 3453 3454 void Arguments::init_shared_archive_paths() { 3455 if (ArchiveClassesAtExit != nullptr) { 3456 assert(!RecordDynamicDumpInfo, "already checked"); 3457 if (DumpSharedSpaces) { 3458 vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump"); 3459 } 3460 check_unsupported_dumping_properties(); 3461 3462 if (os::same_files(get_default_shared_archive_path(), ArchiveClassesAtExit)) { 3463 vm_exit_during_initialization( 3464 "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", get_default_shared_archive_path()); 3465 } 3466 } 3467 3468 if (SharedArchiveFile == nullptr) { 3469 SharedArchivePath = get_default_shared_archive_path(); 3470 } else { 3471 int archives = num_archives(SharedArchiveFile); 3472 assert(archives > 0, "must be"); 3473 3474 if (is_dumping_archive() && archives > 1) { 3475 vm_exit_during_initialization( 3476 "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping"); 3477 } 3478 3479 if (DumpSharedSpaces) { 3480 assert(archives == 1, "must be"); 3481 // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file 3482 // will be overwritten no matter regardless of its contents 3483 SharedArchivePath = os::strdup_check_oom(SharedArchiveFile, mtArguments); 3484 } else { 3485 // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa 3486 // is read from top.jsa 3487 // (a) 1 file: -XX:SharedArchiveFile=base.jsa 3488 // (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa 3489 // (c) 2 files: -XX:SharedArchiveFile=top.jsa 3490 // 3491 // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not 3492 // allow cases (b) and (c). Case (b) is already checked above. 3493 3494 if (archives > 2) { 3495 vm_exit_during_initialization( 3496 "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option"); 3497 } 3498 if (archives == 1) { 3499 char* base_archive_path = nullptr; 3500 bool success = 3501 FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path); 3502 if (!success) { 3503 // If +AutoCreateSharedArchive and the specified shared archive does not exist, 3504 // regenerate the dynamic archive base on default archive. 3505 if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) { 3506 DynamicDumpSharedSpaces = true; 3507 ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile); 3508 SharedArchivePath = get_default_shared_archive_path(); 3509 SharedArchiveFile = nullptr; 3510 } else { 3511 if (AutoCreateSharedArchive) { 3512 warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."); 3513 AutoCreateSharedArchive = false; 3514 } 3515 no_shared_spaces("invalid archive"); 3516 } 3517 } else if (base_archive_path == nullptr) { 3518 // User has specified a single archive, which is a static archive. 3519 SharedArchivePath = const_cast<char *>(SharedArchiveFile); 3520 } else { 3521 // User has specified a single archive, which is a dynamic archive. 3522 SharedDynamicArchivePath = const_cast<char *>(SharedArchiveFile); 3523 SharedArchivePath = base_archive_path; // has been c-heap allocated. 3524 } 3525 } else { 3526 extract_shared_archive_paths((const char*)SharedArchiveFile, 3527 &SharedArchivePath, &SharedDynamicArchivePath); 3528 if (SharedArchivePath == nullptr) { 3529 assert(SharedDynamicArchivePath == nullptr, "must be"); 3530 no_shared_spaces("invalid archive"); 3531 } 3532 } 3533 3534 if (SharedDynamicArchivePath != nullptr) { 3535 // Check for case (c) 3536 if (RecordDynamicDumpInfo) { 3537 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 3538 SharedArchiveFile); 3539 } 3540 if (ArchiveClassesAtExit != nullptr) { 3541 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile", 3542 SharedArchiveFile); 3543 } 3544 } 3545 3546 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) { 3547 vm_exit_during_initialization( 3548 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit", 3549 SharedArchiveFile); 3550 } 3551 } 3552 } 3553 } 3554 #endif // INCLUDE_CDS 3555 3556 #ifndef PRODUCT 3557 // Determine whether LogVMOutput should be implicitly turned on. 3558 static bool use_vm_log() { 3559 if (LogCompilation || !FLAG_IS_DEFAULT(LogFile) || 3560 PrintCompilation || PrintInlining || PrintDependencies || PrintNativeNMethods || 3561 PrintDebugInfo || PrintRelocations || PrintNMethods || PrintExceptionHandlers || 3562 PrintAssembly || TraceDeoptimization || 3563 (VerifyDependencies && FLAG_IS_CMDLINE(VerifyDependencies))) { 3564 return true; 3565 } 3566 3567 #ifdef COMPILER1 3568 if (PrintC1Statistics) { 3569 return true; 3570 } 3571 #endif // COMPILER1 3572 3573 #ifdef COMPILER2 3574 if (PrintOptoAssembly || PrintOptoStatistics) { 3575 return true; 3576 } 3577 #endif // COMPILER2 3578 3579 return false; 3580 } 3581 3582 #endif // PRODUCT 3583 3584 bool Arguments::args_contains_vm_options_file_arg(const JavaVMInitArgs* args) { 3585 for (int index = 0; index < args->nOptions; index++) { 3586 const JavaVMOption* option = args->options + index; 3587 const char* tail; 3588 if (match_option(option, "-XX:VMOptionsFile=", &tail)) { 3589 return true; 3590 } 3591 } 3592 return false; 3593 } 3594 3595 jint Arguments::insert_vm_options_file(const JavaVMInitArgs* args, 3596 const char* vm_options_file, 3597 const int vm_options_file_pos, 3598 ScopedVMInitArgs* vm_options_file_args, 3599 ScopedVMInitArgs* args_out) { 3600 jint code = parse_vm_options_file(vm_options_file, vm_options_file_args); 3601 if (code != JNI_OK) { 3602 return code; 3603 } 3604 3605 if (vm_options_file_args->get()->nOptions < 1) { 3606 return JNI_OK; 3607 } 3608 3609 if (args_contains_vm_options_file_arg(vm_options_file_args->get())) { 3610 jio_fprintf(defaultStream::error_stream(), 3611 "A VM options file may not refer to a VM options file. " 3612 "Specification of '-XX:VMOptionsFile=<file-name>' in the " 3613 "options file '%s' in options container '%s' is an error.\n", 3614 vm_options_file_args->vm_options_file_arg(), 3615 vm_options_file_args->container_name()); 3616 return JNI_EINVAL; 3617 } 3618 3619 return args_out->insert(args, vm_options_file_args->get(), 3620 vm_options_file_pos); 3621 } 3622 3623 // Expand -XX:VMOptionsFile found in args_in as needed. 3624 // mod_args and args_out parameters may return values as needed. 3625 jint Arguments::expand_vm_options_as_needed(const JavaVMInitArgs* args_in, 3626 ScopedVMInitArgs* mod_args, 3627 JavaVMInitArgs** args_out) { 3628 jint code = match_special_option_and_act(args_in, mod_args); 3629 if (code != JNI_OK) { 3630 return code; 3631 } 3632 3633 if (mod_args->is_set()) { 3634 // args_in contains -XX:VMOptionsFile and mod_args contains the 3635 // original options from args_in along with the options expanded 3636 // from the VMOptionsFile. Return a short-hand to the caller. 3637 *args_out = mod_args->get(); 3638 } else { 3639 *args_out = (JavaVMInitArgs *)args_in; // no changes so use args_in 3640 } 3641 return JNI_OK; 3642 } 3643 3644 jint Arguments::match_special_option_and_act(const JavaVMInitArgs* args, 3645 ScopedVMInitArgs* args_out) { 3646 // Remaining part of option string 3647 const char* tail; 3648 ScopedVMInitArgs vm_options_file_args(args_out->container_name()); 3649 3650 for (int index = 0; index < args->nOptions; index++) { 3651 const JavaVMOption* option = args->options + index; 3652 if (match_option(option, "-XX:Flags=", &tail)) { 3653 Arguments::set_jvm_flags_file(tail); 3654 continue; 3655 } 3656 if (match_option(option, "-XX:VMOptionsFile=", &tail)) { 3657 if (vm_options_file_args.found_vm_options_file_arg()) { 3658 jio_fprintf(defaultStream::error_stream(), 3659 "The option '%s' is already specified in the options " 3660 "container '%s' so the specification of '%s' in the " 3661 "same options container is an error.\n", 3662 vm_options_file_args.vm_options_file_arg(), 3663 vm_options_file_args.container_name(), 3664 option->optionString); 3665 return JNI_EINVAL; 3666 } 3667 vm_options_file_args.set_vm_options_file_arg(option->optionString); 3668 // If there's a VMOptionsFile, parse that 3669 jint code = insert_vm_options_file(args, tail, index, 3670 &vm_options_file_args, args_out); 3671 if (code != JNI_OK) { 3672 return code; 3673 } 3674 args_out->set_vm_options_file_arg(vm_options_file_args.vm_options_file_arg()); 3675 if (args_out->is_set()) { 3676 // The VMOptions file inserted some options so switch 'args' 3677 // to the new set of options, and continue processing which 3678 // preserves "last option wins" semantics. 3679 args = args_out->get(); 3680 // The first option from the VMOptionsFile replaces the 3681 // current option. So we back track to process the 3682 // replacement option. 3683 index--; 3684 } 3685 continue; 3686 } 3687 if (match_option(option, "-XX:+PrintVMOptions")) { 3688 PrintVMOptions = true; 3689 continue; 3690 } 3691 if (match_option(option, "-XX:-PrintVMOptions")) { 3692 PrintVMOptions = false; 3693 continue; 3694 } 3695 if (match_option(option, "-XX:+IgnoreUnrecognizedVMOptions")) { 3696 IgnoreUnrecognizedVMOptions = true; 3697 continue; 3698 } 3699 if (match_option(option, "-XX:-IgnoreUnrecognizedVMOptions")) { 3700 IgnoreUnrecognizedVMOptions = false; 3701 continue; 3702 } 3703 if (match_option(option, "-XX:+PrintFlagsInitial")) { 3704 JVMFlag::printFlags(tty, false); 3705 vm_exit(0); 3706 } 3707 3708 #ifndef PRODUCT 3709 if (match_option(option, "-XX:+PrintFlagsWithComments")) { 3710 JVMFlag::printFlags(tty, true); 3711 vm_exit(0); 3712 } 3713 #endif 3714 } 3715 return JNI_OK; 3716 } 3717 3718 static void print_options(const JavaVMInitArgs *args) { 3719 const char* tail; 3720 for (int index = 0; index < args->nOptions; index++) { 3721 const JavaVMOption *option = args->options + index; 3722 if (match_option(option, "-XX:", &tail)) { 3723 logOption(tail); 3724 } 3725 } 3726 } 3727 3728 bool Arguments::handle_deprecated_print_gc_flags() { 3729 if (PrintGC) { 3730 log_warning(gc)("-XX:+PrintGC is deprecated. Will use -Xlog:gc instead."); 3731 } 3732 if (PrintGCDetails) { 3733 log_warning(gc)("-XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead."); 3734 } 3735 3736 if (_legacyGCLogging.lastFlag == 2) { 3737 // -Xloggc was used to specify a filename 3738 const char* gc_conf = PrintGCDetails ? "gc*" : "gc"; 3739 3740 LogTarget(Error, logging) target; 3741 LogStream errstream(target); 3742 return LogConfiguration::parse_log_arguments(_legacyGCLogging.file, gc_conf, nullptr, nullptr, &errstream); 3743 } else if (PrintGC || PrintGCDetails || (_legacyGCLogging.lastFlag == 1)) { 3744 LogConfiguration::configure_stdout(LogLevel::Info, !PrintGCDetails, LOG_TAGS(gc)); 3745 } 3746 return true; 3747 } 3748 3749 static void apply_debugger_ergo() { 3750 #ifndef PRODUCT 3751 // UseDebuggerErgo is notproduct 3752 if (ReplayCompiles) { 3753 FLAG_SET_ERGO_IF_DEFAULT(UseDebuggerErgo, true); 3754 } 3755 #endif 3756 3757 #ifndef PRODUCT 3758 if (UseDebuggerErgo) { 3759 // Turn on sub-flags 3760 FLAG_SET_ERGO_IF_DEFAULT(UseDebuggerErgo1, true); 3761 FLAG_SET_ERGO_IF_DEFAULT(UseDebuggerErgo2, true); 3762 } 3763 #endif 3764 3765 if (UseDebuggerErgo2) { 3766 // Debugging with limited number of CPUs 3767 FLAG_SET_ERGO_IF_DEFAULT(UseNUMA, false); 3768 FLAG_SET_ERGO_IF_DEFAULT(ConcGCThreads, 1); 3769 FLAG_SET_ERGO_IF_DEFAULT(ParallelGCThreads, 1); 3770 FLAG_SET_ERGO_IF_DEFAULT(CICompilerCount, 2); 3771 } 3772 } 3773 3774 // Parse entry point called from JNI_CreateJavaVM 3775 3776 jint Arguments::parse(const JavaVMInitArgs* initial_cmd_args) { 3777 assert(verify_special_jvm_flags(false), "deprecated and obsolete flag table inconsistent"); 3778 JVMFlag::check_all_flag_declarations(); 3779 3780 // If flag "-XX:Flags=flags-file" is used it will be the first option to be processed. 3781 const char* hotspotrc = ".hotspotrc"; 3782 bool settings_file_specified = false; 3783 bool needs_hotspotrc_warning = false; 3784 ScopedVMInitArgs initial_vm_options_args(""); 3785 ScopedVMInitArgs initial_java_tool_options_args("env_var='JAVA_TOOL_OPTIONS'"); 3786 ScopedVMInitArgs initial_java_options_args("env_var='_JAVA_OPTIONS'"); 3787 3788 // Pointers to current working set of containers 3789 JavaVMInitArgs* cur_cmd_args; 3790 JavaVMInitArgs* cur_vm_options_args; 3791 JavaVMInitArgs* cur_java_options_args; 3792 JavaVMInitArgs* cur_java_tool_options_args; 3793 3794 // Containers for modified/expanded options 3795 ScopedVMInitArgs mod_cmd_args("cmd_line_args"); 3796 ScopedVMInitArgs mod_vm_options_args("vm_options_args"); 3797 ScopedVMInitArgs mod_java_tool_options_args("env_var='JAVA_TOOL_OPTIONS'"); 3798 ScopedVMInitArgs mod_java_options_args("env_var='_JAVA_OPTIONS'"); 3799 3800 3801 jint code = 3802 parse_java_tool_options_environment_variable(&initial_java_tool_options_args); 3803 if (code != JNI_OK) { 3804 return code; 3805 } 3806 3807 code = parse_java_options_environment_variable(&initial_java_options_args); 3808 if (code != JNI_OK) { 3809 return code; 3810 } 3811 3812 // Parse the options in the /java.base/jdk/internal/vm/options resource, if present 3813 char *vmoptions = ClassLoader::lookup_vm_options(); 3814 if (vmoptions != nullptr) { 3815 code = parse_options_buffer("vm options resource", vmoptions, strlen(vmoptions), &initial_vm_options_args); 3816 FREE_C_HEAP_ARRAY(char, vmoptions); 3817 if (code != JNI_OK) { 3818 return code; 3819 } 3820 } 3821 3822 code = expand_vm_options_as_needed(initial_java_tool_options_args.get(), 3823 &mod_java_tool_options_args, 3824 &cur_java_tool_options_args); 3825 if (code != JNI_OK) { 3826 return code; 3827 } 3828 3829 code = expand_vm_options_as_needed(initial_cmd_args, 3830 &mod_cmd_args, 3831 &cur_cmd_args); 3832 if (code != JNI_OK) { 3833 return code; 3834 } 3835 3836 code = expand_vm_options_as_needed(initial_java_options_args.get(), 3837 &mod_java_options_args, 3838 &cur_java_options_args); 3839 if (code != JNI_OK) { 3840 return code; 3841 } 3842 3843 code = expand_vm_options_as_needed(initial_vm_options_args.get(), 3844 &mod_vm_options_args, 3845 &cur_vm_options_args); 3846 if (code != JNI_OK) { 3847 return code; 3848 } 3849 3850 const char* flags_file = Arguments::get_jvm_flags_file(); 3851 settings_file_specified = (flags_file != nullptr); 3852 3853 if (IgnoreUnrecognizedVMOptions) { 3854 cur_cmd_args->ignoreUnrecognized = true; 3855 cur_java_tool_options_args->ignoreUnrecognized = true; 3856 cur_java_options_args->ignoreUnrecognized = true; 3857 } 3858 3859 // Parse specified settings file 3860 if (settings_file_specified) { 3861 if (!process_settings_file(flags_file, true, 3862 cur_cmd_args->ignoreUnrecognized)) { 3863 return JNI_EINVAL; 3864 } 3865 } else { 3866 #ifdef ASSERT 3867 // Parse default .hotspotrc settings file 3868 if (!process_settings_file(".hotspotrc", false, 3869 cur_cmd_args->ignoreUnrecognized)) { 3870 return JNI_EINVAL; 3871 } 3872 #else 3873 struct stat buf; 3874 if (os::stat(hotspotrc, &buf) == 0) { 3875 needs_hotspotrc_warning = true; 3876 } 3877 #endif 3878 } 3879 3880 if (PrintVMOptions) { 3881 print_options(cur_java_tool_options_args); 3882 print_options(cur_cmd_args); 3883 print_options(cur_java_options_args); 3884 } 3885 3886 // Parse JavaVMInitArgs structure passed in, as well as JAVA_TOOL_OPTIONS and _JAVA_OPTIONS 3887 jint result = parse_vm_init_args(cur_vm_options_args, 3888 cur_java_tool_options_args, 3889 cur_java_options_args, 3890 cur_cmd_args); 3891 3892 if (result != JNI_OK) { 3893 return result; 3894 } 3895 3896 // Delay warning until here so that we've had a chance to process 3897 // the -XX:-PrintWarnings flag 3898 if (needs_hotspotrc_warning) { 3899 warning("%s file is present but has been ignored. " 3900 "Run with -XX:Flags=%s to load the file.", 3901 hotspotrc, hotspotrc); 3902 } 3903 3904 if (needs_module_property_warning) { 3905 warning("Ignoring system property options whose names match the '-Djdk.module.*'." 3906 " names that are reserved for internal use."); 3907 } 3908 3909 #if defined(_ALLBSD_SOURCE) || defined(AIX) // UseLargePages is not yet supported on BSD and AIX. 3910 UNSUPPORTED_OPTION(UseLargePages); 3911 #endif 3912 3913 #if defined(AIX) 3914 UNSUPPORTED_OPTION_NULL(AllocateHeapAt); 3915 #endif 3916 3917 #ifndef PRODUCT 3918 if (TraceBytecodesAt != 0) { 3919 TraceBytecodes = true; 3920 } 3921 if (CountCompiledCalls) { 3922 if (UseCounterDecay) { 3923 warning("UseCounterDecay disabled because CountCalls is set"); 3924 UseCounterDecay = false; 3925 } 3926 } 3927 #endif // PRODUCT 3928 3929 if (ScavengeRootsInCode == 0) { 3930 if (!FLAG_IS_DEFAULT(ScavengeRootsInCode)) { 3931 warning("Forcing ScavengeRootsInCode non-zero"); 3932 } 3933 ScavengeRootsInCode = 1; 3934 } 3935 3936 if (!handle_deprecated_print_gc_flags()) { 3937 return JNI_EINVAL; 3938 } 3939 3940 // Set object alignment values. 3941 set_object_alignment(); 3942 3943 #if !INCLUDE_CDS 3944 if (DumpSharedSpaces || RequireSharedSpaces) { 3945 jio_fprintf(defaultStream::error_stream(), 3946 "Shared spaces are not supported in this VM\n"); 3947 return JNI_ERR; 3948 } 3949 if (DumpLoadedClassList != nullptr) { 3950 jio_fprintf(defaultStream::error_stream(), 3951 "DumpLoadedClassList is not supported in this VM\n"); 3952 return JNI_ERR; 3953 } 3954 if ((UseSharedSpaces && xshare_auto_cmd_line) || 3955 log_is_enabled(Info, cds)) { 3956 warning("Shared spaces are not supported in this VM"); 3957 UseSharedSpaces = false; 3958 LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(cds)); 3959 } 3960 no_shared_spaces("CDS Disabled"); 3961 #endif // INCLUDE_CDS 3962 3963 // Verify NMT arguments 3964 const NMT_TrackingLevel lvl = NMTUtil::parse_tracking_level(NativeMemoryTracking); 3965 if (lvl == NMT_unknown) { 3966 jio_fprintf(defaultStream::error_stream(), 3967 "Syntax error, expecting -XX:NativeMemoryTracking=[off|summary|detail]\n"); 3968 return JNI_ERR; 3969 } 3970 if (PrintNMTStatistics && lvl == NMT_off) { 3971 warning("PrintNMTStatistics is disabled, because native memory tracking is not enabled"); 3972 FLAG_SET_DEFAULT(PrintNMTStatistics, false); 3973 } 3974 3975 bool trace_dependencies = log_is_enabled(Debug, dependencies); 3976 if (trace_dependencies && VerifyDependencies) { 3977 warning("dependency logging results may be inflated by VerifyDependencies"); 3978 } 3979 3980 bool log_class_load_cause = log_is_enabled(Info, class, load, cause, native) || 3981 log_is_enabled(Info, class, load, cause); 3982 if (log_class_load_cause && LogClassLoadingCauseFor == nullptr) { 3983 warning("class load cause logging will not produce output without LogClassLoadingCauseFor"); 3984 } 3985 3986 apply_debugger_ergo(); 3987 3988 if (log_is_enabled(Info, arguments)) { 3989 LogStream st(Log(arguments)::info()); 3990 Arguments::print_on(&st); 3991 } 3992 3993 return JNI_OK; 3994 } 3995 3996 jint Arguments::apply_ergo() { 3997 // Set flags based on ergonomics. 3998 jint result = set_ergonomics_flags(); 3999 if (result != JNI_OK) return result; 4000 4001 // Set heap size based on available physical memory 4002 set_heap_size(); 4003 4004 GCConfig::arguments()->initialize(); 4005 4006 set_shared_spaces_flags_and_archive_paths(); 4007 4008 // Initialize Metaspace flags and alignments 4009 Metaspace::ergo_initialize(); 4010 4011 if (!StringDedup::ergo_initialize()) { 4012 return JNI_EINVAL; 4013 } 4014 4015 // Set compiler flags after GC is selected and GC specific 4016 // flags (LoopStripMiningIter) are set. 4017 CompilerConfig::ergo_initialize(); 4018 4019 // Set bytecode rewriting flags 4020 set_bytecode_flags(); 4021 4022 // Set flags if aggressive optimization flags are enabled 4023 jint code = set_aggressive_opts_flags(); 4024 if (code != JNI_OK) { 4025 return code; 4026 } 4027 4028 #ifdef ZERO 4029 // Clear flags not supported on zero. 4030 FLAG_SET_DEFAULT(ProfileInterpreter, false); 4031 #endif // ZERO 4032 4033 if (PrintAssembly && FLAG_IS_DEFAULT(DebugNonSafepoints)) { 4034 warning("PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output"); 4035 DebugNonSafepoints = true; 4036 } 4037 4038 if (FLAG_IS_CMDLINE(CompressedClassSpaceSize) && !UseCompressedClassPointers) { 4039 warning("Setting CompressedClassSpaceSize has no effect when compressed class pointers are not used"); 4040 } 4041 4042 // Treat the odd case where local verification is enabled but remote 4043 // verification is not as if both were enabled. 4044 if (BytecodeVerificationLocal && !BytecodeVerificationRemote) { 4045 log_info(verification)("Turning on remote verification because local verification is on"); 4046 FLAG_SET_DEFAULT(BytecodeVerificationRemote, true); 4047 } 4048 4049 #ifndef PRODUCT 4050 if (!LogVMOutput && FLAG_IS_DEFAULT(LogVMOutput)) { 4051 if (use_vm_log()) { 4052 LogVMOutput = true; 4053 } 4054 } 4055 #endif // PRODUCT 4056 4057 if (PrintCommandLineFlags) { 4058 JVMFlag::printSetFlags(tty); 4059 } 4060 4061 #if COMPILER2_OR_JVMCI 4062 if (!FLAG_IS_DEFAULT(EnableVectorSupport) && !EnableVectorSupport) { 4063 if (!FLAG_IS_DEFAULT(EnableVectorReboxing) && EnableVectorReboxing) { 4064 warning("Disabling EnableVectorReboxing since EnableVectorSupport is turned off."); 4065 } 4066 FLAG_SET_DEFAULT(EnableVectorReboxing, false); 4067 4068 if (!FLAG_IS_DEFAULT(EnableVectorAggressiveReboxing) && EnableVectorAggressiveReboxing) { 4069 if (!EnableVectorReboxing) { 4070 warning("Disabling EnableVectorAggressiveReboxing since EnableVectorReboxing is turned off."); 4071 } else { 4072 warning("Disabling EnableVectorAggressiveReboxing since EnableVectorSupport is turned off."); 4073 } 4074 } 4075 FLAG_SET_DEFAULT(EnableVectorAggressiveReboxing, false); 4076 4077 if (!FLAG_IS_DEFAULT(UseVectorStubs) && UseVectorStubs) { 4078 warning("Disabling UseVectorStubs since EnableVectorSupport is turned off."); 4079 } 4080 FLAG_SET_DEFAULT(UseVectorStubs, false); 4081 } 4082 #endif // COMPILER2_OR_JVMCI 4083 4084 if (FLAG_IS_CMDLINE(DiagnoseSyncOnValueBasedClasses)) { 4085 if (DiagnoseSyncOnValueBasedClasses == ObjectSynchronizer::LOG_WARNING && !log_is_enabled(Info, valuebasedclasses)) { 4086 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(valuebasedclasses)); 4087 } 4088 } 4089 return JNI_OK; 4090 } 4091 4092 jint Arguments::adjust_after_os() { 4093 if (UseNUMA) { 4094 if (UseParallelGC) { 4095 if (FLAG_IS_DEFAULT(MinHeapDeltaBytes)) { 4096 FLAG_SET_DEFAULT(MinHeapDeltaBytes, 64*M); 4097 } 4098 } 4099 } 4100 return JNI_OK; 4101 } 4102 4103 int Arguments::PropertyList_count(SystemProperty* pl) { 4104 int count = 0; 4105 while(pl != nullptr) { 4106 count++; 4107 pl = pl->next(); 4108 } 4109 return count; 4110 } 4111 4112 // Return the number of readable properties. 4113 int Arguments::PropertyList_readable_count(SystemProperty* pl) { 4114 int count = 0; 4115 while(pl != nullptr) { 4116 if (pl->readable()) { 4117 count++; 4118 } 4119 pl = pl->next(); 4120 } 4121 return count; 4122 } 4123 4124 const char* Arguments::PropertyList_get_value(SystemProperty *pl, const char* key) { 4125 assert(key != nullptr, "just checking"); 4126 SystemProperty* prop; 4127 for (prop = pl; prop != nullptr; prop = prop->next()) { 4128 if (strcmp(key, prop->key()) == 0) return prop->value(); 4129 } 4130 return nullptr; 4131 } 4132 4133 // Return the value of the requested property provided that it is a readable property. 4134 const char* Arguments::PropertyList_get_readable_value(SystemProperty *pl, const char* key) { 4135 assert(key != nullptr, "just checking"); 4136 SystemProperty* prop; 4137 // Return the property value if the keys match and the property is not internal or 4138 // it's the special internal property "jdk.boot.class.path.append". 4139 for (prop = pl; prop != nullptr; prop = prop->next()) { 4140 if (strcmp(key, prop->key()) == 0) { 4141 if (!prop->internal()) { 4142 return prop->value(); 4143 } else if (strcmp(key, "jdk.boot.class.path.append") == 0) { 4144 return prop->value(); 4145 } else { 4146 // Property is internal and not jdk.boot.class.path.append so return null. 4147 return nullptr; 4148 } 4149 } 4150 } 4151 return nullptr; 4152 } 4153 4154 void Arguments::PropertyList_add(SystemProperty** plist, SystemProperty *new_p) { 4155 SystemProperty* p = *plist; 4156 if (p == nullptr) { 4157 *plist = new_p; 4158 } else { 4159 while (p->next() != nullptr) { 4160 p = p->next(); 4161 } 4162 p->set_next(new_p); 4163 } 4164 } 4165 4166 void Arguments::PropertyList_add(SystemProperty** plist, const char* k, const char* v, 4167 bool writeable, bool internal) { 4168 if (plist == nullptr) 4169 return; 4170 4171 SystemProperty* new_p = new SystemProperty(k, v, writeable, internal); 4172 PropertyList_add(plist, new_p); 4173 } 4174 4175 void Arguments::PropertyList_add(SystemProperty *element) { 4176 PropertyList_add(&_system_properties, element); 4177 } 4178 4179 // This add maintains unique property key in the list. 4180 void Arguments::PropertyList_unique_add(SystemProperty** plist, const char* k, const char* v, 4181 PropertyAppendable append, PropertyWriteable writeable, 4182 PropertyInternal internal) { 4183 if (plist == nullptr) 4184 return; 4185 4186 // If property key exists and is writeable, then update with new value. 4187 // Trying to update a non-writeable property is silently ignored. 4188 SystemProperty* prop; 4189 for (prop = *plist; prop != nullptr; prop = prop->next()) { 4190 if (strcmp(k, prop->key()) == 0) { 4191 if (append == AppendProperty) { 4192 prop->append_writeable_value(v); 4193 } else { 4194 prop->set_writeable_value(v); 4195 } 4196 return; 4197 } 4198 } 4199 4200 PropertyList_add(plist, k, v, writeable == WriteableProperty, internal == InternalProperty); 4201 } 4202 4203 // Copies src into buf, replacing "%%" with "%" and "%p" with pid 4204 // Returns true if all of the source pointed by src has been copied over to 4205 // the destination buffer pointed by buf. Otherwise, returns false. 4206 // Notes: 4207 // 1. If the length (buflen) of the destination buffer excluding the 4208 // null terminator character is not long enough for holding the expanded 4209 // pid characters, it also returns false instead of returning the partially 4210 // expanded one. 4211 // 2. The passed in "buflen" should be large enough to hold the null terminator. 4212 bool Arguments::copy_expand_pid(const char* src, size_t srclen, 4213 char* buf, size_t buflen) { 4214 const char* p = src; 4215 char* b = buf; 4216 const char* src_end = &src[srclen]; 4217 char* buf_end = &buf[buflen - 1]; 4218 4219 while (p < src_end && b < buf_end) { 4220 if (*p == '%') { 4221 switch (*(++p)) { 4222 case '%': // "%%" ==> "%" 4223 *b++ = *p++; 4224 break; 4225 case 'p': { // "%p" ==> current process id 4226 // buf_end points to the character before the last character so 4227 // that we could write '\0' to the end of the buffer. 4228 size_t buf_sz = buf_end - b + 1; 4229 int ret = jio_snprintf(b, buf_sz, "%d", os::current_process_id()); 4230 4231 // if jio_snprintf fails or the buffer is not long enough to hold 4232 // the expanded pid, returns false. 4233 if (ret < 0 || ret >= (int)buf_sz) { 4234 return false; 4235 } else { 4236 b += ret; 4237 assert(*b == '\0', "fail in copy_expand_pid"); 4238 if (p == src_end && b == buf_end + 1) { 4239 // reach the end of the buffer. 4240 return true; 4241 } 4242 } 4243 p++; 4244 break; 4245 } 4246 default : 4247 *b++ = '%'; 4248 } 4249 } else { 4250 *b++ = *p++; 4251 } 4252 } 4253 *b = '\0'; 4254 return (p == src_end); // return false if not all of the source was copied 4255 }