jdk

Форк
0
/
writeableFlags.cpp 
324 строки · 12.3 Кб
1
/*
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.
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 "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"
35

36
#define TEMP_BUF_SIZE 80
37

38
static void buffer_concat(char* buffer, const char* src) {
39
  strncat(buffer, src, TEMP_BUF_SIZE - 1 - strlen(buffer));
40
}
41

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 ");
45

46
    stringStream stream;
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') {
52
        break;
53
      } else if (range_string[i] != ' ') {
54
        buffer[j] = range_string[i];
55
        j++;
56
      }
57
    }
58
    buffer[j] = '\0';
59
  }
60
}
61

62
static void print_flag_error_message_if_needed(JVMFlag::Error error, const JVMFlag* flag, FormatBuffer<80>& err_msg) {
63
  if (error == JVMFlag::SUCCESS) {
64
    return;
65
  }
66

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: ");
72
  } else {
73
    buffer_concat(buffer, "Error: ");
74
  }
75
  switch (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:
91
      break;
92
    default:
93
      break;
94
  }
95

96
  err_msg.print("%s", buffer);
97
}
98

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);
104
  return err;
105
}
106

107

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);
114
  }
115
  err_msg.print("flag value must be a boolean (1/0 or true/false)");
116
  return JVMFlag::WRONG_FORMAT;
117
}
118

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) {
121
  int value;
122

123
  if (sscanf(arg, "%d", &value) == 1) {
124
    return set_flag_impl<JVM_FLAG_TYPE(int)>(name, value, origin, err_msg);
125
  }
126
  err_msg.print("flag value must be an integer");
127
  return JVMFlag::WRONG_FORMAT;
128
}
129

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) {
132
  uint value;
133

134
  if (sscanf(arg, "%u", &value) == 1) {
135
    return set_flag_impl<JVM_FLAG_TYPE(uint)>(name, value, origin, err_msg);
136
  }
137
  err_msg.print("flag value must be an unsigned integer");
138
  return JVMFlag::WRONG_FORMAT;
139
}
140

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) {
143
  intx value;
144

145
  if (sscanf(arg, INTX_FORMAT, &value) == 1) {
146
    return set_flag_impl<JVM_FLAG_TYPE(intx)>(name, value, origin, err_msg);
147
  }
148
  err_msg.print("flag value must be an integer");
149
  return JVMFlag::WRONG_FORMAT;
150
}
151

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) {
154
  uintx value;
155

156
  if (sscanf(arg, UINTX_FORMAT, &value) == 1) {
157
    return set_flag_impl<JVM_FLAG_TYPE(uintx)>(name, value, origin, err_msg);
158
  }
159
  err_msg.print("flag value must be an unsigned integer");
160
  return JVMFlag::WRONG_FORMAT;
161
}
162

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) {
165
  uint64_t value;
166

167
  if (sscanf(arg, UINT64_FORMAT, &value) == 1) {
168
    return set_flag_impl<JVM_FLAG_TYPE(uint64_t)>(name, value, origin, err_msg);
169
  }
170
  err_msg.print("flag value must be an unsigned 64-bit integer");
171
  return JVMFlag::WRONG_FORMAT;
172
}
173

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) {
176
  size_t value;
177

178
  if (sscanf(arg, SIZE_FORMAT, &value) == 1) {
179
    return set_flag_impl<JVM_FLAG_TYPE(size_t)>(name, value, origin, err_msg);
180
  }
181
  err_msg.print("flag value must be an unsigned integer");
182
  return JVMFlag::WRONG_FORMAT;
183
}
184

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) {
187
  double value;
188

189
  if (sscanf(arg, "%lf", &value) == 1) {
190
    return set_flag_impl<JVM_FLAG_TYPE(double)>(name, value, origin, err_msg);
191
  }
192
  err_msg.print("flag value must be a double");
193
  return JVMFlag::WRONG_FORMAT;
194
}
195

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");
202
  }
203
  print_flag_error_message_if_needed(err, flag, err_msg);
204
  return err;
205
}
206

207
/* sets a writeable flag to the provided value
208
 *
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
211
 */
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);
214
}
215

216
/* sets a writeable flag to the provided value
217
 *
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
220
 */
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);
223
}
224

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;
230
  }
231
  if (value == nullptr) {
232
    err_msg.print("flag value is missing");
233
    return JVMFlag::MISSING_VALUE;
234
  }
235

236
  JVMFlag* f = JVMFlag::find_flag(name);
237
  if (f) {
238
    // only writeable flags are allowed to be set
239
    if (f->is_writeable()) {
240
      return setter(f, value, origin, err_msg);
241
    } else {
242
      err_msg.print("only 'writeable' flags can be set");
243
      return JVMFlag::NON_WRITABLE;
244
    }
245
  }
246

247
  err_msg.print("flag %s does not exist", name);
248
  return JVMFlag::INVALID_FLAG;
249
}
250

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;
257
  }
258
  if (f->is_bool()) {
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);
276
  } else {
277
    ShouldNotReachHere();
278
  }
279
  return JVMFlag::ERR_OTHER;
280
}
281

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;
286
  if (f->is_bool()) {
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;
315
    }
316
    ResourceMark rm;
317
    ccstr svalue = java_lang_String::as_utf8_string(str);
318
    JVMFlag::Error ret = WriteableFlags::set_ccstr_flag(f->name(), svalue, origin, err_msg);
319
    return ret;
320
  } else {
321
    ShouldNotReachHere();
322
  }
323
  return JVMFlag::ERR_OTHER;
324
}
325

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.