1 /*
   2  * Copyright (c) 2015, 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 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "runtime/arguments.hpp"
  28 #include "runtime/java.hpp"
  29 #include "runtime/jniHandles.hpp"
  30 #include "services/writeableFlags.hpp"
  31 
  32 // set a boolean global flag
  33 int WriteableFlags::set_bool_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  34   int value = true;
  35 
  36   if (sscanf(arg, "%d", &value)) {
  37     return set_bool_flag(name, value != 0, origin, err_msg);
  38   }
  39   err_msg.print("flag value must be a boolean (1 or 0)");
  40   return WRONG_FORMAT;
  41 }
  42 
  43 int WriteableFlags::set_bool_flag(const char* name, bool value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  44   return CommandLineFlags::boolAtPut((char*)name, &value, origin) ? SUCCESS : ERR_OTHER;
  45 }
  46 
  47 // set a intx global flag
  48 int WriteableFlags::set_intx_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  49   intx value;
  50 
  51   if (sscanf(arg, INTX_FORMAT, &value)) {
  52     return set_intx_flag(name, value, origin, err_msg);
  53   }
  54   err_msg.print("flag value must be an integer");
  55   return WRONG_FORMAT;
  56 }
  57 
  58 int WriteableFlags::set_intx_flag(const char* name, intx value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  59   return CommandLineFlags::intxAtPut((char*)name, &value, origin) ? SUCCESS : ERR_OTHER;
  60 }
  61 
  62 // set a uintx global flag
  63 int WriteableFlags::set_uintx_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  64   uintx value;
  65 
  66   if (sscanf(arg, UINTX_FORMAT, &value)) {
  67     return set_uintx_flag(name, value, origin, err_msg);
  68   }
  69   err_msg.print("flag value must be an unsigned integer");
  70   return WRONG_FORMAT;
  71 }
  72 
  73 int WriteableFlags::set_uintx_flag(const char* name, uintx value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  74   if (strncmp(name, "MaxHeapFreeRatio", 17) == 0) {
  75       if (!Arguments::verify_MaxHeapFreeRatio(err_msg, value)) {
  76         return OUT_OF_BOUNDS;
  77       }
  78     } else if (strncmp(name, "MinHeapFreeRatio", 17) == 0) {
  79       if (!Arguments::verify_MinHeapFreeRatio(err_msg, value)) {
  80         return OUT_OF_BOUNDS;
  81       }
  82     }
  83     return CommandLineFlags::uintxAtPut((char*)name, &value, origin) ? SUCCESS : ERR_OTHER;
  84 }
  85 
  86 // set a uint64_t global flag
  87 int WriteableFlags::set_uint64_t_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  88   uint64_t value;
  89 
  90   if (sscanf(arg, UINT64_FORMAT, &value)) {
  91     return set_uint64_t_flag(name, value, origin, err_msg);
  92   }
  93   err_msg.print("flag value must be an unsigned 64-bit integer");
  94   return WRONG_FORMAT;
  95 }
  96 
  97 int WriteableFlags::set_uint64_t_flag(const char* name, uint64_t value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
  98   return CommandLineFlags::uint64_tAtPut((char*)name, &value, origin) ? SUCCESS : ERR_OTHER;
  99 }
 100 
 101 // set a size_t global flag
 102 int WriteableFlags::set_size_t_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 103   size_t value;
 104 
 105   if (sscanf(arg, SIZE_FORMAT, &value)) {
 106     return set_size_t_flag(name, value, origin, err_msg);
 107   }
 108   err_msg.print("flag value must be an unsigned integer");
 109   return WRONG_FORMAT;
 110 }
 111 
 112 int WriteableFlags::set_size_t_flag(const char* name, size_t value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 113   return CommandLineFlags::size_tAtPut((char*)name, &value, origin) ? SUCCESS : ERR_OTHER;
 114 }
 115 
 116 // set a string global flag using value from AttachOperation
 117 int WriteableFlags::set_ccstr_flag(const char* name, const char* arg, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 118   bool res = CommandLineFlags::ccstrAtPut((char*)name, &arg, origin);
 119 
 120   return res? SUCCESS : ERR_OTHER;
 121 }
 122 
 123 /* sets a writeable flag to the provided value
 124  *
 125  * - return status is one of the WriteableFlags::err enum values
 126  * - an eventual error message will be generated to the provided err_msg buffer
 127  */
 128 int WriteableFlags::set_flag(const char* flag_name, const char* flag_value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 129   return set_flag(flag_name, &flag_value, set_flag_from_char, origin, err_msg);
 130 }
 131 
 132 /* sets a writeable flag to the provided value
 133  *
 134  * - return status is one of the WriteableFlags::err enum values
 135  * - an eventual error message will be generated to the provided err_msg buffer
 136  */
 137 int WriteableFlags::set_flag(const char* flag_name, jvalue flag_value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 138   return set_flag(flag_name, &flag_value, set_flag_from_jvalue, origin, err_msg);
 139 }
 140 
 141 // a writeable flag setter accepting either 'jvalue' or 'char *' values
 142 int WriteableFlags::set_flag(const char* name, const void* value, int(*setter)(Flag*,const void*,Flag::Flags,FormatBuffer<80>&), Flag::Flags origin, FormatBuffer<80>& err_msg) {
 143   if (name == NULL) {
 144     err_msg.print("flag name is missing");
 145     return MISSING_NAME;
 146   }
 147   if (value == NULL) {
 148     err_msg.print("flag value is missing");
 149     return MISSING_VALUE;
 150   }
 151 
 152   Flag* f = Flag::find_flag((char*)name, strlen(name));
 153   if (f) {
 154     // only writeable flags are allowed to be set
 155     if (f->is_writeable()) {
 156       return setter(f, value, origin, err_msg);
 157     } else {
 158       err_msg.print("only 'writeable' flags can be set");
 159       return NON_WRITABLE;
 160     }
 161   }
 162 
 163   err_msg.print("flag %s does not exist", name);
 164   return INVALID_FLAG;
 165 }
 166 
 167 // a writeable flag setter accepting 'char *' values
 168 int WriteableFlags::set_flag_from_char(Flag* f, const void* value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 169   char* flag_value = *(char**)value;
 170   if (flag_value == NULL) {
 171     err_msg.print("flag value is missing");
 172     return MISSING_VALUE;
 173   }
 174   if (f->is_bool()) {
 175     return set_bool_flag(f->_name, flag_value, origin, err_msg);
 176   } else if (f->is_intx()) {
 177     return set_intx_flag(f->_name, flag_value, origin, err_msg);
 178   } else if (f->is_uintx()) {
 179     return set_uintx_flag(f->_name, flag_value, origin, err_msg);
 180   } else if (f->is_uint64_t()) {
 181     return set_uint64_t_flag(f->_name, flag_value, origin, err_msg);
 182   } else if (f->is_size_t()) {
 183     return set_size_t_flag(f->_name, flag_value, origin, err_msg);
 184   } else if (f->is_ccstr()) {
 185     return set_ccstr_flag(f->_name, flag_value, origin, err_msg);
 186   } else {
 187     ShouldNotReachHere();
 188   }
 189   return ERR_OTHER;
 190 }
 191 
 192 // a writeable flag setter accepting 'jvalue' values
 193 int WriteableFlags::set_flag_from_jvalue(Flag* f, const void* value, Flag::Flags origin, FormatBuffer<80>& err_msg) {
 194   jvalue new_value = *(jvalue*)value;
 195   if (f->is_bool()) {
 196     bool bvalue = (new_value.z == JNI_TRUE ? true : false);
 197     return set_bool_flag(f->_name, bvalue, origin, err_msg);
 198   } else if (f->is_intx()) {
 199     intx ivalue = (intx)new_value.j;
 200     return set_intx_flag(f->_name, ivalue, origin, err_msg);
 201   } else if (f->is_uintx()) {
 202     uintx uvalue = (uintx)new_value.j;
 203     return set_uintx_flag(f->_name, uvalue, origin, err_msg);
 204   } else if (f->is_uint64_t()) {
 205     uint64_t uvalue = (uint64_t)new_value.j;
 206     return set_uint64_t_flag(f->_name, uvalue, origin, err_msg);
 207   } else if (f->is_size_t()) {
 208     size_t svalue = (size_t)new_value.j;
 209     return set_size_t_flag(f->_name, svalue, origin, err_msg);
 210   } else if (f->is_ccstr()) {
 211     oop str = JNIHandles::resolve_external_guard(new_value.l);
 212     if (str == NULL) {
 213       err_msg.print("flag value is missing");
 214       return MISSING_VALUE;
 215     }
 216     ccstr svalue = java_lang_String::as_utf8_string(str);
 217     int ret = WriteableFlags::set_ccstr_flag(f->_name, svalue, origin, err_msg);
 218     if (ret != SUCCESS) {
 219       FREE_C_HEAP_ARRAY(char, svalue);
 220     }
 221     return ret;
 222   } else {
 223     ShouldNotReachHere();
 224   }
 225   return ERR_OTHER;
 226 }
 227