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