1 /* 2 * Copyright (c) 1997, 2019, 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 #ifndef SHARE_VM_RUNTIME_ARGUMENTS_HPP 26 #define SHARE_VM_RUNTIME_ARGUMENTS_HPP 27 28 #include "runtime/java.hpp" 29 #include "runtime/perfData.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/top.hpp" 32 33 // Arguments parses the command line and recognizes options 34 35 // Invocation API hook typedefs (these should really be defined in jni.hpp) 36 extern "C" { 37 typedef void (JNICALL *abort_hook_t)(void); 38 typedef void (JNICALL *exit_hook_t)(jint code); 39 typedef jint (JNICALL *vfprintf_hook_t)(FILE *fp, const char *format, va_list args) ATTRIBUTE_PRINTF(2, 0); 40 } 41 42 // Forward declarations 43 44 class SysClassPath; 45 46 // Element describing System and User (-Dkey=value flags) defined property. 47 48 class SystemProperty: public CHeapObj<mtInternal> { 49 private: 50 char* _key; 51 char* _value; 52 SystemProperty* _next; 53 bool _internal; 54 55 bool _writeable; 56 bool writeable() { return _writeable; } 57 58 public: 59 // Accessors 60 const char* key() const { return _key; } 61 bool internal() const { return _internal; } 62 char* value() const { return _value; } 63 SystemProperty* next() const { return _next; } 64 void set_next(SystemProperty* next) { _next = next; } 65 bool set_value(char *value) { 66 if (writeable()) { 67 if (_value != NULL) { 68 FreeHeap(_value); 69 } 70 _value = AllocateHeap(strlen(value)+1, mtInternal); 71 if (_value != NULL) { 72 strcpy(_value, value); 73 } 74 return true; 75 } 76 return false; 77 } 78 79 void append_value(const char *value) { 80 char *sp; 81 size_t len = 0; 82 if (value != NULL) { 83 len = strlen(value); 84 if (_value != NULL) { 85 len += strlen(_value); 86 } 87 sp = AllocateHeap(len+2, mtInternal); 88 if (sp != NULL) { 89 if (_value != NULL) { 90 strcpy(sp, _value); 91 strcat(sp, os::path_separator()); 92 strcat(sp, value); 93 FreeHeap(_value); 94 } else { 95 strcpy(sp, value); 96 } 97 _value = sp; 98 } 99 } 100 } 101 102 // Constructor 103 SystemProperty(const char* key, const char* value, bool writeable, bool internal = false) { 104 if (key == NULL) { 105 _key = NULL; 106 } else { 107 _key = AllocateHeap(strlen(key)+1, mtInternal); 108 strcpy(_key, key); 109 } 110 if (value == NULL) { 111 _value = NULL; 112 } else { 113 _value = AllocateHeap(strlen(value)+1, mtInternal); 114 strcpy(_value, value); 115 } 116 _next = NULL; 117 _writeable = writeable; 118 _internal = internal; 119 } 120 }; 121 122 123 // For use by -agentlib, -agentpath and -Xrun 124 class AgentLibrary : public CHeapObj<mtInternal> { 125 friend class AgentLibraryList; 126 public: 127 // Is this library valid or not. Don't rely on os_lib == NULL as statically 128 // linked lib could have handle of RTLD_DEFAULT which == 0 on some platforms 129 enum AgentState { 130 agent_invalid = 0, 131 agent_valid = 1 132 }; 133 134 private: 135 char* _name; 136 char* _options; 137 void* _os_lib; 138 bool _is_absolute_path; 139 bool _is_static_lib; 140 AgentState _state; 141 AgentLibrary* _next; 142 143 public: 144 // Accessors 145 const char* name() const { return _name; } 146 char* options() const { return _options; } 147 bool is_absolute_path() const { return _is_absolute_path; } 148 void* os_lib() const { return _os_lib; } 149 void set_os_lib(void* os_lib) { _os_lib = os_lib; } 150 AgentLibrary* next() const { return _next; } 151 bool is_static_lib() const { return _is_static_lib; } 152 void set_static_lib(bool is_static_lib) { _is_static_lib = is_static_lib; } 153 bool valid() { return (_state == agent_valid); } 154 void set_valid() { _state = agent_valid; } 155 void set_invalid() { _state = agent_invalid; } 156 157 // Constructor 158 AgentLibrary(const char* name, const char* options, bool is_absolute_path, void* os_lib) { 159 _name = AllocateHeap(strlen(name)+1, mtInternal); 160 strcpy(_name, name); 161 if (options == NULL) { 162 _options = NULL; 163 } else { 164 _options = AllocateHeap(strlen(options)+1, mtInternal); 165 strcpy(_options, options); 166 } 167 _is_absolute_path = is_absolute_path; 168 _os_lib = os_lib; 169 _next = NULL; 170 _state = agent_invalid; 171 _is_static_lib = false; 172 } 173 }; 174 175 // maintain an order of entry list of AgentLibrary 176 class AgentLibraryList VALUE_OBJ_CLASS_SPEC { 177 private: 178 AgentLibrary* _first; 179 AgentLibrary* _last; 180 public: 181 bool is_empty() const { return _first == NULL; } 182 AgentLibrary* first() const { return _first; } 183 184 // add to the end of the list 185 void add(AgentLibrary* lib) { 186 if (is_empty()) { 187 _first = _last = lib; 188 } else { 189 _last->_next = lib; 190 _last = lib; 191 } 192 lib->_next = NULL; 193 } 194 195 // search for and remove a library known to be in the list 196 void remove(AgentLibrary* lib) { 197 AgentLibrary* curr; 198 AgentLibrary* prev = NULL; 199 for (curr = first(); curr != NULL; prev = curr, curr = curr->next()) { 200 if (curr == lib) { 201 break; 202 } 203 } 204 assert(curr != NULL, "always should be found"); 205 206 if (curr != NULL) { 207 // it was found, by-pass this library 208 if (prev == NULL) { 209 _first = curr->_next; 210 } else { 211 prev->_next = curr->_next; 212 } 213 if (curr == _last) { 214 _last = prev; 215 } 216 curr->_next = NULL; 217 } 218 } 219 220 AgentLibraryList() { 221 _first = NULL; 222 _last = NULL; 223 } 224 }; 225 226 227 class Arguments : AllStatic { 228 friend class VMStructs; 229 friend class JvmtiExport; 230 public: 231 // Operation modi 232 enum Mode { 233 _int, // corresponds to -Xint 234 _mixed, // corresponds to -Xmixed 235 _comp // corresponds to -Xcomp 236 }; 237 238 enum ArgsRange { 239 arg_unreadable = -3, 240 arg_too_small = -2, 241 arg_too_big = -1, 242 arg_in_range = 0 243 }; 244 245 private: 246 247 // an array containing all flags specified in the .hotspotrc file 248 static char** _jvm_flags_array; 249 static int _num_jvm_flags; 250 // an array containing all jvm arguments specified in the command line 251 static char** _jvm_args_array; 252 static int _num_jvm_args; 253 // string containing all java command (class/jarfile name and app args) 254 static char* _java_command; 255 256 // Property list 257 static SystemProperty* _system_properties; 258 259 // Quick accessor to System properties in the list: 260 static SystemProperty *_java_ext_dirs; 261 static SystemProperty *_java_endorsed_dirs; 262 static SystemProperty *_sun_boot_library_path; 263 static SystemProperty *_java_library_path; 264 static SystemProperty *_java_home; 265 static SystemProperty *_java_class_path; 266 static SystemProperty *_sun_boot_class_path; 267 268 // Meta-index for knowing what packages are in the boot class path 269 static char* _meta_index_path; 270 static char* _meta_index_dir; 271 272 // java.vendor.url.bug, bug reporting URL for fatal errors. 273 static const char* _java_vendor_url_bug; 274 275 // sun.java.launcher, private property to provide information about 276 // java/gamma launcher 277 static const char* _sun_java_launcher; 278 279 // sun.java.launcher.pid, private property 280 static int _sun_java_launcher_pid; 281 282 // was this VM created by the gamma launcher 283 static bool _created_by_gamma_launcher; 284 285 // Option flags 286 static bool _has_profile; 287 static const char* _gc_log_filename; 288 // Value of the conservative maximum heap alignment needed 289 static size_t _conservative_max_heap_alignment; 290 291 static uintx _min_heap_size; 292 293 // Used to store original flag values 294 static uintx _min_heap_free_ratio; 295 static uintx _max_heap_free_ratio; 296 297 // -Xrun arguments 298 static AgentLibraryList _libraryList; 299 static void add_init_library(const char* name, char* options) 300 { _libraryList.add(new AgentLibrary(name, options, false, NULL)); } 301 302 // -agentlib and -agentpath arguments 303 static AgentLibraryList _agentList; 304 static void add_init_agent(const char* name, char* options, bool absolute_path) 305 { _agentList.add(new AgentLibrary(name, options, absolute_path, NULL)); } 306 307 // Late-binding agents not started via arguments 308 static void add_loaded_agent(AgentLibrary *agentLib) 309 { _agentList.add(agentLib); } 310 static void add_loaded_agent(const char* name, char* options, bool absolute_path, void* os_lib) 311 { _agentList.add(new AgentLibrary(name, options, absolute_path, os_lib)); } 312 313 // Operation modi 314 static Mode _mode; 315 static void set_mode_flags(Mode mode); 316 static bool _java_compiler; 317 static void set_java_compiler(bool arg) { _java_compiler = arg; } 318 static bool java_compiler() { return _java_compiler; } 319 320 // -Xdebug flag 321 static bool _xdebug_mode; 322 static void set_xdebug_mode(bool arg) { _xdebug_mode = arg; } 323 static bool xdebug_mode() { return _xdebug_mode; } 324 325 // Used to save default settings 326 static bool _AlwaysCompileLoopMethods; 327 static bool _UseOnStackReplacement; 328 static bool _BackgroundCompilation; 329 static bool _ClipInlining; 330 static bool _CIDynamicCompilePriority; 331 332 // Tiered 333 static void set_tiered_flags(); 334 static int get_min_number_of_compiler_threads(); 335 // CMS/ParNew garbage collectors 336 static void set_parnew_gc_flags(); 337 static void set_cms_and_parnew_gc_flags(); 338 // UseParallel[Old]GC 339 static void set_parallel_gc_flags(); 340 // Garbage-First (UseG1GC) 341 static void set_g1_gc_flags(); 342 // GC ergonomics 343 static void set_conservative_max_heap_alignment(); 344 static void set_use_compressed_oops(); 345 static void set_use_compressed_klass_ptrs(); 346 static void select_gc(); 347 static void set_ergonomics_flags(); 348 static void set_shared_spaces_flags(); 349 // limits the given memory size by the maximum amount of memory this process is 350 // currently allowed to allocate or reserve. 351 static julong limit_by_allocatable_memory(julong size); 352 // Setup heap size 353 static void set_heap_size(); 354 // Based on automatic selection criteria, should the 355 // low pause collector be used. 356 static bool should_auto_select_low_pause_collector(); 357 358 // Bytecode rewriting 359 static void set_bytecode_flags(); 360 361 // Invocation API hooks 362 static abort_hook_t _abort_hook; 363 static exit_hook_t _exit_hook; 364 static vfprintf_hook_t _vfprintf_hook; 365 366 // System properties 367 static bool add_property(const char* prop); 368 369 // Aggressive optimization flags. 370 static void set_aggressive_opts_flags(); 371 372 static jint set_aggressive_heap_flags(); 373 374 // Argument parsing 375 static void do_pd_flag_adjustments(); 376 static bool parse_argument(const char* arg, Flag::Flags origin); 377 static bool process_argument(const char* arg, jboolean ignore_unrecognized, Flag::Flags origin); 378 static void process_java_launcher_argument(const char*, void*); 379 static void process_java_compiler_argument(char* arg); 380 static jint parse_options_environment_variable(const char* name, SysClassPath* scp_p, bool* scp_assembly_required_p); 381 static jint parse_java_tool_options_environment_variable(SysClassPath* scp_p, bool* scp_assembly_required_p); 382 static jint parse_java_options_environment_variable(SysClassPath* scp_p, bool* scp_assembly_required_p); 383 static jint parse_vm_init_args(const JavaVMInitArgs* args); 384 static jint parse_each_vm_init_arg(const JavaVMInitArgs* args, SysClassPath* scp_p, bool* scp_assembly_required_p, Flag::Flags origin); 385 static jint finalize_vm_init_args(SysClassPath* scp_p, bool scp_assembly_required); 386 static bool is_bad_option(const JavaVMOption* option, jboolean ignore, const char* option_type); 387 388 static bool is_bad_option(const JavaVMOption* option, jboolean ignore) { 389 return is_bad_option(option, ignore, NULL); 390 } 391 392 static bool is_percentage(uintx val) { 393 return val <= 100; 394 } 395 396 static bool verify_interval(uintx val, uintx min, 397 uintx max, const char* name); 398 static bool verify_min_value(intx val, intx min, const char* name); 399 static bool verify_percentage(uintx value, const char* name); 400 static void describe_range_error(ArgsRange errcode); 401 static ArgsRange check_memory_size(julong size, julong min_size); 402 static ArgsRange parse_memory_size(const char* s, julong* long_arg, 403 julong min_size); 404 // Parse a string for a unsigned integer. Returns true if value 405 // is an unsigned integer greater than or equal to the minimum 406 // parameter passed and returns the value in uintx_arg. Returns 407 // false otherwise, with uintx_arg undefined. 408 static bool parse_uintx(const char* value, uintx* uintx_arg, 409 uintx min_size); 410 411 // methods to build strings from individual args 412 static void build_jvm_args(const char* arg); 413 static void build_jvm_flags(const char* arg); 414 static void add_string(char*** bldarray, int* count, const char* arg); 415 static const char* build_resource_string(char** args, int count); 416 417 static bool methodExists( 418 char* className, char* methodName, 419 int classesNum, char** classes, bool* allMethods, 420 int methodsNum, char** methods, bool* allClasses 421 ); 422 423 static void parseOnlyLine( 424 const char* line, 425 short* classesNum, short* classesMax, char*** classes, bool** allMethods, 426 short* methodsNum, short* methodsMax, char*** methods, bool** allClasses 427 ); 428 429 // Returns true if the string s is in the list of flags that have recently 430 // been made obsolete. If we detect one of these flags on the command 431 // line, instead of failing we print a warning message and ignore the 432 // flag. This gives the user a release or so to stop using the flag. 433 static bool is_newly_obsolete(const char* s, JDK_Version* buffer); 434 435 static short CompileOnlyClassesNum; 436 static short CompileOnlyClassesMax; 437 static char** CompileOnlyClasses; 438 static bool* CompileOnlyAllMethods; 439 440 static short CompileOnlyMethodsNum; 441 static short CompileOnlyMethodsMax; 442 static char** CompileOnlyMethods; 443 static bool* CompileOnlyAllClasses; 444 445 static short InterpretOnlyClassesNum; 446 static short InterpretOnlyClassesMax; 447 static char** InterpretOnlyClasses; 448 static bool* InterpretOnlyAllMethods; 449 450 static bool CheckCompileOnly; 451 452 static char* SharedArchivePath; 453 454 public: 455 // Parses the arguments, first phase 456 static jint parse(const JavaVMInitArgs* args); 457 // Apply ergonomics 458 static jint apply_ergo(); 459 // Adjusts the arguments after the OS have adjusted the arguments 460 static jint adjust_after_os(); 461 462 static void set_gc_specific_flags(); 463 static inline bool gc_selected(); // whether a gc has been selected 464 static void select_gc_ergonomically(); 465 466 // Verifies that the given value will fit as a MinHeapFreeRatio. If not, an error 467 // message is returned in the provided buffer. 468 static bool verify_MinHeapFreeRatio(FormatBuffer<80>& err_msg, uintx min_heap_free_ratio); 469 470 // Verifies that the given value will fit as a MaxHeapFreeRatio. If not, an error 471 // message is returned in the provided buffer. 472 static bool verify_MaxHeapFreeRatio(FormatBuffer<80>& err_msg, uintx max_heap_free_ratio); 473 474 // Check for consistency in the selection of the garbage collector. 475 static bool check_gc_consistency(); // Check user-selected gc 476 static void check_deprecated_gcs(); 477 static void check_deprecated_gc_flags(); 478 // Check consistecy or otherwise of VM argument settings 479 static bool check_vm_args_consistency(); 480 // Check stack pages settings 481 static bool check_stack_pages(); 482 // Used by os_solaris 483 static bool process_settings_file(const char* file_name, bool should_exist, jboolean ignore_unrecognized); 484 485 static size_t conservative_max_heap_alignment() { return _conservative_max_heap_alignment; } 486 // Return the maximum size a heap with compressed oops can take 487 static size_t max_heap_for_compressed_oops(); 488 489 // return a char* array containing all options 490 static char** jvm_flags_array() { return _jvm_flags_array; } 491 static char** jvm_args_array() { return _jvm_args_array; } 492 static int num_jvm_flags() { return _num_jvm_flags; } 493 static int num_jvm_args() { return _num_jvm_args; } 494 // return the arguments passed to the Java application 495 static const char* java_command() { return _java_command; } 496 497 // print jvm_flags, jvm_args and java_command 498 static void print_on(outputStream* st); 499 500 // convenient methods to obtain / print jvm_flags and jvm_args 501 static const char* jvm_flags() { return build_resource_string(_jvm_flags_array, _num_jvm_flags); } 502 static const char* jvm_args() { return build_resource_string(_jvm_args_array, _num_jvm_args); } 503 static void print_jvm_flags_on(outputStream* st); 504 static void print_jvm_args_on(outputStream* st); 505 506 // -Dkey=value flags 507 static SystemProperty* system_properties() { return _system_properties; } 508 static const char* get_property(const char* key); 509 510 // -Djava.vendor.url.bug 511 static const char* java_vendor_url_bug() { return _java_vendor_url_bug; } 512 513 // -Dsun.java.launcher 514 static const char* sun_java_launcher() { return _sun_java_launcher; } 515 // Was VM created by a Java launcher? 516 static bool created_by_java_launcher(); 517 // Was VM created by the gamma Java launcher? 518 static bool created_by_gamma_launcher(); 519 // -Dsun.java.launcher.pid 520 static int sun_java_launcher_pid() { return _sun_java_launcher_pid; } 521 522 // -Xloggc:<file>, if not specified will be NULL 523 static const char* gc_log_filename() { return _gc_log_filename; } 524 525 // -Xprof 526 static bool has_profile() { return _has_profile; } 527 528 // -Xms, -Xmx 529 static uintx min_heap_size() { return _min_heap_size; } 530 static void set_min_heap_size(uintx v) { _min_heap_size = v; } 531 532 // Returns the original values of -XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio 533 static uintx min_heap_free_ratio() { return _min_heap_free_ratio; } 534 static uintx max_heap_free_ratio() { return _max_heap_free_ratio; } 535 536 // -Xrun 537 static AgentLibrary* libraries() { return _libraryList.first(); } 538 static bool init_libraries_at_startup() { return !_libraryList.is_empty(); } 539 static void convert_library_to_agent(AgentLibrary* lib) 540 { _libraryList.remove(lib); 541 _agentList.add(lib); } 542 543 // -agentlib -agentpath 544 static AgentLibrary* agents() { return _agentList.first(); } 545 static bool init_agents_at_startup() { return !_agentList.is_empty(); } 546 547 // abort, exit, vfprintf hooks 548 static abort_hook_t abort_hook() { return _abort_hook; } 549 static exit_hook_t exit_hook() { return _exit_hook; } 550 static vfprintf_hook_t vfprintf_hook() { return _vfprintf_hook; } 551 552 static bool GetCheckCompileOnly () { return CheckCompileOnly; } 553 554 static const char* GetSharedArchivePath() { return SharedArchivePath; } 555 556 static bool CompileMethod(char* className, char* methodName) { 557 return 558 methodExists( 559 className, methodName, 560 CompileOnlyClassesNum, CompileOnlyClasses, CompileOnlyAllMethods, 561 CompileOnlyMethodsNum, CompileOnlyMethods, CompileOnlyAllClasses 562 ); 563 } 564 565 // Java launcher properties 566 static void process_sun_java_launcher_properties(JavaVMInitArgs* args); 567 568 // System properties 569 static void init_system_properties(); 570 571 // Update/Initialize System properties after JDK version number is known 572 static void init_version_specific_system_properties(); 573 574 // Property List manipulation 575 static void PropertyList_add(SystemProperty** plist, SystemProperty *element); 576 static void PropertyList_add(SystemProperty** plist, const char* k, char* v); 577 static void PropertyList_unique_add(SystemProperty** plist, const char* k, char* v) { 578 PropertyList_unique_add(plist, k, v, false); 579 } 580 static void PropertyList_unique_add(SystemProperty** plist, const char* k, char* v, jboolean append); 581 static const char* PropertyList_get_value(SystemProperty* plist, const char* key); 582 static int PropertyList_count(SystemProperty* pl); 583 static const char* PropertyList_get_key_at(SystemProperty* pl,int index); 584 static char* PropertyList_get_value_at(SystemProperty* pl,int index); 585 586 // Miscellaneous System property value getter and setters. 587 static void set_dll_dir(char *value) { _sun_boot_library_path->set_value(value); } 588 static void set_java_home(char *value) { _java_home->set_value(value); } 589 static void set_library_path(char *value) { _java_library_path->set_value(value); } 590 static void set_ext_dirs(char *value) { _java_ext_dirs->set_value(value); } 591 static void set_endorsed_dirs(char *value) { _java_endorsed_dirs->set_value(value); } 592 static void set_sysclasspath(char *value) { _sun_boot_class_path->set_value(value); } 593 static void append_sysclasspath(const char *value) { _sun_boot_class_path->append_value(value); } 594 static void set_meta_index_path(char* meta_index_path, char* meta_index_dir) { 595 _meta_index_path = meta_index_path; 596 _meta_index_dir = meta_index_dir; 597 } 598 599 static char* get_java_home() { return _java_home->value(); } 600 static char* get_dll_dir() { return _sun_boot_library_path->value(); } 601 static char* get_endorsed_dir() { return _java_endorsed_dirs->value(); } 602 static char* get_sysclasspath() { return _sun_boot_class_path->value(); } 603 static char* get_meta_index_path() { return _meta_index_path; } 604 static char* get_meta_index_dir() { return _meta_index_dir; } 605 static char* get_ext_dirs() { return _java_ext_dirs->value(); } 606 static char* get_appclasspath() { return _java_class_path->value(); } 607 static void fix_appclasspath(); 608 609 // Operation modi 610 static Mode mode() { return _mode; } 611 static bool is_interpreter_only() { return mode() == _int; } 612 613 614 // Utility: copies src into buf, replacing "%%" with "%" and "%p" with pid. 615 static bool copy_expand_pid(const char* src, size_t srclen, char* buf, size_t buflen); 616 }; 617 618 bool Arguments::gc_selected() { 619 return UseConcMarkSweepGC || UseG1GC || UseParallelGC || UseParallelOldGC || 620 UseParNewGC || UseSerialGC; 621 } 622 623 #endif // SHARE_VM_RUNTIME_ARGUMENTS_HPP