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