2
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/javaClasses.hpp"
27
#include "memory/allocation.inline.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "runtime/flags/jvmFlag.hpp"
30
#include "runtime/flags/jvmFlagAccess.hpp"
31
#include "runtime/flags/jvmFlagLimit.hpp"
32
#include "runtime/java.hpp"
33
#include "runtime/jniHandles.hpp"
34
#include "services/writeableFlags.hpp"
36
#define TEMP_BUF_SIZE 80
38
static void buffer_concat(char* buffer, const char* src) {
39
strncat(buffer, src, TEMP_BUF_SIZE - 1 - strlen(buffer));
42
static void print_flag_error_message_bounds(const JVMFlag* flag, char* buffer) {
43
if (JVMFlagLimit::get_range(flag) != nullptr) {
44
buffer_concat(buffer, "must have value in range ");
47
JVMFlagAccess::print_range(&stream, flag);
48
const char* range_string = stream.as_string();
49
size_t j = strlen(buffer);
50
for (size_t i=0; j<TEMP_BUF_SIZE-1; i++) {
51
if (range_string[i] == '\0') {
53
} else if (range_string[i] != ' ') {
54
buffer[j] = range_string[i];
62
static void print_flag_error_message_if_needed(JVMFlag::Error error, const JVMFlag* flag, FormatBuffer<80>& err_msg) {
63
if (error == JVMFlag::SUCCESS) {
67
const char* name = flag->name();
68
char buffer[TEMP_BUF_SIZE] = {'\0'};
69
if ((error != JVMFlag::MISSING_NAME) && (name != nullptr)) {
70
buffer_concat(buffer, name);
71
buffer_concat(buffer, " error: ");
73
buffer_concat(buffer, "Error: ");
76
case JVMFlag::MISSING_NAME:
77
buffer_concat(buffer, "flag name is missing."); break;
78
case JVMFlag::MISSING_VALUE:
79
buffer_concat(buffer, "parsing the textual form of the value."); break;
80
case JVMFlag::NON_WRITABLE:
81
buffer_concat(buffer, "flag is not writeable."); break;
82
case JVMFlag::OUT_OF_BOUNDS:
83
if (name != nullptr) { print_flag_error_message_bounds(flag, buffer); } break;
84
case JVMFlag::VIOLATES_CONSTRAINT:
85
buffer_concat(buffer, "value violates its flag's constraint."); break;
86
case JVMFlag::INVALID_FLAG:
87
buffer_concat(buffer, "there is no flag with the given name."); break;
88
case JVMFlag::ERR_OTHER:
89
buffer_concat(buffer, "other, unspecified error related to setting the flag."); break;
90
case JVMFlag::SUCCESS:
96
err_msg.print("%s", buffer);
99
template <typename T, int type_enum>
100
JVMFlag::Error WriteableFlags::set_flag_impl(const char* name, T value, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
101
JVMFlag* flag = JVMFlag::find_flag(name);
102
JVMFlag::Error err = JVMFlagAccess::set<T, type_enum>(flag, &value, origin);
103
print_flag_error_message_if_needed(err, flag, err_msg);
108
// set a boolean global flag
109
JVMFlag::Error WriteableFlags::set_bool_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
110
if ((strcasecmp(arg, "true") == 0) || (*arg == '1' && *(arg + 1) == 0)) {
111
return set_flag_impl<JVM_FLAG_TYPE(bool)>(name, true, origin, err_msg);
112
} else if ((strcasecmp(arg, "false") == 0) || (*arg == '0' && *(arg + 1) == 0)) {
113
return set_flag_impl<JVM_FLAG_TYPE(bool)>(name, false, origin, err_msg);
115
err_msg.print("flag value must be a boolean (1/0 or true/false)");
116
return JVMFlag::WRONG_FORMAT;
119
// set a int global flag
120
JVMFlag::Error WriteableFlags::set_int_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
123
if (sscanf(arg, "%d", &value) == 1) {
124
return set_flag_impl<JVM_FLAG_TYPE(int)>(name, value, origin, err_msg);
126
err_msg.print("flag value must be an integer");
127
return JVMFlag::WRONG_FORMAT;
130
// set a uint global flag
131
JVMFlag::Error WriteableFlags::set_uint_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
134
if (sscanf(arg, "%u", &value) == 1) {
135
return set_flag_impl<JVM_FLAG_TYPE(uint)>(name, value, origin, err_msg);
137
err_msg.print("flag value must be an unsigned integer");
138
return JVMFlag::WRONG_FORMAT;
141
// set a intx global flag
142
JVMFlag::Error WriteableFlags::set_intx_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
145
if (sscanf(arg, INTX_FORMAT, &value) == 1) {
146
return set_flag_impl<JVM_FLAG_TYPE(intx)>(name, value, origin, err_msg);
148
err_msg.print("flag value must be an integer");
149
return JVMFlag::WRONG_FORMAT;
152
// set a uintx global flag
153
JVMFlag::Error WriteableFlags::set_uintx_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
156
if (sscanf(arg, UINTX_FORMAT, &value) == 1) {
157
return set_flag_impl<JVM_FLAG_TYPE(uintx)>(name, value, origin, err_msg);
159
err_msg.print("flag value must be an unsigned integer");
160
return JVMFlag::WRONG_FORMAT;
163
// set a uint64_t global flag
164
JVMFlag::Error WriteableFlags::set_uint64_t_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
167
if (sscanf(arg, UINT64_FORMAT, &value) == 1) {
168
return set_flag_impl<JVM_FLAG_TYPE(uint64_t)>(name, value, origin, err_msg);
170
err_msg.print("flag value must be an unsigned 64-bit integer");
171
return JVMFlag::WRONG_FORMAT;
174
// set a size_t global flag
175
JVMFlag::Error WriteableFlags::set_size_t_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
178
if (sscanf(arg, SIZE_FORMAT, &value) == 1) {
179
return set_flag_impl<JVM_FLAG_TYPE(size_t)>(name, value, origin, err_msg);
181
err_msg.print("flag value must be an unsigned integer");
182
return JVMFlag::WRONG_FORMAT;
185
// set a double global flag
186
JVMFlag::Error WriteableFlags::set_double_flag(const char* name, const char* arg, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
189
if (sscanf(arg, "%lf", &value) == 1) {
190
return set_flag_impl<JVM_FLAG_TYPE(double)>(name, value, origin, err_msg);
192
err_msg.print("flag value must be a double");
193
return JVMFlag::WRONG_FORMAT;
196
// set a string global flag using value from AttachOperation
197
JVMFlag::Error WriteableFlags::set_ccstr_flag(const char* name, const char* value, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
198
JVMFlag* flag = JVMFlag::find_flag(name);
199
JVMFlag::Error err = JVMFlagAccess::set_ccstr(flag, &value, origin);
200
if (err == JVMFlag::SUCCESS) {
201
assert(value == nullptr, "old value is freed automatically and not returned");
203
print_flag_error_message_if_needed(err, flag, err_msg);
207
/* sets a writeable flag to the provided value
209
* - return status is one of the WriteableFlags::err enum values
210
* - an eventual error message will be generated to the provided err_msg buffer
212
JVMFlag::Error WriteableFlags::set_flag(const char* flag_name, const char* flag_value, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
213
return set_flag(flag_name, &flag_value, set_flag_from_char, origin, err_msg);
216
/* sets a writeable flag to the provided value
218
* - return status is one of the WriteableFlags::err enum values
219
* - an eventual error message will be generated to the provided err_msg buffer
221
JVMFlag::Error WriteableFlags::set_flag(const char* flag_name, jvalue flag_value, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
222
return set_flag(flag_name, &flag_value, set_flag_from_jvalue, origin, err_msg);
225
// a writeable flag setter accepting either 'jvalue' or 'char *' values
226
JVMFlag::Error WriteableFlags::set_flag(const char* name, const void* value, JVMFlag::Error(*setter)(JVMFlag*,const void*,JVMFlagOrigin,FormatBuffer<80>&), JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
227
if (name == nullptr) {
228
err_msg.print("flag name is missing");
229
return JVMFlag::MISSING_NAME;
231
if (value == nullptr) {
232
err_msg.print("flag value is missing");
233
return JVMFlag::MISSING_VALUE;
236
JVMFlag* f = JVMFlag::find_flag(name);
238
// only writeable flags are allowed to be set
239
if (f->is_writeable()) {
240
return setter(f, value, origin, err_msg);
242
err_msg.print("only 'writeable' flags can be set");
243
return JVMFlag::NON_WRITABLE;
247
err_msg.print("flag %s does not exist", name);
248
return JVMFlag::INVALID_FLAG;
251
// a writeable flag setter accepting 'char *' values
252
JVMFlag::Error WriteableFlags::set_flag_from_char(JVMFlag* f, const void* value, JVMFlagOrigin origin, FormatBuffer<80>& err_msg) {
253
char* flag_value = *(char**)value;
254
if (flag_value == nullptr) {
255
err_msg.print("flag value is missing");
256
return JVMFlag::MISSING_VALUE;
259
return set_bool_flag(f->name(), flag_value, origin, err_msg);
260
} else if (f->is_int()) {
261
return set_int_flag(f->name(), flag_value, origin, err_msg);
262
} else if (f->is_uint()) {
263
return set_uint_flag(f->name(), flag_value, origin, err_msg);
264
} else if (f->is_intx()) {
265
return set_intx_flag(f->name(), flag_value, origin, err_msg);
266
} else if (f->is_uintx()) {
267
return set_uintx_flag(f->name(), flag_value, origin, err_msg);
268
} else if (f->is_uint64_t()) {
269
return set_uint64_t_flag(f->name(), flag_value, origin, err_msg);
270
} else if (f->is_size_t()) {
271
return set_size_t_flag(f->name(), flag_value, origin, err_msg);
272
} else if (f->is_double()) {
273
return set_double_flag(f->name(), flag_value, origin, err_msg);
274
} else if (f->is_ccstr()) {
275
return set_ccstr_flag(f->name(), flag_value, origin, err_msg);
277
ShouldNotReachHere();
279
return JVMFlag::ERR_OTHER;
282
// a writeable flag setter accepting 'jvalue' values
283
JVMFlag::Error WriteableFlags::set_flag_from_jvalue(JVMFlag* f, const void* value, JVMFlagOrigin origin,
284
FormatBuffer<80>& err_msg) {
285
jvalue new_value = *(jvalue*)value;
287
bool bvalue = (new_value.z == JNI_TRUE ? true : false);
288
return set_flag_impl<JVM_FLAG_TYPE(bool)>(f->name(), bvalue, origin, err_msg);
289
} else if (f->is_int()) {
290
int ivalue = (int)new_value.j;
291
return set_flag_impl<JVM_FLAG_TYPE(int)>(f->name(), ivalue, origin, err_msg);
292
} else if (f->is_uint()) {
293
uint uvalue = (uint)new_value.j;
294
return set_flag_impl<JVM_FLAG_TYPE(uint)>(f->name(), uvalue, origin, err_msg);
295
} else if (f->is_intx()) {
296
intx ivalue = (intx)new_value.j;
297
return set_flag_impl<JVM_FLAG_TYPE(intx)>(f->name(), ivalue, origin, err_msg);
298
} else if (f->is_uintx()) {
299
uintx uvalue = (uintx)new_value.j;
300
return set_flag_impl<JVM_FLAG_TYPE(uintx)>(f->name(), uvalue, origin, err_msg);
301
} else if (f->is_uint64_t()) {
302
uint64_t uvalue = (uint64_t)new_value.j;
303
return set_flag_impl<JVM_FLAG_TYPE(uint64_t)>(f->name(), uvalue, origin, err_msg);
304
} else if (f->is_size_t()) {
305
size_t svalue = (size_t)new_value.j;
306
return set_flag_impl<JVM_FLAG_TYPE(size_t)>(f->name(), svalue, origin, err_msg);
307
} else if (f->is_double()) {
308
double dvalue = (double)new_value.d;
309
return set_flag_impl<JVM_FLAG_TYPE(double)>(f->name(), dvalue, origin, err_msg);
310
} else if (f->is_ccstr()) {
311
oop str = JNIHandles::resolve_external_guard(new_value.l);
312
if (str == nullptr) {
313
err_msg.print("flag value is missing");
314
return JVMFlag::MISSING_VALUE;
317
ccstr svalue = java_lang_String::as_utf8_string(str);
318
JVMFlag::Error ret = WriteableFlags::set_ccstr_flag(f->name(), svalue, origin, err_msg);
321
ShouldNotReachHere();
323
return JVMFlag::ERR_OTHER;