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