2
* Copyright (c) 2011, 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"
27
#include "memory/allocation.inline.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "runtime/javaThread.hpp"
30
#include "services/diagnosticArgument.hpp"
31
#include "utilities/globalDefinitions.hpp"
33
StringArrayArgument::StringArrayArgument() {
34
_array = new (mtServiceability) GrowableArray<char *>(32, mtServiceability);
35
assert(_array != nullptr, "Sanity check");
38
StringArrayArgument::~StringArrayArgument() {
39
for (int i=0; i<_array->length(); i++) {
40
FREE_C_HEAP_ARRAY(char, _array->at(i));
45
void StringArrayArgument::add(const char* str, size_t len) {
47
char* ptr = NEW_C_HEAP_ARRAY(char, len+1, mtInternal);
48
strncpy(ptr, str, len);
54
void GenDCmdArgument::read_value(const char* str, size_t len, TRAPS) {
55
/* NOTE:Some argument types doesn't require a value,
56
* for instance boolean arguments: "enableFeatureX". is
57
* equivalent to "enableFeatureX=true". In these cases,
58
* str will be null. This is perfectly valid.
59
* All argument types must perform null checks on str.
62
if (is_set() && !allow_multiple()) {
63
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
64
"Duplicates in diagnostic command arguments\n");
66
parse_value(str, len, CHECK);
70
void GenDCmdArgument::to_string(jlong l, char* buf, size_t len) const {
71
jio_snprintf(buf, len, INT64_FORMAT, l);
74
void GenDCmdArgument::to_string(bool b, char* buf, size_t len) const {
75
jio_snprintf(buf, len, b ? "true" : "false");
78
void GenDCmdArgument::to_string(NanoTimeArgument n, char* buf, size_t len) const {
79
jio_snprintf(buf, len, INT64_FORMAT, n._nanotime);
82
void GenDCmdArgument::to_string(MemorySizeArgument m, char* buf, size_t len) const {
83
jio_snprintf(buf, len, INT64_FORMAT, m._size);
86
void GenDCmdArgument::to_string(char* c, char* buf, size_t len) const {
87
jio_snprintf(buf, len, "%s", (c != nullptr) ? c : "");
90
void GenDCmdArgument::to_string(StringArrayArgument* f, char* buf, size_t len) const {
91
int length = f->array()->length();
94
for (int i = 0; i < length; i++) {
95
char* next_str = f->array()->at(i);
96
size_t next_size = strlen(next_str);
97
//Check if there's room left to write next element
98
if (written + next_size > len) {
101
//Actually write element
102
strcat(buf, next_str);
103
written += next_size;
104
//Check if there's room left for the comma
105
if (i < length-1 && len - written > 0) {
111
template <> void DCmdArgument<jlong>::parse_value(const char* str,
115
|| sscanf(str, JLONG_FORMAT "%n", &_value, &scanned) != 1
116
|| (size_t)scanned != len)
118
const int maxprint = 64;
119
Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),
120
"Integer parsing error in command argument '%s'. Could not parse: %.*s%s.\n", _name,
121
MIN2((int)len, maxprint),
122
(str == nullptr ? "<null>" : str),
123
(len > maxprint ? "..." : ""));
127
template <> void DCmdArgument<jlong>::init_value(TRAPS) {
129
this->parse_value(_default_string, strlen(_default_string), THREAD);
130
if (HAS_PENDING_EXCEPTION) {
131
fatal("Default string must be parseable");
138
template <> void DCmdArgument<jlong>::destroy_value() { }
140
template <> void DCmdArgument<bool>::parse_value(const char* str,
142
// len is the length of the current token starting at str
146
if (len == strlen("true") && strncasecmp(str, "true", len) == 0) {
148
} else if (len == strlen("false") && strncasecmp(str, "false", len) == 0) {
153
char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
156
PRAGMA_STRINGOP_TRUNCATION_IGNORED
157
// This code can incorrectly cause a "stringop-truncation" warning with gcc
158
strncpy(buf, str, len);
162
Exceptions::fthrow(THREAD_AND_LOCATION, vmSymbols::java_lang_IllegalArgumentException(),
163
"Boolean parsing error in command argument '%s'. Could not parse: %s.\n", _name, buf);
168
template <> void DCmdArgument<bool>::init_value(TRAPS) {
170
this->parse_value(_default_string, strlen(_default_string), THREAD);
171
if (HAS_PENDING_EXCEPTION) {
172
fatal("Default string must be parsable");
179
template <> void DCmdArgument<bool>::destroy_value() { }
181
template <> void DCmdArgument<char*>::destroy_value() {
182
FREE_C_HEAP_ARRAY(char, _value);
186
template <> void DCmdArgument<char*>::parse_value(const char* str,
188
if (str == nullptr) {
191
// Use realloc as we may have a default set.
192
_value = REALLOC_C_HEAP_ARRAY(char, _value, len + 1, mtInternal);
193
int n = os::snprintf(_value, len + 1, "%.*s", (int)len, str);
194
assert((size_t)n <= len, "Unexpected number of characters in string");
198
template <> void DCmdArgument<char*>::init_value(TRAPS) {
199
set_value(nullptr); // Must be initialized before calling parse_value
201
this->parse_value(_default_string, strlen(_default_string), THREAD);
205
template <> void DCmdArgument<NanoTimeArgument>::parse_value(const char* str,
207
if (str == nullptr) {
208
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
209
"Integer parsing error nanotime value: syntax error, value is null\n");
212
int argc = sscanf(str, JLONG_FORMAT, &_value._time);
214
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
215
"Integer parsing error nanotime value: syntax error\n");
218
while(idx < len && isdigit(str[idx])) {
222
// only accept missing unit if the value is 0
223
if (_value._time != 0) {
224
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
225
"Integer parsing error nanotime value: unit required\n");
227
_value._nanotime = 0;
228
strcpy(_value._unit, "ns");
231
} else if(len - idx > 2) {
232
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
233
"Integer parsing error nanotime value: illegal unit\n");
235
strncpy(_value._unit, &str[idx], len - idx);
236
/*Write an extra null termination. This is safe because _value._unit
237
* is declared as char[3], and length is checked to be not larger than
238
* two above. Also, this is necessary, since length might be 1, and the
239
* default value already in the string is ns, which is two chars.
241
_value._unit[len-idx] = '\0';
244
if (strcmp(_value._unit, "ns") == 0) {
245
_value._nanotime = _value._time;
246
} else if (strcmp(_value._unit, "us") == 0) {
247
_value._nanotime = _value._time * 1000;
248
} else if (strcmp(_value._unit, "ms") == 0) {
249
_value._nanotime = _value._time * 1000 * 1000;
250
} else if (strcmp(_value._unit, "s") == 0) {
251
_value._nanotime = _value._time * 1000 * 1000 * 1000;
252
} else if (strcmp(_value._unit, "m") == 0) {
253
_value._nanotime = _value._time * 60 * 1000 * 1000 * 1000;
254
} else if (strcmp(_value._unit, "h") == 0) {
255
_value._nanotime = _value._time * 60 * 60 * 1000 * 1000 * 1000;
256
} else if (strcmp(_value._unit, "d") == 0) {
257
_value._nanotime = _value._time * 24 * 60 * 60 * 1000 * 1000 * 1000;
259
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
260
"Integer parsing error nanotime value: illegal unit\n");
264
template <> void DCmdArgument<NanoTimeArgument>::init_value(TRAPS) {
266
this->parse_value(_default_string, strlen(_default_string), THREAD);
267
if (HAS_PENDING_EXCEPTION) {
268
fatal("Default string must be parsable");
272
_value._nanotime = 0;
273
strcpy(_value._unit, "ns");
277
template <> void DCmdArgument<NanoTimeArgument>::destroy_value() { }
279
// WARNING StringArrayArgument can only be used as an option, it cannot be
280
// used as an argument with the DCmdParser
282
template <> void DCmdArgument<StringArrayArgument*>::parse_value(const char* str,
284
_value->add(str,len);
287
template <> void DCmdArgument<StringArrayArgument*>::init_value(TRAPS) {
288
_value = new StringArrayArgument();
289
_allow_multiple = true;
291
fatal("StringArrayArgument cannot have default value");
295
template <> void DCmdArgument<StringArrayArgument*>::destroy_value() {
296
if (_value != nullptr) {
302
template <> void DCmdArgument<MemorySizeArgument>::parse_value(const char* str,
304
if (str == nullptr) {
305
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
306
"Parsing error memory size value: syntax error, value is null\n");
309
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
310
"Parsing error memory size value: negative values not allowed\n");
312
int res = sscanf(str, UINT64_FORMAT "%c", &_value._val, &_value._multiplier);
314
switch (_value._multiplier) {
316
_value._size = _value._val * 1024;
319
_value._size = _value._val * 1024 * 1024;
322
_value._size = _value._val * 1024 * 1024 * 1024;
325
_value._size = _value._val;
326
_value._multiplier = ' ';
327
//default case should be to break with no error, since user
328
//can write size in bytes, or might have a delimiter and next arg
331
} else if (res == 1) {
332
_value._size = _value._val;
334
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
335
"Parsing error memory size value: invalid value\n");
339
template <> void DCmdArgument<MemorySizeArgument>::init_value(TRAPS) {
341
this->parse_value(_default_string, strlen(_default_string), THREAD);
342
if (HAS_PENDING_EXCEPTION) {
343
fatal("Default string must be parsable");
348
_value._multiplier = ' ';
352
template <> void DCmdArgument<MemorySizeArgument>::destroy_value() { }