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