1 /*
2 * Copyright (c) 1997, 2026, 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_RUNTIME_ARGUMENTS_HPP
26 #define SHARE_RUNTIME_ARGUMENTS_HPP
27
28 #include "jni.h"
29 #include "memory/allocation.hpp"
30 #include "memory/allStatic.hpp"
31 #include "runtime/flags/jvmFlag.hpp"
32 #include "runtime/java.hpp"
33 #include "utilities/globalDefinitions.hpp"
34
35 // Arguments parses the command line and recognizes options
36
37 template <typename E>
38 class GrowableArray;
39 class JVMFlag;
40
41 // Invocation API hook typedefs (these should really be defined in jni.h)
42 extern "C" {
43 typedef void (JNICALL *abort_hook_t)(void);
44 typedef void (JNICALL *exit_hook_t)(jint code);
45 typedef jint (JNICALL *vfprintf_hook_t)(FILE *fp, const char *format, va_list args) ATTRIBUTE_PRINTF(2, 0);
46 }
47
48 // Obsolete or deprecated -XX flag.
49 struct SpecialFlag {
50 const char* name;
51 JDK_Version deprecated_in; // When the deprecation warning started (or "undefined").
52 JDK_Version obsolete_in; // When the obsolete warning started (or "undefined").
53 JDK_Version expired_in; // When the option expires (or "undefined").
54 };
55
56 struct LegacyGCLogging {
57 const char* file; // null -> stdout
58 int lastFlag; // 0 not set; 1 -> -verbose:gc; 2 -> -Xloggc
59 };
60
61 // PathString is used as:
62 // - the underlying value for a SystemProperty
63 // - the path portion of an --patch-module module/path pair
64 // - the string that represents the boot class path, Arguments::_boot_class_path.
65 class PathString : public CHeapObj<mtArguments> {
66 protected:
67 char* _value;
68 public:
69 char* value() const { return _value; }
70
71 // return false iff OOM && alloc_failmode == AllocFailStrategy::RETURN_NULL
72 bool set_value(const char *value, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
73 void append_value(const char *value);
74
75 PathString(const char* value);
76 ~PathString();
77 };
78
79 // ModulePatchPath records the module/path pair as specified to --patch-module.
80 class ModulePatchPath : public CHeapObj<mtInternal> {
81 private:
82 char* _module_name;
83 PathString* _path;
84 public:
85 ModulePatchPath(const char* module_name, const char* path);
86 ~ModulePatchPath();
87
88 inline const char* module_name() const { return _module_name; }
89 inline char* path_string() const { return _path->value(); }
90 };
91
92 // Element describing System and User (-Dkey=value flags) defined property.
93 //
94 // An internal SystemProperty is one that has been removed in
95 // jdk.internal.VM.saveAndRemoveProperties, like jdk.boot.class.path.append.
96 //
97 class SystemProperty : public PathString {
98 private:
99 char* _key;
100 SystemProperty* _next;
101 bool _internal;
102 bool _writeable;
103
104 public:
105 // Accessors
106 char* value() const { return PathString::value(); }
107 const char* key() const { return _key; }
108 bool internal() const { return _internal; }
109 SystemProperty* next() const { return _next; }
110 void set_next(SystemProperty* next) { _next = next; }
111 bool writeable() const { return _writeable; }
112
113 bool readable() const {
114 return !_internal || (strcmp(_key, "jdk.boot.class.path.append") == 0 &&
115 value() != nullptr);
116 }
117
118 // A system property should only have its value set
119 // via an external interface if it is a writeable property.
120 // The internal, non-writeable property jdk.boot.class.path.append
121 // is the only exception to this rule. It can be set externally
122 // via -Xbootclasspath/a or JVMTI OnLoad phase call to AddToBootstrapClassLoaderSearch.
123 // In those cases for jdk.boot.class.path.append, the base class
124 // set_value and append_value methods are called directly.
125 void set_writeable_value(const char *value) {
126 if (writeable()) {
127 set_value(value);
128 }
129 }
130 void append_writeable_value(const char *value) {
131 if (writeable()) {
132 append_value(value);
133 }
134 }
135
136 // Constructor
137 SystemProperty(const char* key, const char* value, bool writeable, bool internal = false);
138 };
139
140 // Helper class for controlling the lifetime of JavaVMInitArgs objects.
141 class ScopedVMInitArgs;
142
143 struct VMInitArgsGroup;
144 template <typename E, MemTag MT> class GrowableArrayCHeap;
145
146 class Arguments : AllStatic {
147 friend class VMStructs;
148 friend class JvmtiExport;
149 friend class ArgumentsTest;
150 friend class LargeOptionsTest;
151 public:
152 // Operation modi
153 enum Mode {
154 _int, // corresponds to -Xint
155 _mixed, // corresponds to -Xmixed
156 _comp // corresponds to -Xcomp
157 };
158
159 enum ArgsRange {
160 arg_unreadable = -3,
161 arg_too_small = -2,
162 arg_too_big = -1,
163 arg_in_range = 0
164 };
165
166 enum PropertyAppendable {
167 AppendProperty,
168 AddProperty
169 };
170
171 enum PropertyWriteable {
172 WriteableProperty,
173 UnwriteableProperty
174 };
175
176 enum PropertyInternal {
177 InternalProperty,
178 ExternalProperty
179 };
180
181 private:
182
183 // a pointer to the flags file name if it is specified
184 static char* _jvm_flags_file;
185 // an array containing all flags specified in the .hotspotrc file
186 static char** _jvm_flags_array;
187 static int _num_jvm_flags;
188 // an array containing all jvm arguments specified in the command line
189 static char** _jvm_args_array;
190 static int _num_jvm_args;
191 // string containing all java command (class/jarfile name and app args)
192 static char* _java_command;
193 // number of unique modules specified in the --add-modules option
194 static unsigned int _addmods_count;
195
196 // Property list
197 static SystemProperty* _system_properties;
198
199 // Quick accessor to System properties in the list:
200 static SystemProperty *_sun_boot_library_path;
201 static SystemProperty *_java_library_path;
202 static SystemProperty *_java_home;
203 static SystemProperty *_java_class_path;
204 static SystemProperty *_jdk_boot_class_path_append;
205 static SystemProperty *_vm_info;
206
207 // --patch-module=module=<file>(<pathsep><file>)*
208 // Each element contains the associated module name, path
209 // string pair as specified to --patch-module.
210 static GrowableArray<ModulePatchPath*>* _patch_mod_prefix;
211
212 // The constructed value of the system class path after
213 // argument processing and JVMTI OnLoad additions via
214 // calls to AddToBootstrapClassLoaderSearch. This is the
215 // final form before ClassLoader::setup_bootstrap_search().
216 // Note: since --patch-module is a module name/path pair, the
217 // boot class path string no longer contains the "prefix"
218 // to the boot class path base piece as it did when
219 // -Xbootclasspath/p was supported.
220 static PathString* _boot_class_path;
221
222 // Set if a modular java runtime image is present vs. a build with exploded modules
223 static bool _has_jimage;
224
225 // temporary: to emit warning if the default ext dirs are not empty.
226 // remove this variable when the warning is no longer needed.
227 static char* _ext_dirs;
228
229 // java.vendor.url.bug, bug reporting URL for fatal errors.
230 static const char* _java_vendor_url_bug;
231
232 // sun.java.launcher, private property to provide information about
233 // java launcher
234 static const char* _sun_java_launcher;
235
236 // was this VM created with the -XX:+ExecutingUnitTests option
237 static bool _executing_unit_tests;
238
239 // for legacy gc options (-verbose:gc and -Xloggc:)
240 static LegacyGCLogging _legacyGCLogging;
241
242 // Value of the conservative maximum heap alignment needed
243 static size_t _conservative_max_heap_alignment;
244
245 // Operation modi
246 static Mode _mode;
247
248 // preview features
249 static bool _enable_preview;
250
251 // jdwp
252 static bool _has_jdwp_agent;
253
254 // Used to save default settings
255 static bool _AlwaysCompileLoopMethods;
256 static bool _UseOnStackReplacement;
257 static bool _BackgroundCompilation;
258 static bool _ClipInlining;
259
260 // GC ergonomics
261 static void set_conservative_max_heap_alignment();
262 static void set_use_compressed_oops();
263 static jint set_ergonomics_flags();
264 static void set_compact_headers_flags();
265
266 // Bytecode rewriting
267 static void set_bytecode_flags();
268
269 // Invocation API hooks
270 static abort_hook_t _abort_hook;
271 static exit_hook_t _exit_hook;
272 static vfprintf_hook_t _vfprintf_hook;
273
274 // System properties
275 static bool add_property(const char* prop, PropertyWriteable writeable=WriteableProperty,
276 PropertyInternal internal=ExternalProperty);
277
278 // Used for module system related properties: converted from command-line flags.
279 // Basic properties are writeable as they operate as "last one wins" and will get overwritten.
280 // Numbered properties are never writeable, and always internal.
281 static bool create_module_property(const char* prop_name, const char* prop_value, PropertyInternal internal);
282 static bool create_numbered_module_property(const char* prop_base_name, const char* prop_value, unsigned int count);
283
284 static int process_patch_mod_option(const char* patch_mod_tail);
285
286 // Aggressive optimization flags.
287 static jint set_aggressive_opts_flags();
288
289 // Argument parsing
290 static bool parse_argument(const char* arg, JVMFlagOrigin origin);
291 static bool process_argument(const char* arg, jboolean ignore_unrecognized, JVMFlagOrigin origin);
292 static void process_java_launcher_argument(const char*, void*);
293 static jint parse_options_environment_variable(const char* name, ScopedVMInitArgs* vm_args);
294 static jint parse_java_tool_options_environment_variable(ScopedVMInitArgs* vm_args);
295 static jint parse_java_options_environment_variable(ScopedVMInitArgs* vm_args);
296 static jint parse_jdk_aot_vm_options_environment_variable(GrowableArrayCHeap<VMInitArgsGroup, mtArguments>* all_args,
297 ScopedVMInitArgs* jdk_aot_vm_options_args);
298 static jint parse_vm_options_file(const char* file_name, ScopedVMInitArgs* vm_args);
299 static jint parse_options_buffer(const char* name, char* buffer, const size_t buf_len, ScopedVMInitArgs* vm_args);
300 static jint parse_xss(const JavaVMOption* option, const char* tail, intx* out_ThreadStackSize);
301 static jint insert_vm_options_file(const JavaVMInitArgs* args,
302 const char* vm_options_file,
303 const int vm_options_file_pos,
304 ScopedVMInitArgs* vm_options_file_args,
305 ScopedVMInitArgs* args_out);
306 static bool args_contains_vm_options_file_arg(const JavaVMInitArgs* args);
307 static jint expand_vm_options_as_needed(const JavaVMInitArgs* args_in,
308 ScopedVMInitArgs* mod_args,
309 JavaVMInitArgs** args_out);
310 static jint match_special_option_and_act(const JavaVMInitArgs* args,
311 ScopedVMInitArgs* args_out);
312
313 static bool handle_deprecated_print_gc_flags();
314
315 static jint parse_vm_init_args(GrowableArrayCHeap<VMInitArgsGroup, mtArguments>* all_args);
316 static jint parse_each_vm_init_arg(const JavaVMInitArgs* args, JVMFlagOrigin origin);
317 static jint finalize_vm_init_args();
318 static bool is_bad_option(const JavaVMOption* option, jboolean ignore, const char* option_type);
319
320 static bool is_bad_option(const JavaVMOption* option, jboolean ignore) {
321 return is_bad_option(option, ignore, nullptr);
322 }
323
324 static void describe_range_error(ArgsRange errcode);
325 static ArgsRange check_memory_size(julong size, julong min_size, julong max_size);
326 static ArgsRange parse_memory_size(const char* s, julong* long_arg,
327 julong min_size, julong max_size = max_uintx);
328
329 // methods to build strings from individual args
330 static void build_jvm_args(const char* arg);
331 static void build_jvm_flags(const char* arg);
332 static void add_string(char*** bldarray, int* count, const char* arg);
333 static const char* build_resource_string(char** args, int count);
334
335 // Returns true if the flag is obsolete (and not yet expired).
336 // In this case the 'version' buffer is filled in with
337 // the version number when the flag became obsolete.
338 static bool is_obsolete_flag(const char* flag_name, JDK_Version* version);
339
340 // Returns 1 if the flag is deprecated (and not yet obsolete or expired).
341 // In this case the 'version' buffer is filled in with the version number when
342 // the flag became deprecated.
343 // Returns -1 if the flag is expired or obsolete.
344 // Returns 0 otherwise.
345 static int is_deprecated_flag(const char* flag_name, JDK_Version* version);
346
347 // Return the real name for the flag passed on the command line (either an alias name or "flag_name").
348 static const char* real_flag_name(const char *flag_name);
349 static JVMFlag* find_jvm_flag(const char* name, size_t name_length);
350
351 // Return the "real" name for option arg if arg is an alias, and print a warning if arg is deprecated.
352 // Return nullptr if the arg has expired.
353 static const char* handle_aliases_and_deprecation(const char* arg);
354 static size_t _default_SharedBaseAddress; // The default value specified in globals.hpp
355
356 static bool internal_module_property_helper(const char* property, bool check_for_cds);
357
358 public:
359 // Parses the arguments, first phase
360 static jint parse(const JavaVMInitArgs* args);
361 // Parse a string for a unsigned integer. Returns true if value
362 // is an unsigned integer greater than or equal to the minimum
363 // parameter passed and returns the value in uint_arg. Returns
364 // false otherwise, with uint_arg undefined.
365 static bool parse_uint(const char* value, uint* uintx_arg,
366 uint min_size);
367 // Apply ergonomics
368 static jint apply_ergo();
369 // Adjusts the arguments after the OS have adjusted the arguments
370 static jint adjust_after_os();
371
372 // Check consistency or otherwise of VM argument settings
373 static bool check_vm_args_consistency();
374 // Used by os_solaris
375 static bool process_settings_file(const char* file_name, bool should_exist, jboolean ignore_unrecognized);
376
377 static size_t conservative_max_heap_alignment() { return _conservative_max_heap_alignment; }
378 // Return the maximum size a heap with compressed oops can take
379 static size_t max_heap_for_compressed_oops();
380
381 // return a char* array containing all options
382 static char** jvm_flags_array() { return _jvm_flags_array; }
383 static char** jvm_args_array() { return _jvm_args_array; }
384 static int num_jvm_flags() { return _num_jvm_flags; }
385 static int num_jvm_args() { return _num_jvm_args; }
386 // return the arguments passed to the Java application
387 static const char* java_command() { return _java_command; }
388
389 // print jvm_flags, jvm_args and java_command
390 static void print_on(outputStream* st);
391 static void print_summary_on(outputStream* st);
392
393 // convenient methods to get and set jvm_flags_file
394 static const char* get_jvm_flags_file() { return _jvm_flags_file; }
395 static void set_jvm_flags_file(const char *value);
396
397 // convenient methods to obtain / print jvm_flags and jvm_args
398 static const char* jvm_flags() { return build_resource_string(_jvm_flags_array, _num_jvm_flags); }
399 static const char* jvm_args() { return build_resource_string(_jvm_args_array, _num_jvm_args); }
400 static void print_jvm_flags_on(outputStream* st);
401 static void print_jvm_args_on(outputStream* st);
402
403 // -Dkey=value flags
404 static SystemProperty* system_properties() { return _system_properties; }
405 static const char* get_property(const char* key);
406
407 // -Djava.vendor.url.bug
408 static const char* java_vendor_url_bug() { return _java_vendor_url_bug; }
409
410 // -Dsun.java.launcher
411 static const char* sun_java_launcher() { return _sun_java_launcher; }
412 // Was VM created by a Java launcher?
413 static bool created_by_java_launcher();
414 // -XX:+ExecutingUnitTests
415 static bool executing_unit_tests();
416
417 // abort, exit, vfprintf hooks
418 static abort_hook_t abort_hook() { return _abort_hook; }
419 static exit_hook_t exit_hook() { return _exit_hook; }
420 static vfprintf_hook_t vfprintf_hook() { return _vfprintf_hook; }
421
422 static void no_shared_spaces(const char* message);
423 static size_t default_SharedBaseAddress() { return _default_SharedBaseAddress; }
424 // Java launcher properties
425 static void process_sun_java_launcher_properties(JavaVMInitArgs* args);
426
427 // System properties
428 static void init_system_properties();
429
430 // Update/Initialize System properties after JDK version number is known
431 static void init_version_specific_system_properties();
432
433 // Update VM info property - called after argument parsing
434 static void update_vm_info_property(const char* vm_info) {
435 _vm_info->set_value(vm_info);
436 }
437
438 // Property List manipulation
439 static void PropertyList_add(SystemProperty *element);
440 static void PropertyList_add(SystemProperty** plist, SystemProperty *element);
441 static void PropertyList_add(SystemProperty** plist, const char* k, const char* v, bool writeable, bool internal);
442
443 static void PropertyList_unique_add(SystemProperty** plist, const char* k, const char* v,
444 PropertyAppendable append, PropertyWriteable writeable,
445 PropertyInternal internal);
446 static const char* PropertyList_get_value(SystemProperty* plist, const char* key);
447 static const char* PropertyList_get_readable_value(SystemProperty* plist, const char* key);
448 static int PropertyList_count(SystemProperty* pl);
449 static int PropertyList_readable_count(SystemProperty* pl);
450
451 static bool is_internal_module_property(const char* option);
452 static bool is_incompatible_cds_internal_module_property(const char* property);
453
454 // Miscellaneous System property value getter and setters.
455 static void set_dll_dir(const char *value) { _sun_boot_library_path->set_value(value); }
456 static void set_java_home(const char *value) { _java_home->set_value(value); }
457 static void set_library_path(const char *value) { _java_library_path->set_value(value); }
458 static void set_ext_dirs(char *value);
459
460 // Set up the underlying pieces of the boot class path
461 static void add_patch_mod_prefix(const char *module_name, const char *path);
462 static void set_boot_class_path(const char *value, bool has_jimage) {
463 // During start up, set by os::set_boot_path()
464 assert(get_boot_class_path() == nullptr, "Boot class path previously set");
465 _boot_class_path->set_value(value);
466 _has_jimage = has_jimage;
467 }
468 static void append_sysclasspath(const char *value) {
469 _boot_class_path->append_value(value);
470 _jdk_boot_class_path_append->append_value(value);
471 }
472
473 static GrowableArray<ModulePatchPath*>* get_patch_mod_prefix() { return _patch_mod_prefix; }
474 static char* get_boot_class_path() { return _boot_class_path->value(); }
475 static bool has_jimage() { return _has_jimage; }
476
477 static char* get_java_home() { return _java_home->value(); }
478 static char* get_dll_dir() { return _sun_boot_library_path->value(); }
479 static char* get_appclasspath() { return _java_class_path->value(); }
480 static void fix_appclasspath();
481
482 // Operation modi
483 static Mode mode() { return _mode; }
484 static void set_mode_flags(Mode mode);
485 static bool is_interpreter_only() { return mode() == _int; }
486 static bool is_compiler_only() { return mode() == _comp; }
487
488
489 // preview features
490 static void set_enable_preview() { _enable_preview = true; }
491 static bool enable_preview() { return _enable_preview; }
492
493 // jdwp
494 static bool has_jdwp_agent() { return _has_jdwp_agent; }
495
496 // Utility: copies src into buf, replacing "%%" with "%" and "%p" with pid.
497 static bool copy_expand_pid(const char* src, size_t srclen, char* buf, size_t buflen);
498
499 static bool atojulong(const char *s, julong* result);
500
501 static bool has_jfr_option() NOT_JFR_RETURN_(false);
502
503 DEBUG_ONLY(static bool verify_special_jvm_flags(bool check_globals);)
504 };
505
506 // Disable options not supported in this release, with a warning if they
507 // were explicitly requested on the command-line
508 #define UNSUPPORTED_OPTION(opt) \
509 do { \
510 if (opt) { \
511 if (FLAG_IS_CMDLINE(opt)) { \
512 warning("-XX:+" #opt " not supported in this VM"); \
513 } \
514 FLAG_SET_DEFAULT(opt, false); \
515 } \
516 } while(0)
517
518 // similar to UNSUPPORTED_OPTION but sets flag to nullptr
519 #define UNSUPPORTED_OPTION_NULL(opt) \
520 do { \
521 if (opt) { \
522 if (FLAG_IS_CMDLINE(opt)) { \
523 warning("-XX flag " #opt " not supported in this VM"); \
524 } \
525 FLAG_SET_DEFAULT(opt, nullptr); \
526 } \
527 } while(0)
528
529 // Initialize options not supported in this release, with a warning
530 // if they were explicitly requested on the command-line
531 #define UNSUPPORTED_OPTION_INIT(opt, value) \
532 do { \
533 if (FLAG_IS_CMDLINE(opt)) { \
534 warning("-XX flag " #opt " not supported in this VM"); \
535 } \
536 FLAG_SET_DEFAULT(opt, value); \
537 } while(0)
538
539 #endif // SHARE_RUNTIME_ARGUMENTS_HPP