1 /*
   2  * Copyright (c) 1997, 2009, 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   static intx _Tier2CompileThreshold;
 292 
 293   // CMS/ParNew garbage collectors
 294   static void set_parnew_gc_flags();
 295   static void set_cms_and_parnew_gc_flags();
 296   // UseParallel[Old]GC
 297   static void set_parallel_gc_flags();
 298   // Garbage-First (UseG1GC)
 299   static void set_g1_gc_flags();
 300   // GC ergonomics
 301   static void set_ergonomics_flags();
 302   // Setup heap size
 303   static void set_heap_size();
 304   // Based on automatic selection criteria, should the
 305   // low pause collector be used.
 306   static bool should_auto_select_low_pause_collector();
 307 
 308   // Bytecode rewriting
 309   static void set_bytecode_flags();
 310 
 311   // Invocation API hooks
 312   static abort_hook_t     _abort_hook;
 313   static exit_hook_t      _exit_hook;
 314   static vfprintf_hook_t  _vfprintf_hook;
 315 
 316   // System properties
 317   static bool add_property(const char* prop);
 318 
 319   // Aggressive optimization flags.
 320   static void set_aggressive_opts_flags();
 321 
 322   // Argument parsing
 323   static void do_pd_flag_adjustments();
 324   static bool parse_argument(const char* arg, FlagValueOrigin origin);
 325   static bool process_argument(const char* arg, jboolean ignore_unrecognized, FlagValueOrigin origin);
 326   static void process_java_launcher_argument(const char*, void*);
 327   static void process_java_compiler_argument(char* arg);
 328   static jint parse_options_environment_variable(const char* name, SysClassPath* scp_p, bool* scp_assembly_required_p);
 329   static jint parse_java_tool_options_environment_variable(SysClassPath* scp_p, bool* scp_assembly_required_p);
 330   static jint parse_java_options_environment_variable(SysClassPath* scp_p, bool* scp_assembly_required_p);
 331   static jint parse_vm_init_args(const JavaVMInitArgs* args);
 332   static jint parse_each_vm_init_arg(const JavaVMInitArgs* args, SysClassPath* scp_p, bool* scp_assembly_required_p, FlagValueOrigin origin);
 333   static jint finalize_vm_init_args(SysClassPath* scp_p, bool scp_assembly_required);
 334   static bool is_bad_option(const JavaVMOption* option, jboolean ignore,
 335     const char* option_type);
 336   static bool is_bad_option(const JavaVMOption* option, jboolean ignore) {
 337     return is_bad_option(option, ignore, NULL);
 338   }
 339   static bool verify_interval(uintx val, uintx min,
 340                               uintx max, const char* name);
 341   static bool verify_percentage(uintx value, const char* name);
 342   static void describe_range_error(ArgsRange errcode);
 343   static ArgsRange check_memory_size(julong size, julong min_size);
 344   static ArgsRange parse_memory_size(const char* s, julong* long_arg,
 345                                      julong min_size);
 346   // Parse a string for a unsigned integer.  Returns true if value
 347   // is an unsigned integer greater than or equal to the minimum
 348   // parameter passed and returns the value in uintx_arg.  Returns
 349   // false otherwise, with uintx_arg undefined.
 350   static bool parse_uintx(const char* value, uintx* uintx_arg,
 351                           uintx min_size);
 352 
 353   // methods to build strings from individual args
 354   static void build_jvm_args(const char* arg);
 355   static void build_jvm_flags(const char* arg);
 356   static void add_string(char*** bldarray, int* count, const char* arg);
 357   static const char* build_resource_string(char** args, int count);
 358 
 359   static bool methodExists(
 360     char* className, char* methodName,
 361     int classesNum, char** classes, bool* allMethods,
 362     int methodsNum, char** methods, bool* allClasses
 363   );
 364 
 365   static void parseOnlyLine(
 366     const char* line,
 367     short* classesNum, short* classesMax, char*** classes, bool** allMethods,
 368     short* methodsNum, short* methodsMax, char*** methods, bool** allClasses
 369   );
 370 
 371   // Returns true if the string s is in the list of flags that have recently
 372   // been made obsolete.  If we detect one of these flags on the command
 373   // line, instead of failing we print a warning message and ignore the
 374   // flag.  This gives the user a release or so to stop using the flag.
 375   static bool is_newly_obsolete(const char* s, JDK_Version* buffer);
 376 
 377   static short  CompileOnlyClassesNum;
 378   static short  CompileOnlyClassesMax;
 379   static char** CompileOnlyClasses;
 380   static bool*  CompileOnlyAllMethods;
 381 
 382   static short  CompileOnlyMethodsNum;
 383   static short  CompileOnlyMethodsMax;
 384   static char** CompileOnlyMethods;
 385   static bool*  CompileOnlyAllClasses;
 386 
 387   static short  InterpretOnlyClassesNum;
 388   static short  InterpretOnlyClassesMax;
 389   static char** InterpretOnlyClasses;
 390   static bool*  InterpretOnlyAllMethods;
 391 
 392   static bool   CheckCompileOnly;
 393 
 394   static char*  SharedArchivePath;
 395 
 396  public:
 397   // Parses the arguments
 398   static jint parse(const JavaVMInitArgs* args);
 399   // Check for consistency in the selection of the garbage collector.
 400   static bool check_gc_consistency();
 401   // Check consistecy or otherwise of VM argument settings
 402   static bool check_vm_args_consistency();
 403   // Used by os_solaris
 404   static bool process_settings_file(const char* file_name, bool should_exist, jboolean ignore_unrecognized);
 405 
 406   // return a char* array containing all options
 407   static char** jvm_flags_array()          { return _jvm_flags_array; }
 408   static char** jvm_args_array()           { return _jvm_args_array; }
 409   static int num_jvm_flags()               { return _num_jvm_flags; }
 410   static int num_jvm_args()                { return _num_jvm_args; }
 411   // return the arguments passed to the Java application
 412   static const char* java_command()        { return _java_command; }
 413 
 414   // print jvm_flags, jvm_args and java_command
 415   static void print_on(outputStream* st);
 416 
 417   // convenient methods to obtain / print jvm_flags and jvm_args
 418   static const char* jvm_flags()           { return build_resource_string(_jvm_flags_array, _num_jvm_flags); }
 419   static const char* jvm_args()            { return build_resource_string(_jvm_args_array, _num_jvm_args); }
 420   static void print_jvm_flags_on(outputStream* st);
 421   static void print_jvm_args_on(outputStream* st);
 422 
 423   // -Dkey=value flags
 424   static SystemProperty*  system_properties()   { return _system_properties; }
 425   static const char*    get_property(const char* key);
 426 
 427   // -Djava.vendor.url.bug
 428   static const char* java_vendor_url_bug()  { return _java_vendor_url_bug; }
 429 
 430   // -Dsun.java.launcher
 431   static const char* sun_java_launcher()    { return _sun_java_launcher; }
 432   // Was VM created by a Java launcher?
 433   static bool created_by_java_launcher();
 434   // -Dsun.java.launcher.pid
 435   static int sun_java_launcher_pid()        { return _sun_java_launcher_pid; }
 436 
 437   // -Xloggc:<file>, if not specified will be NULL
 438   static const char* gc_log_filename()      { return _gc_log_filename; }
 439 
 440   // -Xprof/-Xaprof
 441   static bool has_profile()                 { return _has_profile; }
 442   static bool has_alloc_profile()           { return _has_alloc_profile; }
 443 
 444   // -Xms, -Xmx
 445   static uintx min_heap_size()              { return _min_heap_size; }
 446   static void  set_min_heap_size(uintx v)   { _min_heap_size = v;  }
 447 
 448   // -Xrun
 449   static AgentLibrary* libraries()          { return _libraryList.first(); }
 450   static bool init_libraries_at_startup()   { return !_libraryList.is_empty(); }
 451   static void convert_library_to_agent(AgentLibrary* lib)
 452                                             { _libraryList.remove(lib);
 453                                               _agentList.add(lib); }
 454 
 455   // -agentlib -agentpath
 456   static AgentLibrary* agents()             { return _agentList.first(); }
 457   static bool init_agents_at_startup()      { return !_agentList.is_empty(); }
 458 
 459   // abort, exit, vfprintf hooks
 460   static abort_hook_t    abort_hook()       { return _abort_hook; }
 461   static exit_hook_t     exit_hook()        { return _exit_hook; }
 462   static vfprintf_hook_t vfprintf_hook()    { return _vfprintf_hook; }
 463 
 464   static bool GetCheckCompileOnly ()        { return CheckCompileOnly; }
 465 
 466   static const char* GetSharedArchivePath() { return SharedArchivePath; }
 467 
 468   static bool CompileMethod(char* className, char* methodName) {
 469     return
 470       methodExists(
 471         className, methodName,
 472         CompileOnlyClassesNum, CompileOnlyClasses, CompileOnlyAllMethods,
 473         CompileOnlyMethodsNum, CompileOnlyMethods, CompileOnlyAllClasses
 474       );
 475   }
 476 
 477   // Java launcher properties
 478   static void process_sun_java_launcher_properties(JavaVMInitArgs* args);
 479 
 480   // System properties
 481   static void init_system_properties();
 482 
 483   // Property List manipulation
 484   static void PropertyList_add(SystemProperty** plist, SystemProperty *element);
 485   static void PropertyList_add(SystemProperty** plist, const char* k, char* v);
 486   static void PropertyList_unique_add(SystemProperty** plist, const char* k, char* v) {
 487     PropertyList_unique_add(plist, k, v, false);
 488   }
 489   static void PropertyList_unique_add(SystemProperty** plist, const char* k, char* v, jboolean append);
 490   static const char* PropertyList_get_value(SystemProperty* plist, const char* key);
 491   static int  PropertyList_count(SystemProperty* pl);
 492   static const char* PropertyList_get_key_at(SystemProperty* pl,int index);
 493   static char* PropertyList_get_value_at(SystemProperty* pl,int index);
 494 
 495   // Miscellaneous System property value getter and setters.
 496   static void set_dll_dir(char *value) { _sun_boot_library_path->set_value(value); }
 497   static void set_java_home(char *value) { _java_home->set_value(value); }
 498   static void set_library_path(char *value) { _java_library_path->set_value(value); }
 499   static void set_ext_dirs(char *value) { _java_ext_dirs->set_value(value); }
 500   static void set_endorsed_dirs(char *value) { _java_endorsed_dirs->set_value(value); }
 501   static void set_sysclasspath(char *value) { _sun_boot_class_path->set_value(value); }
 502   static void append_sysclasspath(const char *value) { _sun_boot_class_path->append_value(value); }
 503   static void set_meta_index_path(char* meta_index_path, char* meta_index_dir) {
 504     _meta_index_path = meta_index_path;
 505     _meta_index_dir  = meta_index_dir;
 506   }
 507 
 508   static char *get_java_home() { return _java_home->value(); }
 509   static char *get_dll_dir() { return _sun_boot_library_path->value(); }
 510   static char *get_endorsed_dir() { return _java_endorsed_dirs->value(); }
 511   static char *get_sysclasspath() { return _sun_boot_class_path->value(); }
 512   static char* get_meta_index_path() { return _meta_index_path; }
 513   static char* get_meta_index_dir()  { return _meta_index_dir;  }
 514 
 515   // Operation modi
 516   static Mode mode()                        { return _mode; }
 517 
 518   // Utility: copies src into buf, replacing "%%" with "%" and "%p" with pid.
 519   static bool copy_expand_pid(const char* src, size_t srclen, char* buf, size_t buflen);
 520 
 521 #ifdef KERNEL
 522   // For java kernel vm, return property string for kernel properties.
 523   static char *get_kernel_properties();
 524 #endif // KERNEL
 525 };